From fc1c447319d2eca318c751d641f9ce8ce6a92005 Mon Sep 17 00:00:00 2001 From: jdhao Date: Mon, 28 Aug 2023 21:08:51 +0200 Subject: [PATCH] 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`). --- lua/custom-autocmd.lua | 19 ++++++++++++++++++- lua/mappings.lua | 14 -------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lua/custom-autocmd.lua b/lua/custom-autocmd.lua index b8284f24..bfa2e8ef 100644 --- a/lua/custom-autocmd.lua +++ b/lua/custom-autocmd.lua @@ -17,14 +17,31 @@ api.nvim_create_autocmd({ "BufRead" }, { }) -- highlight yanked region, see `:h lua-highlight` +local yank_group = api.nvim_create_augroup("highlight_yank", { clear = true }) api.nvim_create_autocmd({ "TextYankPost" }, { pattern = "*", - group = api.nvim_create_augroup("highlight_yank", { clear = true }), + group = yank_group, callback = function() vim.highlight.on_yank { higroup = "YankColor", timeout = 300 } 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 api.nvim_create_autocmd({ "BufWritePre" }, { pattern = "*", diff --git a/lua/mappings.lua b/lua/mappings.lua index 8a1e2e64..18533172 100644 --- a/lua/mappings.lua +++ b/lua/mappings.lua @@ -201,20 +201,6 @@ end -- insert semicolon in the end keymap.set("i", "", "miA;`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 keymap.set("i", "", "") keymap.set("i", "", "")