It’s actually really easy to do, once you know how. Vim uses registers for copy/paste operations. By default it uses its own set of internal registers. It doesn’t use the clipboard. But vim does have a special register for the system clipboard.
In order to copy to the system clipboard in vim, you need to copy to the
+
register.
So for example, if you want to copy an entire line of text to the system clipboard in vim:
- Hit escape, to ensure you’re in command mode.
- Use the motion keys
hjkl
to move to the line you want to copy, or use a motion command like {line number}G
(where line number is the line you want to move to).
Once you’ve moved to the line you want to select:
- Use
V
to enter "visual line" selection mode. This will select everything on the current line.
- Type the command
"+y
- where
"
tells vim you want to use a register,
+
specifies the system clipboard register and
y
is the "yank" command/operation - which is the Vim equivalent to "copy". So the overall effect of that command will be to copy the highlighted line of text to the system clipboard.
Now the line of text will be in the system clipboard. So you should be able to paste it from the clipboard into any other external program that has access to the clipboard. Your browser, another text editor/IDE, etc.
NOTE: If you want to do a cut/paste operation, instead of copy/paste, you need to use the
d
command (delete) instead of
y
(yank).
In other words, select some text in one of vims visual selection modes (using V, v or c-v (where c- is vim-speak for the ctrl key)), then delete the text to the system clipboard using
"+d
.
This will copy the text to the clipboard and remove it from the file/buffer in vim. So this is equivalent to selecting text and using ctrl+x in windows (cut).
Again, once it’s in the clipboard, you can use an external program’s paste command to paste the text from the system clipboard.
Incidentally- another useful register in Vim is the /dev/null/ register, which is the
_
register. This comes in handy if you want to delete sensitive information from a file and you don’t want it being stored in vims registers/history.
So for example, if you’re deleting a password, or some sensitive contact information from a file and you don’t want that text lingering in any of vim’s default copy/paste registers, you’d delete it to the /dev/null register.
The process is exactly the same as the "cut" or “delete to the system clipboard” that we did before, but this time we use the /dev/null register
_
instead of the system clipboard register
+
.
So with a line of sensitive text selected, we’d use
"_d
to delete the text to /dev/null, to avoid it ending up in vim’s internal copy/paste registers.
Going the other way, if you have copied some text into the clipboard from your browser, or another program and you want to paste it into vim, you need to paste it via vim’s special clipboard register
+
.
So the order of operations is:
- Copy some text into the clipboard from your browser (or other external program)
- Switch to vim
- Use motion keys to move to the line where you want to paste the text to.
- Type the command
"+p
, where "+
tells Vim to use the system clipboard and p
is vim’s Paste command.
This will paste whatever text is currently in the clipboard into your current file in vim.