My Frustrations with Copying and Pasting in Neovim

Adam Drake
5 min readMar 25, 2024

Arrrghhhh!!

That’s what I felt when I knew I had to copy something from Neovim and paste it into another program like Chrome or Slack. Why was copying and pasting to the system clipboard so freaking difficult with Neovim? I had to press like four different buttons to enable the functionality and even then it copied text with line numbers in it. It was so frustrating! Surely there was a better way. Turns out there way. It just took me 5 months of “suffering” before I found it.

(Going off on a tangent… this 5 months of basically not researching to find a better solution is an interesting human condition. It’s when you have something that works but you just know it’s not optimal but there is something within you preventing you from taking the time and effort required to find that “better solution”. Is it a fear that this “better solution” doesn’t exist? Is it a fear that it could require a tremendous amount of effort which will lead you down a horrendously long rabbit hole from which you may never escape? Is it just pure laziness? What is at the root of this pervasive procrastination that invades all aspects of our lives? I wonder…)

Neovim, like Vim, uses the concept of registers to store text.

Yanking in Neovim

If you want to copy and paste something within Neovim then you have many possibilities.

  • yy This is known as “Yanking”. If you have a cursor on a specific line and you are in NORMAL mode, then pressing yywill “Yank” (essentially copy) the current contents from that line. The first y indicates the start of the yank operation and the second y specifies that the scope of the yank operation is the whole of the current line. Then pressing P or p will paste it. P — Line above. p — Line below
  • dd This is similar to “Yanking” but this will delete the current contents of the line also. It’s essentially “cut and paste” in a traditional editor.

You can also use y or d in VISUAL mode on selected characters. If you select a bunch of characters and press one of these buttons, again it will copy the content and you can use P or p again to paste it somewhere else within Neovim.

Under the Hood with Yanking

What is actually going on in Neovim when you press y or d? Neovim, like Vim, uses the concept of registers to store text. When the yank operation is run it takes the copied text and stores it in the default register. Registers are storage spaces that can hold text.

When the p paste command is run it then retrieves the content from the register and inserts it into the document in the specified location.

This meant the issue I was having is this default register, which is the unnamed register, is not accessible to the system clipboard. When trying to paste some yanked content to another program, the system clipboard didn’t use this unnamed register to retrieve content.

Turns out there is a better way

So it turns out, like most things in Neovim I have learnt, that there is a better way. You can use “Special Registers”. These “Special Registers” are linked to the system clipboard (not sure how, that is another rabbit hole I will save for a future date) so when using them you can copy text from Neovim and paste that text into other programs.

Using the "+ and "* Registers

The "+ and "* registers are the special registers because they can interact with the system clipboard:

  • The "+ register is for the system clipboard that you interact with using Ctrl+C and Ctrl+V (or Cmd on macOS).
  • The "* register is for the primary selection in X11 (Linux and Unix-like systems), which you typically interact with using middle-click.

To copy text to the clipboard:

  1. Select the text in VISUAL mode by pressing v, then move to select the text you want to copy.
  2. Press "*y to copy to the primary selection or "+y to copy to the clipboard.

To paste from the clipboard:

  • Press "*p or "+p in NORMAL mode to paste the content of the primary selection or clipboard, respectively.

Key Maps

As a finishing touch I added a key map for this "+y command as it is something I use very regularly:

map("v", "<leader>y", '"+y', opts)

I just used that key map to copy this from my Neovim config file into this article! It’s already helpful.

However, through continual usage and a will to improve I managed to learn these concepts and also more about the underlying workings of the system I use everyday.

Copying and Pasting Multiple Times

One other issue I have had in the past with Neovim is replacing multiple instances of the same word in a file. In VSCode there is the beautiful cmd + d keyboard shortcut which on first press selects the current word the cursor is on. Then on all subsequent presses it selects the next instance of that word. When you have multiple instances selected you can type in the replacement word.

I haven’t found something similar in Neovim yet. I have found though that if you select a word and yank it — y then you can paste that word to replace another word but once you pasted once you have to reyank it to paste it again! If you have 5 items to replace this is very annoying. I know you can do the :%s/ global find and replace but its a bit tedious and sometimes I only want to replace a few words and this seems over kill.

I have found at least as a short term solution if you yank the original word to paste into the special register for the system clipboard you can then replace every other instance using cmd + v Not a great solution but does saves the constant reyanking.

Conclusion

I have found with Neovim that there are many ways to do the same thing. I have also found that tasks that appear simple in editors like VSCode sometimes are not so straightforward in Neovim. This copy and paste is a great example of this. However, through continual usage and a will to improve I am learning more concepts and also more about the underlying workings of the system I use everyday. I think it’s really important to continually dive deeper and deeper into one’s understanding of the system one uses.

It’s relatively easy to pick up a tool designed by amazingly smart and talented people and live in ignorance to how it all works under the hood. But if you are a developer then I think you should take the time to at least gain some understanding. After all, there is no magic going on… usually.

About me

I am a Frontend Developer working mainly with React and Typescript in my day to day professional life. I love exploring new tools and libraries and love the Javascript Ecosystem.

I like to write blog posts that share exciting new tools I’ve discovered, how-to articles and also the occasional opinion piece.

I live in Prague in the Czech Republic with my family.

Check out the original blog post on my blog.

Check out my Linkedin and Github if you are interested.

--

--

Adam Drake

I'm a Frontend Developer and I write about all things Frontend Related - Think lightly of yourself and deeply of the world. Based in Prague, CZ