1
0
mirror of https://github.com/jdhao/nvim-config.git synced 2025-06-08 14:14:33 +02:00

fix multi-line yank issue, close #220

The previous way of keep the cursor while yanking is not perfect.
It will break multi-line yank (`{count}yy`).
This commit is contained in:
jdhao 2023-08-28 21:08:51 +02:00
parent 1391ca7ff1
commit fc1c447319
2 changed files with 18 additions and 15 deletions

View File

@ -17,14 +17,31 @@ api.nvim_create_autocmd({ "BufRead" }, {
}) })
-- highlight yanked region, see `:h lua-highlight` -- highlight yanked region, see `:h lua-highlight`
local yank_group = api.nvim_create_augroup("highlight_yank", { clear = true })
api.nvim_create_autocmd({ "TextYankPost" }, { api.nvim_create_autocmd({ "TextYankPost" }, {
pattern = "*", pattern = "*",
group = api.nvim_create_augroup("highlight_yank", { clear = true }), group = yank_group,
callback = function() callback = function()
vim.highlight.on_yank { higroup = "YankColor", timeout = 300 } vim.highlight.on_yank { higroup = "YankColor", timeout = 300 }
end, end,
}) })
api.nvim_create_autocmd({ "CursorMoved" }, {
pattern = "*",
group = yank_group,
callback = function()
vim.g.current_cursor_pos = vim.fn.getcurpos()
end,
})
api.nvim_create_autocmd("TextYankPost", {
pattern = "*",
group = yank_group,
callback = function(ev)
vim.fn.setpos('.', vim.g.current_cursor_pos)
end,
})
-- Auto-create dir when saving a file, in case some intermediate directory does not exist -- Auto-create dir when saving a file, in case some intermediate directory does not exist
api.nvim_create_autocmd({ "BufWritePre" }, { api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = "*", pattern = "*",

View File

@ -201,20 +201,6 @@ end
-- insert semicolon in the end -- insert semicolon in the end
keymap.set("i", "<A-;>", "<Esc>miA;<Esc>`ii") keymap.set("i", "<A-;>", "<Esc>miA;<Esc>`ii")
-- Keep cursor position after yanking
keymap.set("n", "y", "myy")
api.nvim_create_autocmd("TextYankPost", {
pattern = "*",
group = api.nvim_create_augroup("restore_after_yank", { clear = true }),
callback = function()
vim.cmd([[
silent! normal! `y
silent! delmarks y
]])
end,
})
-- Go to the beginning and end of current line in insert mode quickly -- Go to the beginning and end of current line in insert mode quickly
keymap.set("i", "<C-A>", "<HOME>") keymap.set("i", "<C-A>", "<HOME>")
keymap.set("i", "<C-E>", "<END>") keymap.set("i", "<C-E>", "<END>")