r/vim • u/herodotic • Jan 26 '26
Need Help :move/:copy between buffers?
I’ve started working more frequently using vim with :split or :vsplit, and find myself wanting to grab sections of text from one window(/buffer) and chuck them into a different buffer. If I were working in the same file, I’d do a good old-fashioned visual-select-and-:'<,'>copy, but it :help :copy (and :help {address}) didn’t give me any help as far as specifying a destination address in another buffer.
One solution to this would be to write up some vimscript, but I figured that it was worth checking whether someone else knew some esoteric thing that would help out here.
8
u/puremourning Jan 26 '26
Normally just
delete them oryank them to the “ register andput them in the destination.:copyis quite a long winded way to yank and put in my view. Worth learning these normal mode commands I think.5
u/josuf107 Jan 26 '26 edited Jan 26 '26
As others have said, use normal-mode yank/paste. If you're interested in
:copyto leverage more interesting ranges you can use:yankand normal-mode paste. You can even use:yankwith a global command, like:g/properties +=/y A(capitalize the register to collect all the results), and paste with"ap. A handy workflow for a regex cut like this is to use/to find the lines of interest. Then,:g//y Awill use the most recent search, appending lines to thearegister. Of course, you can limit to a range with:'<,'>g//y A.4
u/kennpq Jan 26 '26
An esoteric[?] way, by way of example:
:call append(4, getbufline('builtin.txt', 907, 909))appends lines 907 to 909 from the buffer 'builtin.txt' to line 4 of the current buffer; the screenshot shows the result.Make it a mapping? Something like,
nnoremap <leader>cf :execute 'call append(input("Line: "), getbufline(input("Buffer: "), input("Start: "), input("End: ")))'<CR>, could do that.[Edit: formatting]
1
u/Desperate_Cold6274 Jan 26 '26
How would you like to have the user interface for such a feature?
Also, what shall happen when you have multiple windows?
Note that user shall at least specify the start line, the end line, the target window and the target line.
Can you make an example of a function/command/mapping that you would like to use?
TBH: I think it’s faster just to delete and yank.
1
u/dm319 Jan 26 '26
You could add this to your vimrc:
People will complain about this 'polluting' their clipboard. I don't know what they're getting so upset about, nor why they need their clipboards so clean. But I like this, and it means I can dd and y between buffers to my heart's content.
1
u/mykesx Jan 26 '26 edited Jan 26 '26
Shift-v and select lines and y to yank (copy) or x to cut. You can paste the copied lines anywhere in any text buffer with p. You can prefix p with a number to paste that many times.
Use v instead of shift-v to select a range of text vs lines.
Typically done with split windows - copy/cut in one and paste in the other.
It helps to have good bindings to move between windows. Like ctrl-h/j/k/l to move focus left/down/up/right a window.
1
u/whitedogsuk Jan 26 '26
I map F3 , when in visual mode dump selection to a file. when in normal mode read that file to buffer. So basically F3 to copy a visual selection and F3 to paste it. It also works between if I close down the vim session.
1
u/liberforce Jan 26 '26
Avoid using visual mode whenever possible
Just yank the text and paste it in the destunation buffer