Vim has a surprisingly robust pasting mechanism. For what looks like a basic text editing program, you can perform complex wizardry using concepts like registers, and you can even enhance its pasting functionality using plugins. All of this without resorting to a mouse! Vim is an exemplary example of how a keyboard-based tool can encapsulate all the functionality you can get from a mouse-based application. Of course, you must learn and get used to all useful keyboard commands – a tall order for a newbie. But if you spend a lot of time in vim, making changes and sorting through large configuration files, the investment might be well worth it.
How to Perform a Simple Copy/Paste in Vim
Before we get into the advanced functionalities of how to paste in vim, let’s see how to quickly copy a selection of text and paste it like you would with any other GUI program. After opening vim, move your cursor to the point where you want to start your selection, and press “v”. This puts vim into “visual” mode, and you can then move your cursor to continue selecting as shown here:
It’s worth noting that the selection includes the green cursor mark. So if you want to select just one character, for example, you just hit “v” and leave it. The character on which you hit “v” is already selected.
The vim terminology for “copy” is “yank”, represented by the letter “y”. So when you have your selection ready, press “y” and vim will “yank” the highlighted text into its buffer. The cursor will go back to the beginning of the selection, and the “visual” mode text at the bottom will vanish.
Now to paste the text you just copied, go to the location where you want to paste it, and press “p” to paste the text after the cursor and “P” to paste it before the cursor. And that’s it! If you want, you can stop reading here, because that’s the most that a regular text editor like Notepad in Windows will allow you to do. But if you want to know more about what vim is capable of, then read on!
To understand this, we first need to learn about “registers” in Vim.
Vim Registers
A “register” in vim, is simply a storage location that can be identified with a character. With Vim, you can identify a register using a single digit, a single alphabetic character, or a number pre-set character that stores specific pieces of text.
So this means, we have the following registers:
- 0-9
- a-z
- Bunch of special characters
Of these, the registers numbered from a-z are for general use, in the sense that what you put into them won’t change once you’ve stored something inside it. They’re called “named registers”. The other registers, such as the double-quote (“) and zero (0) store the most recently yanked text, so they get overridden when a new text is yanked.
You can see the content of all the registers in vim using the command:
:registers
For context, here’s what’s stored in my vim editor right now:
As you can see, the double-quote and zero registers contain the same text because that was the last “yank”. The “a” register contains some text. The other registers you see are special cases that contain information. For example, the percentage sign (%) register contains the name of the current file, and the forward-slash (/) register contains the most recent search pattern. Here are the most common and useful special character registers:
Using Vim Registers to Copy (Yank) and Paste
Now that you know what registers are and how they work, here’s how to yank text from vim into a register. Like before, you first select something in vim, and then if you want to yank your selection to the “a” register, you type:
"ay
In the following screenshot, I’m going to yank “let data” on the third line, into the “a” register.
You can see below, that “let data” is now allocated to the “a” register:
And then when you want to paste the contents of the “a” register, go to the place where you want to paste it and type:
"ap
This will paste whatever’s in “a” after the cursor. If you type:
"aP
Then it will put everything before the cursor and move everything else up ahead.
Using “Ctrl+r” to Paste in Vim’s Insert Mode
You might have noticed that the above methods all work in vim’s “visual” mode, which isn’t what we usually use when modifying text files. When I work with text files and vim, I usually enter the “Insert” mode, which I access by pressing the “i” key.
While in insert mode, you can’t directly enter commands because whatever you type will show up on the screen instead of being interpreted as a command. So in insert mode, if you want to paste something, you must press “Ctrl+r”, followed by the name of the register. If you just want to paste the most recently yanked text, then you must use the “0” register.
Dealing with Vim’s Auto-Indentation While Pasting
Vim, while it’s pretty much a plain text editor, does sometimes try and auto-indent some stuff based on context. For example, if you’re writing code, vim might try and indent lines within code blocks by inserting a few additional spaces before each line. If you’re pasting multiple lines of structured content, vim can apply its auto-indentation rules to your content and mess up the final output by creating double indentations and other unexpected changes.
To avoid this, it’s best to set vim in “paste mode” before pasting many lines of text. We do this with the command:
:set paste
Setting the paste mode tells vim that you’re going to be pasting content and that it should do automatic line wrapping, for example. Once you’re done with pasting, you can restore the default mode using:
:set nopaste
You can actually permanently disable auto-indentation in vim by modifying the ~/.vimrc file, but that’s beyond the scope here.
Use Case for Vim’s Registers
While it might seem that the huge number of registers is overkill, it can be very useful when needing to combine different aspects of a document from different sources. Keep in mind that these registers maintain their data between vim sessions – so you can open a file, copy stuff into a register, then open another file and paste using that same register. In this way, you can combine text from multiple documents with a simplified workflow.
You can even use the registers as pseudo-shortcuts for common text patterns in your project, and the large number of registers makes this particularly powerful. The tools are available. It’s up to your imagination to find the use cases where you can use them.
Conclusion
As you can see, vim’s pasting capabilities are far superior to most other supposedly sophisticated text editors. The ability to have separate pasting storage locations in the form of registers makes it very versatile for stitching together complex documents without sections accidentally overwriting one another. Given that vim is a purely text-based tool, it’s quite amazing to see the depth of its pasting powers!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply