Advanced vi
Several tasks are part of vi that don't fit in any other section. Most of these are quite advanced, such as running external commands, joining lines, and splitting windows. This section covers these in detail.
Running External Commands in vi
A frequent question on the exams is how to run an external command inside vi, such as seeing an ls l listing of the current directory so you can remember a filename:
:! ls l
In this, the command ls l executes, with the command's output displaying onscreen, and you need only press Enter or enter a command to return to the vi session. If the output scrolls more than one screen, it's piped to the more command and all the normal movement keystrokes will apply.
Joining Lines
It's quite irritating in vi to be at the front of a line and want to use the Backspace key to move that line to the end of the previous line. The Backspace key works only on the current line. If you need a line to be joined to the previous line, you can position the cursor in either line and press Shift+J to cause the second to be appended to the end of the first line.
Say you have a file named file1 that contains the following text:
This is line 1 This is a longer line 2 This is an even longer line 3
You want to join line 1 and line 2, so you position your cursor somewhere on line 1 and press the J key. The file then looks like the following:
This is line 1 This is a longer line 2 This is an even longer line 3
Putting the cursor on line 2 and pressing J joins line 2 and line 3 in a similar fashion.
Split Windows
Last, but not least, is splitting windows in vi, specifically the vim version of vi. When you're editing a particular file and want to see either another section of that same file or even another file altogether, you can use the following:
:splitThis splits the window horizontally, with the same file and lines shown in both windows.
:vsplitThis splits the window on the same file vertically, with the same document shown in both the left and right panes.
Moving between the panes is somewhat counterintuitive because you must press Ctrl+W twice to move between the windows.
To edit a completely different file, you should edit the first one in vi; then to split the screen horizontally with the other file loaded in the second pane, you can enter
:split file2
To set the height of the newly split window from the split, you could enter the following:
:10split /etc/fstab
This command splits the top 10 lines of the screen and displays the contents of the /etc/fstab file therein.
If you wanted to close the split window, you would focus on it by pressing Ctrl+W and then entering
:close
Better yet, after comparing something or getting a filename straight, you can close all the other split windows and just have the current window open by entering the following:
:only
NOTE
Many times I've opened a couple of split windows to see the difference between two files or used the diff command. The easiest way to compare two files and make edits to them is to use the vimdiff command, such as
:vimdiff file1 file2
This loads the two files into a vertically split vim session and uses color and other symbols to show you what is similar or different between the two files. This is useful for comparing a log file before and after a problem or two configuration files to see why one doesn't work.