In this and future articles, I’m going to cover Vim features that you may not be familiar with if you’re a casual Vim user. If you’re confident using Vim to edit configuration files or make short edits in text files, but maybe not too comfortable with undertaking major writing or coding in Vim, then these articles should be for you.
View all the things
A really useful feature in Vim is the ability to split the viewable area between one or more files, or just to split the window to view two bits of the same file more easily. The Vim documentation refers to this as a viewport or window, interchangeably.
You may already be familiar with this feature if you’ve ever used Vim’s help feature by using
:help topic or pressing the F1 key. When you enter help, Vim splits the viewport and opens the help documentation in the top viewport, leaving your document open in the bottom viewport.
Splitting Vim’s viewport
If you want to use this feature for something other than viewing Vim’s help files, you can split the viewport by using
Ctrl-w n — they do the same thing, but just typing
:sp will save you a few characters, and it’s easier to use with arguments, such as filenames. Remember the
Ctrl-w part, though, because it will come in handy for most operations with windows.
:sp command will divvy up the viewport into two equal viewports for the file that you have open. If you’d like to work on two files simultaneously, no problem — just follow the command with the filename you’d like to use, like this:
That will open filename in the new viewport. You can even add a search string to that to move directly to the first instance of a keyword, like so:
:sp +/searchstring filename
Easy as falling off a log. What if you don’t want to have equal viewports? For example, let’s say you want to open a reference file in the top viewport, but want the majority of the viewport available for the file you’re actually editing. No problem. Just prepend a number to the
sp command, and the new viewport will fill that number of lines:
:10 sp filename
Now you have a viewport with 10 lines, but what if you’ve decided that you’d like to give both viewports equal real estate? That’s easy enough too. Instead of going into command mode, you can use a normal keybinding to accomplish this.
Ctrl-w = tells Vim to assign an equal number of lines to each viewport.
Movement between Vim viewports
To move between the viewports while working, use
Ctrl-w j to move down, and
Ctrl-w k to move up. This should prove easy to remember —
Ctrl-w for “window” commands, and the normal vi movement commands
j for down and
k for up. You can also cycle between viewports by using
You can increase or decrease a viewport’s size after it’s been created. Use
Ctrl-w + to increase the active viewport, and
Ctrl-w - to decrease its size by one line. If one line at a time isn’t sufficient, add a modifier before the
-. For instance, to add 13 lines, use
If horizontal viewports just don’t do it for you, Vim also supports splitting viewports vertically. To do this, just use
:vsplit if you prefer to spell it out.
Movement between vertical viewports is similar to moving between horizontal viewports.
Ctrl-w Ctrl-w works, and instead of using the j and k movement keys, use the h and l movement keys to move back and forth between viewports. To move to the viewport to the right, for example, you’d use
It’s also worth mentioning that you can open a file in a viewport just to view the file, without opening it to edit. To do this, use the
:sview filename command. To do it vertically, use
:vert sview filename .
You can close a window in one of several ways. The easiest is to just use the quit command,
:q, or you can use
Ctrl-w q. Note that if it’s the only window open with a file, Vim will prompt you to save the file if it isn’t saved already.
Rotating windows in Vim
Also, it’s possible to rotate the windows, if you decide you’d prefer to have the top window on the bottom or vice versa. To do this, use
Ctrl-w r to move windows to the right or down. When you do this, for example, in a Vim session with three horizontal viewports open, the top viewport would go to the middle position, the middle viewport would take the bottom position, and the bottom viewport would rotate to the top. To go in the opposite direction, use
Ctrl-w R instead.
Vim often offers several different ways to achieve the same things. For instance, as I pointed out,
Ctrl-w n all create a new viewport. I haven’t listed all of the possible commands or keybindings to accomplish all of the tasks here.
Over the years, I’ve found Vim’s split windows features to be very useful. I often use it to edit a column or article in one viewport, and to read and copy from notes in another viewport.
|Vim viewport keybinding quick reference|