|
|
|
|
Splits and multi-file editingPerhaps 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 splitAssuming 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:
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 splitsOnce 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.
Saving and quittingEach 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 mappingsI 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. |