Editing multiple files in Vim

Splits and multi-file editing

Perhaps one of the most limiting features of the original vi is its inability to edit more than one file at once. vim removes this limitation; it can edit many files at the same time. In typical vim fashion, it provides many ways to do this. I’ll focus on the easiest way to visualize, which uses “splits,” which are like independent windows that can each contain a text file.

Opening a file in a split

Assuming you are already editing a file in a vim session and want to split the screen and edit a second file, you can use either of these commands:

:sp filename
Splits the screen horizontally and opens filename in the new area.
:vsp filename
Splits the screen vertically and opens filename in the new area.

For most purposes, horizontal splits are easier to manage. Any split can be further split — you can edit as many files in a single instance of vim as you like.

Managing splits

Once you are editing multiple files, you will probably need to move back and forth through your files and manage the size of the splits so that you can see the relevant portions of the files you’re interested in.

The prefix for all the below commands is <C-W> — that is, hold down Ctrl and type w. Press <C-W> before typing any of the below commands.

<C-W>
Moves to the next split.
k
Moves to the split above the current split.
Up
Moves to the split above the current split.
j
Moves to the split below the current split.
Down
Moves to the split below the current split.
+
Increases the size of the current split by one line. (Try combining this with counts — e.g. 5<C-W>+ will make the split five lines bigger.)
(Minus sign) Decreases the size of the current split by one line.
_
(Underscore) Maximize the current split (that is, make it take up as much room as possible.)

Saving and quitting

Each split acts something like an individual instance of vim — so :w and :q work on the current split. If you want those commands to apply to all the splits instead of just the current one, add a to them — for instance, :wa saves all the open files, :qa quits all open files (and exits vim), and :wqa saves and then closes all open files.

Recommended mappings

I recommend adding the below files to your .vimrc file to make working with splits easier:

map <C-J> <C-W>j<C-W>_ map <C-K> <C-W>k<C-W>_ set wmh = 0

The first two lines allow you to switch between splits much more smoothly — just press <C-J> to open and maximize the split below the current one and <C-K> to open and maximize the split above the current one. I chose these mappings because they correspond to vi’s default up and down keys, you might want to use different key combinations if you’ve ever used an editor that had hotkeys for moving from one open file to another.

The last line allows splits to reduce their size to a single line (which includes the filename and position); this saves a lot of space when you have many splits open. By default, vim forces splits to include an additional line that contains the line of text the cursor was on in that file.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s