In this article, we discuss how we can copy and paste from multiple registers in vim/noevim effectively. This article is meant for vim user who has knowledge of the basic commands in vim. This article explains the basic concepts of copy and paste commands, registers with examples and also discusses how we can add keybindings for faster copy-pasting from/to registers.
What is cut, copy, and paste in VIM?
Vim does not offer cut and copy. Instead Vim offers yank(y), delete(d). Yank is synonymous with the copy command and the delete command is functionally similar to the cut command. There is nothing interesting about how copy and paste works – the copy command saves the selected part of the text in a named memory location called register (not to be confused with registers in the CPU). You can now paste the text somewhere else in the document or maybe in another document.
What is special about vim copy and paste?
Vim is special and so is yank and paste in vim. Unlike many other editors, vim offers its own set of registers. Most of the text editors rely only on the registers maintained by the operating system, for the copy and paste operations.
This can become overwhelming at a point in time, where you need to copy/cut from multiple sources at a time. In a normal text editor, you have to copy and paste a single selection and then move on to the next selection. How ineffective!!!
Vim being the god of editors, allows you to be more efficient and error-proof copy-pasting without any hassle. But to get to that point we need a bit more knowledge about the registers that vim has to offer.
Understanding the multiple registers in VIM
Although vim offers a multitude of registers (each has its own special purpose), we are interested in few important ones which we will be discussing here.
- The Unnamed Register: Vim doesn’t normally interact with the OS register for yanking or deleting. Everything you yank or delete without mentioning the register(eg: x/X, d/D, y/yy) are stored in the unnamed register.
- The Numbered Registers: Vim offers numbered registers from 0 to 9. These registers also have special purposes
- Number 0 – Stores the content of the last yank
- Number 1 – Stores the content of the last delete
- Number 2-9 – Stores the content of the previous deletes in respective order
- The Alphabets Registers: Vim offers registers dentoed by lower case alphabets(a-z) specifically for our own use. This registers have a special optional property – we can to append to (instead of replace)the text in register when necessary.
Apart from these vim specific registers, you can use the system registers for recording text outside of vim. There are two system registers:
- + : This the default system clipboard. We use Ctrl+Shift+C for pasting the contents from the clipboard to the shell.
- * : This register stores the currently selected item in the system. we generally use Ctrl+Shift+s for pasting the contents of the ‘select’ clipboard to the shell.
How to copy and paste from multiple registers in VIM?
Here are some of the commands in vim with examples of how to use them with multiple registers.
1. Yank to multiple registers in VIM
The (y)ank command yanks(copies) the selected text into the register x (if specified) else it is yanked to the unnamed register.
# Yank the selected text into the unnamed(default) register :y # Yank into register a :"ay # Yank into the system clipboard :"+y
2. Paste from multiple registers in VIM
The (p)paste command paste the text from the register x if specified, else it pastes from the unnamed register.
# paste from the unnamed register :p # paste from register a :"ay # Paste from the system clipboard :"+y
Pro Tip: You can append to a alphabet register by using Uppercase letters for yanking. Example:
# Yank and replace the current text in register a :"ay # Yank and append to the current text in register a :"Ay
Note: The vim integrated registers work for the buffers opened under the same vim process. The registers are not updated concurrently for two different vim processes. For copy-pasting between two different vim processes, the system clipboard is the best option.
Customizing VIM for increased productivity
Vim provides tons of features and to be honest no one uses all these features at once. Moreover copy-pasting from/to register is a little complex. To deal with this problem you can set the custom vim shortcuts for commands you use regularly. Here are some of the suggested key re-mappings that can help you out in faster copy-pasting
" Set a key-mapping for copy and pasting to the system clipboard :vnoremap <Leader>y "+y :nnoremap <Leader>p "+p " Set key-mapping for dealing with two alphabetical registers easily " Two does the work for me, you can set more :vnoremap <Leader>a "ay :vnoremap <Leader>A "Ay :nnoremap <Leader>a "ap :vnoremap <Leader>x "xy :vnoremap <Leader>X "Xy :nnoremap <Leader>x "xp
You can add similar key-mappings to your .vimrc or init.vim for smooth and faster copy paste experience.
I hope you learned something new about copy pasting and registers in vim from this article. There are always more new commands and combinations to learn in vim, but this article should serve as good starting point for your day to day coding/text editing. If you require more help you can always refer to the inbuilt manual by pressing :h [search-term]. If you liked this article stay tuned for more such articles on vim.