That's Vimprovement! A Better vi

Be a better editor—try Vim.
Copy and Paste Using Visual Mode

Vim has one new major mode: visual mode. In visual mode, highlight the text you want to edit, then type an editing command. In this example, you need to copy the first line of the function even from the bottom window and paste it in the top window.

Start by sending the cursor to the bottom window (CTRL-Wj) and positioning it on the first line of the function even. Next, enter line visual mode by typing the V command. The current line is highlighted, which indicates that this line will be affected by any editing command you type. At this point you can use the cursor commands to move the cursor down a few lines. As you move the cursor, the text that you pass over is highlighted. Move the cursor up, and it's unhighlighted. Go past the starting point, and the text above will be highlighted.

Highlighting is done on a line-by-line basis. That's because you've entered visual line mode. If you had typed v, for character visual mode, you could highlight text on a character by character basis. Finally, there is block visual mode (CTRL-V) that highlights a rectangle on the screen. This is good if you are working with text tables because you can manipulate columns of figures on the screen.

Now, to copy a single line from the bottom window to the top one, use line visual mode (V) and highlight that line. Then use the yank y command to copy it into the unnamed register. Next go to the top window and do a p to put (paste) the text into the top window. All you need to do now is add a “;” to the end of the line and you have an even header. Figure 3 shows these commands in action.

Figure 3. Copying a Line

Compiling the Program

In order to compile our program we need a Makefile. (This exercise is left to the reader.)

Once our Makefile is in place we use Vim's integration with make to compile the program. All we need to do is issue the :make command from within Vim and the editor will run make and capture the output.

Our program contains an error:

even.c:4: parse error before `=='

Vim captures the output of the :make command and parses it. From this it can see that we have an error on line 4 of even.c, so it moves the cursor to this line and displays the error message at the bottom of the screen. (It will even switch files if it has to in order to locate the error.)

This makes it easy to fix the error. To see the next error, issue the :cn command. If you want to see the current error again, use the :cc command. The previous :cp. Finally, if you're done editing this file and want to see the first error in the next file use the :cnf command.

Vim is smart regarding the location of errors. When you add or delete lines at the start of the file, Vim adjusts the location of errors at the end of the file to reflect these changes. This avoids the problem you have with conventional editors like vi, where fixing an error at the start of a file throws off the line numbers for the rest of the file.

Finding out Where a Function Is Used

The grep command is great for finding out where a function is used and defined. Simply enter the command:

$ grep -n even *.c

and you get a printout of each line that contains the word even.

Vim has a :grep command that does the same thing. Actually this command is very similar to the :make command. It runs grep, captures the output and lets you navigate through the files using :cn, :cp, :cc, and :cnf.--just like :make.

Fixing Indentation

If you turn on the cindent option, Vim will indent your program correctly when you insert new text. But what about the text that's already there? That's where the = command comes in.

It will run a block of text through Vim's internal indentation program. (Actually, you can select which program is to be used for indentation through the equalprog option.)

Let's see how this can work to indent a basic C statement block. There are two ways of invoking the = command. The first is to use ={motion}. The second is to enter visual mode, select a block of text and then press =.

So, one way to indent a block of code is to go to the first “{” of the block. Now enter =%. The = tells Vim to indent the text from here to where the next command takes the cursor. The next command in this case is %, which tells Vim to go to the matching “}”.

If you wanted to do things using visual mode, you would position the cursor on the first “{”, then enter line visual mode with the V command. Next position the cursor on the corresponding “}” using any set of commands that get you there. Finally, the highlighted block is indented with the = command.