Right now every time I use paste p
I have to copy again the value.
It is possible to avoid this behaviour?
You can try map p
to pgvy
Tried, remember the copied string but move the cursor where I copied the original string and not where I put before the copy itself after pressing that key.
Lately, I am becomming used to see the content of registers to make sure I am doing the right thing:
:reg
Once I have a map helper function I normally do:
map('n', '<leader>r', '<cmd>registers<cr>')
Not every system has the “primary selection clipboard” ‘*’ but sometimes I have forgot to copy the string but at least I’ve selected and this saves me. Because this I hate the “unnamedplus” idea.
This suggestion follows in line with @voyeg3r 's strategy.
If you’re using telescope
, I found a plugin yesterday called nvim-neoclip
that adds a picker for registers.
The preview window is really helpful for looking at multiline yanks.
I added a mapping that opens the telescope neoclip picker and immediately goes into normal mode as I’m usually trying to scroll back through my last few registers as opposed to fuzzy searching for them.
map('n', "<leader>'", '<cmd>Telescope neoclip<cr><esc>') -- open neoclip menu AND transition to normal mode
The best thing I’ve found in my limited use of this plugin so far is that you can either apply an old register to where your cursor is with p
or P
OR you can update the value of your default "
register by selecting some found value, exiting the picker, and using the yanked value later.
I found this plugin while searching for an answer to your original question but decided to extend the editors behavior instead of overriding it, though I really can’t think of a situation where I’d use the default workflow of paste this chunk over that selection and replace the default register with the value overwritten by that paste.
I am not using telescope and I don’t want to use a picker as I am just using a single c&p and not multiple registers.
Have you red :h registers
?
As a tip last thing you copied/yanked (copied not deleted) is in the register 0
so yank what you want do any deletion operation and use this register. Also any deletion that’s less than a line like dw
is in the -
register.
Personally what I do since I consider hard to remember to use a named register to avoid losing it when handling many yanks/copies and delete operations is I have a function to copy registers interactively.
PD: very verbose function, far from the best.
M.is_register = function (register)
--@note: Return true if it's a valid vim register
--@param register: The register to check
--@type register string
if register:match('^[%l%d%-%*%.%+%%#/=":]$') then
return true
end
return false
end
M.copy_register = function (source, destination, fun)
--@note: Function to move registers around and modify them
--@source: Source register
--@destination: Destination register.
--@fun: Function to apply to the register, function must return a string.
--@return string: destination register after fun is applied
assert(source ~= nil, "Must provide a source register")
assert(destination ~= nil, "Must provide a destination register")
local reg_contents = vim.fn.getreg(source)
if type(fun) == "function" then
reg_contents = fun(reg_contents)
assert(type(reg_contents) == "string", "Function provided doesn't return a string")
end
-- Set register contents
vim.fn.setreg(destination, reg_contents)
return reg_contents
end
M.copy_register_interactive = function ()
---@note: Interactive function to move registers around
local source, destination, input, fun
input = vim.fn.input("Moving registers source (def:\") -> destination: ")
if string.len(input) < 1 then error("Input should be at least one registers") end
--If only one register provided use " as a source
if string.len(input) == 1 then source = '"' end
for c in input:gmatch(".") do
if M.is_register(c) then
if source == nil then
source = c
elseif destination == nil then
destination = c
end
elseif c:match("F") then
fun = M.format_commandline
end
end
print(string.format("Copying register %s to register %s", source, destination))
M.copy_register(source, destination, fun)
end
I have a mapping to copy_register_interactive
so I can move what I just copied to any other register quickly.
Thanks for the hints.
After various investigation I did it
- neovim - Clipboard is reset after first paste in Visual Mode - Vi and Vim Stack Exchange
- How do I replace-paste yanked text in vim without yanking the deleted lines? - Super User
- https://www.reddit.com/r/vim/comments/bk6ec8/how_can_i_fix_my_vim_clipboard_settings_so_that_i/
I needed to map not just to map
but to xenoremap.
So xnoremap { 'p', 'pgvy' }
in this way (if you are using GitHub - tjdevries/astronauta.nvim: You now feel at home traveling to the moon)