Another option is to use one of vims visual modes. If you're copying entire lines of text, it's best to use "visual line" mode.
In command mode:
- Go to the first line you want to copy
- Press
V
- as in captial v / shift-v
- Use vims navigation/motion keys to move the cursor to the last line you want to copy
- Yank the text using
y
, or if you're planning to do a cut/paste use d
to delete the text (which by default stores it in the copy/paste buffer)
Then move the cursor to the place you want to paste the text and use
p
to paste the copied block of text below the current line, or
P
to paste the copied block to the line above the cursor position.
If you're planning on pasting a block of text over the top of another block of text:
- Navigate to the first line of the text you want to replace.
- Enter visual line mode again and move the selection down to the last line you want to replace and then hit
p
to paste the copied text over the selected lines.
Another thing to consider:
If you plan to paste text from the copy/paste buffer over the top of a selection, the copied text will be copied over the top of the selected text but the selected text is deleted to the copy/paste buffer. So the copy/paste buffer ends up containing the lines of text you just replaced.
So if you plan to paste a copied piece of text multiple times in a document using a visual selection to select and replace other lines, you should yank the selected text to one of vims registers.
E.g.
Select the text you want to copy/cut using visual line mode, then yank using
"my
"
specifies to use a register,
m
is the name of the register (you can use any letter from a to z for the register) and
y
to yank the selection to register m.
Or for a cut:
"md
, where
"m
specifies register m and
d
deletes the selected text to register m.
Then to paste, you can use:
"mp
to paste from register m to the next line, or
"mP
to paste from register m to the previous line.
Or again, if using visual line mode to visually select a region of text to replace with the copied text, you select the lines to replace in visual line mode, then use
"mp
to paste from the register.
Because you’ve used a register, you can repeat the paste operation over visually selected areas as many times as you like.
Those lines of text will remain in register m until you overwrite the register with something else.
To sum up:
If you plan to paste to new lines of text - use visual selection with
y
or
d
to copy/cut to vims copy/paste buffer and then
p
or
P
to paste.
But if you plan to select an area to replace, consider copying/cutting to a named register, especially if you plan to use paste over more than one selected area. That way, you can recall the copied text from the register and all of the replaced text ends up in the default copy/paste buffer.
There is a way of recalling items from the default copy/paste buffers history. But I can’t remember how to do it without looking it up. I find It’s more convenient to just use a named register.
Another thing that can help you with making numbered actions in vim is to enable line numbers AND relative line numbers.
To do that, in command mode - enter the following command:
:set nu rnu
With line numbers AND relative line numbers shown, the current line is listed with whatever the current line number is. Lines above or below the current line are numbered from 1 onwards.
So the lines immediately above AND below the current line are numbered 1.
The next lines above AND below are numbered 2 etc.
E.g.:
2: some text
1: more text
102: <<<<This is the current line
1: blah blah blah
2: etc
3: waffle waffle
4: and so on….
That way you can just look at a line number and see how far away it is from the current line. So, we’re at line 102 and we want to delete from line 102 to the line that says "and so on….".
We enter visual line mode, we look at the line number next to the line containing "and so on…." and we see the number 4. So we know we want to move the selection four lines down. Instead of pressing
j
four times, we can simply type
4j
to select the entire area and then delete using
d
.
So by setting the line numbering to relative numbers, we can instantly see how far away any line is from the current line. That way, when making selections or doing any other line-based repeatable motion, we can do so without having to use any mental arithmetic. That can save you from having to spam the j and k keys a bunch of times!
Edit:
One other option I forgot to mention is the register for the systems copy/paste buffer, or the "clipboard" as it’s often referred to.
Rather than yanking/copying/deleting to vims internal copy/paste buffer, or to a named register, you can also copy to the system clipboard using vims
+
register.
E.g.
"+y
to yank to the system clipboard and
"+p
to paste from the system clipboard.
By copying to the system clipboard, you have the same benefit as using a named register - plus, you can also use the system clipboard to copy/paste to/from external applications.
So in Vim, you could yank a URL from a document into the system clipboard and then switch to your browser and hit
ctrl v
in the address bar to paste the URL.