Vim: How to use Multiple Clipboards to do Copy and Paste on Steroids
Tags: programming, productivity, softwaredevelopment, productivityhacks
Published: 2019-04-14 07:05:25

---
title: Vim: How to use Multiple Clipboards to do Copy and Paste on Steroids
published: true
tags: programming,productivity,software-development,productivity-hacks
canonical_url: https://www.datastuff.tech/programming/vim-squeezing-text-editors-juice-with-more-features/
---

### Vim: Squeezing the Text Editor’s Juice with More Features

![](https://cdn-images-1.medium.com/max/1024/1*PwUdhpg4ULj5RKb81TKxpA.jpeg)<figcaption>Productivity is always one pomodoro away. Source: <a href="https://pixabay.com/en/tomatoes-vegetables-red-food-5356/">Pixabay</a></figcaption>

Vim is the Swiss-army knife of text editing. It’s not enough that it has a feature and command for almost every use case and user: it will also let you customize it to add whatever specific things you think it’s missing.

In this tutorial we’re going to see how to use two of those features: _multiple windows_, and _multiple vim registers_.

This tutorial will assume you’re already familiar with the basics of Vim, so I’d suggest you check out [this article](http://www.datastuff.tech/programming/vim-start-using-the-text-editor-for-developers/) if you don’t know where to start.

### Multiple Vim Windows

Whenever we’re editing a file, here are two things we may want to do:

- Edit two different files from the same terminal.
- Edit two different parts of the file at the same time.

Luckily for us, Vim allows us to do this without having to open a new tab on our terminal.

### Many Vim windows for the same file

Let’s say we’re editing a Python script, and there are two different functions in it: f1 and f2 we’d like to edit at the same time.

Vim makes this easy:

- Press _Ctrl + w_ (_cmd + w_ in a Mac).
- Press _v_ (for vertical) or _h_ (for horizontal).

This will create a vertical splice, or a horizontal one. This means the file will be opened in a different ‘window’ besides the current one (inside the same terminal), with the cursor on the same line.  
Vertical or Horizontal are the two possible ‘splits’ of the screen, so _Ctrl + w v_ will open the file in two windows side by side, whereas _Ctrl + w h_ will open one below the other.

To close a window after you’re done using it, just press :q to exit it.

To open a different file in a new Vim window, we can type _:vsplit \<filename\>_ to effectively edit many different files from a single terminal.

### Navigating between windows

Now you have two different windows pointing to the same file. Whenever you edit something in one, it will automatically update the other (unlike Sublime or other editors, where you have to press reload).

To quickly navigate to the function we want to edit, we’ll just

- Press / to enter search mode.
- Type the name of the function.
- Press enter.

This is equivalent to doing _Ctrl + F_ in other editors (in Vim, _Ctrl + F_ acts like page-down).

If we want to move the cursor between different windows, we have to press _Ctrl + W_ and then any motion, like _h_ or _l_ to move horizontally.   
For instance if we had opened, say, 5 windows vertically, we could press _Ctrl + W, 3l_ to move three windows to the right.

#### Customizing navigation between Vim windows with the .vimrc file

This seems like a good enough place to introduce the _.vimrc_ file. This small text file, under the _~_ directory, stores all your configurations for Vim, and will allow you to customize the text editor even more.

Configuring the text editor is as simple as editing this file, and you can do a lot of things with it. You can edit it using Vim, for maximum recursiveness.

Usually one of the most common things you will do with it is mapping a key or a key combination, to a different sequence you find awkward.

In this case, pressing _Ctrl + W_ and then _h,j,k_ or _l_ can be a bit uncomfortable if you’re doing it often enough. What I find many people do (and I do myself) is adding the following lines to their .vimrc file:

```
nnoremap \<C-h\> \<C-w\>h
nnoremap \<C-j\> \<C-w\>j
nnoremap \<C-k\> \<C-w\>k
nnoremap \<C-l\> \<C-w\>l
```

Lines starting with nnoremap will map a key combination (for instance, _Ctrl + h_) to a different one (for instance, _Ctrl+W, h_).

I find pressing _Ctrl + h,j,k,l_ a lot more fluid than the whole sequence, and most times you’re only switching between a few windows anyway.   
Of course if you’d prefer a different shortcut, go at it! That’s the beauty of this editor.

### Multiple Vim Registers: Have Many Clipboards.

Here’s another awesome thing we can do with Vim: Is there some particular string you find yourself typing a lot?  
For instance, it may be that you start many of your Python programs with

```
import pandas as pd
import seaborn as sns
import tensorflow as tf
```

And don’t want to ever type that again.

Or maybe a whole snippet, like

```
for(int i = 0; i \< N; i++){}
```

For all of these things and probably many others, it would be very convenient if we could have a lot of different buffers from which to ‘paste’ strings.

IDEs usually automate a bit of this, but they often won’t let us customize which snippets can be added and will only allow us to use some predefined ones.

Vim, however, has almost forty different buffers we can use for this exact purpose. Each of them is like a new clipboard.

### Yanking and Putting with Vim registers

I already said before how you can ‘yank’ and ‘put’: Vim’s equivalent of copy and paste, except yanked strings won’t go into the clipboard, but to a reserved memory buffer inaccessible outside of Vim instead.

I lied a bit there. It’s not a reserved buffer, but many. To decide which buffer to yank things into, or put things from, press “ and then a key in _[a-z], [A-Z]_ or _[0–9]_.

For instance, if we are editing a text and find ourselves typing the words “particular”, “especially” and “text editor” very often, we can just do:

- Press _v_ to enter visual mode and select the word by moving the cursor (for this example, let’s use “particular”)
- Press _“p_ to select the _p_ register and then _y_ to yank into it.
- Whenever we have to type the word again, just press _“p_ to say which register to put from, and then _p._

If we wish to see the registers’ contents, we can run _:reg_ to list them all, or_ :reg_ followed by a space separated list of register names (e.g.,_ :reg a b_) to see each one individually.

### Vim registers: special considerations

Lowercase and uppercase letters point to the same Vim registers. However, using lowercase names will overwrite the register’s content, whereas uppercase letters tell Vim to concatenate new strings to the current content without deleting it.

Numbered registers can be used in the same way as the others, but will also store the last 10 deleted bits of text, as a stack. When you delete something, it goes into register 1, and it’s moved into register 2 when you delete the next thing, and so on until register 9 is filled.

Interestingly, Vim registers persist even after we close Vim. This means eventually, you may add some very convenient snippets or lines to a register, and use them in different text editing sessions. For this reason, it is worth paying attention to patterns in what we usually write, and seeing how much we can optimize our editing process with these tools.

If our register’s contents can be interpreted as a Vim command, pressing _@r_ (where _r_ is the register’s name) will run them.

To erase a Vim register’s contents, press _qrq_ if _r_ is your register’s name.

### Conclusion

Using Vim can save us a lot of time, and that’s especially true if we take the time to customize it to our needs.

Multiple Vim registers can make text editing a lot quicker, and replace the ‘snippets’ functionality from IDEs, allowing us a use many clipboards.

Multiple Vim windows allow us to edit closely connected pieces of code in a single file, or taking a look at different files from a single project and seeing how well they fit together.

Combining these two tools can produce a huge boost in our productivity when editing files. However, the registers bit should be used with caution, since it may make us a bit more dependent on our environment.

That’s all for today, folks. I hope you’ve found this article useful, or at least entertaining. If there’s anything you think I should’ve mentioned and didn’t, or any part of this that could be improved or is plain wrong, please let me know. Feedback from my readers is one of my favorite parts of writing, and it allows me to learn more about these topics.

Have you already used these features? What are some useful things to store in our Vim registers? What other features should I cover in a different article? Let me know in the comments!

_Follow me on_ [_Medium_](http://www.medium.com/@strikingloo) _and_ [_Twitter_](http://twitter.com/strikingloo) _to keep reading more Tutorials, tips and tricks for developers and data scientists. If you liked this article, share it with a programmer friend!_

_Originally published at_ [_www.datastuff.tech_](http://www.datastuff.tech/programming/vim-squeezing-text-editors-juice-with-more-features/)

* * *