Waits at the window wearing the face that she keeps in a jar by the door…

Editing multiple documents in vim: Using vim windows

We’ve talked about using buffers to edit multiple documents in vim. Each file lives in a buffer which, in turn, lives in it’s own tab page, rather like a tab in a web browser. But sometimes I want to view one file while editing another, and it would be handy to have both files in the same screen at once. To support that, vim gives us windows.

To split the screen into multiple windows in vim, switch to command mode and run :split or control-w followed by s. This will split the screen into two windows, each containing a copy of the file that was in the original screen. Running :split again will create a third pane, and so on.

If you want to open the new pane with a different file, run :split <filename>. This will open a new window with the other file loaded inside. To open a window with a new, blank file, run :new or control-w n.

The active window can be resized with with the command :resize # or z#, where the # sign is the number of lines high that the window should be.

To move up and down among the visible windows use control-w k and control-w j respectively. To close the selected window, run :close or press control-w c.

There are actually many commands to manipulate buffers and windows in vim. These are the basics and will get you through most common tasks. To learn more, go to vim’s command mode and run :help window.

Be seeing you.

So many files, so little time

Editing multiple documents in vim: Using vim buffers

When editing a file in vim it is stored and displayed in a buffer, something like a tab in a web browser window. You can open multiple files at once and easily move back and forth among the buffers to work with each file.

Opening multiple files in vim

To open multiple files in vim when you start your session, simply list them on the command line:
$ vim myfile1 myfile2 myfile3 myfile4
This will start vim with four buffers pre-loaded with your four files. You can use wildcards:
$ vim documents/myfile*

If you already have vim running and want to open another file, go to command mode and type:
:e documents/myfile5

You’ll be happy to know that you can use tab completion with the :e command. If you want to create a new file within vim, from command mode run :enew which will open an empty buffer. Just remember to save the file with a name anytime before you exit:
:sav documents/myfile6

To see the buffers open in your current session, from command mode run :buffers. Note that each buffer lists the file that it contains, and each has a unique number. To move to a different buffer, run :b# where the # sign represents the buffer number you want to move to. For example, to begin editing the file in buffer number 3, run:
:b3

Keep in mind that before you can switch to a new buffer you must save or discard all the changes made to the current one. Use the :w command to save your file before changing buffers.

You can cut, copy and paste among buffers just as you would within a single file. Just remember that if you make a cut you will need to save the changes in the current file before switching to the target buffer.

When you no longer want to work on a file you can close its buffer with the “buffer delete” command, :bd. This command closes the file in the current buffer and deletes the buffer; the file is not removed. By adding a number you can delete a specific buffer. For example:
:bd3
This will close the file in buffer 3 and delete the buffer. And quitting vim will close all files and delete all buffers.

Be seeing you.