Skip to content

Commit

Permalink
fix multi-line yank issue, close #220
Browse files Browse the repository at this point in the history
The previous way of keep the cursor while yanking is not perfect.
It will break multi-line yank (`{count}yy`).
  • Loading branch information
jdhao committed Aug 28, 2023
1 parent 1391ca7 commit fc1c447
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
19 changes: 18 additions & 1 deletion lua/custom-autocmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "*",
Expand Down
14 changes: 0 additions & 14 deletions lua/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,6 @@ end
-- insert semicolon in the end
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
keymap.set("i", "<C-A>", "<HOME>")
keymap.set("i", "<C-E>", "<END>")
Expand Down

4 comments on commit fc1c447

@baldricni
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used the :PackerSync and :PackerUpdate, but I found it doesn't work still?

@jdhao
Copy link
Owner Author

@jdhao jdhao commented on fc1c447 Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have switched to lazy. now you should use Lazy sync.

@baldricni
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I get into nvim, “Plugins: 52 loaded / 83 installed” seems not load more than 52, is it fine?

@jdhao
Copy link
Owner Author

@jdhao jdhao commented on fc1c447 Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I get into nvim, “Plugins: 52 loaded / 83 installed” seems not load more than 52, is it fine?

It is fine, because a lot of plugins are lazy-loaded, also check this issue: #224

Please sign in to comment.