vim solution :
source
You can just copy the paragraph to the clipboard with "+yip
Instead of selecting the paragraph and copying it just set an auto command with the TextYankPost
event. In vim, I think you will need a plugin and in neovim, you can see an example below. Basically, the TextYankPost event will highlight your copy for some milliseconds.
If you simply copy you can use this function:
augroup yank_restore_cursor
autocmd!
autocmd VimEnter,CursorMoved *
\ let s:cursor = getpos('.')
autocmd TextYankPost *
\ if v:event.operator ==? 'y' |
\ call setpos('.', s:cursor) |
\ endif
augroup END
Neovim solution:
local augroups = {}
augroups.yankpost = {
save_cursor_position = {
event = { "VimEnter", "CursorMoved" },
pattern = "*",
callback = function()
cursor_pos = vim.fn.getpos('.')
end,
},
highlight_yank = {
event = "TextYankPost",
pattern = "*",
callback = function ()
vim.highlight.on_yank{higroup="IncSearch", timeout=400, on_visual=true}
end,
},
yank_restore_cursor = {
event = "TextYankPost",
pattern = "*",
callback = function()
local cursor = vim.fn.getpos('.')
if vim.v.event.operator == 'y' then
vim.fn.setpos('.', cursor_pos)
end
end,
},
}
for group, commands in pairs(augroups) do
local augroup = vim.api.nvim_create_augroup("AU_"..group, {clear = true})
for _, opts in pairs(commands) do
local event = opts.event
opts.event = nil
opts.group = augroup
vim.api.nvim_create_autocmd(event, opts)
end
end
NOTE: In both cases, it will only work in normal mode, although being possible to enhance it with some code.
The idea for the neovim solution and the vim solution comes from this answer on reddit.
mm "+yap `m
(Note that"+y
yanks to your system clipboard)