Productivity is always one pomodoro away. Source: Pixabay
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 if you don’t know where to start.
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.
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.
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.
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<br>nnoremap <C-j> <C-w>j<br>nnoremap <C-k> <C-w>k<br>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.
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<br>import seaborn as sns<br>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.
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.
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.
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 and Twitter 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