That's Vimprovement! A Better vi
The Linux system comes with a vi clone called Vim. However, this editor can do more than just mimic vi. It has literally hundreds of additional functions, including commands for a help system, multiple windows, syntax coloring, program compilation and error corrections, advanced searching and many more.
By default, Vim starts in vi-compatibility mode. This means that many of the advanced features are turned off. To turn on these Vim improvements you need to create a $HOME/.vimrc file.
Listing 1 contains a sample .vimrc file. You can also create one by copying your .exrc file if you have one. A zero length file works as well.
There are two flavors of the Vim editor. The command vim starts the console version of the editor. In this version, you edit inside the terminal window in which you executed the command. The gvim command creates its own window. This second command is preferred because you get features, such as a command menu and toolbar, not found on the console version.
One of the most useful innovations that Vim has is the on-line, integrated help.
The :help command displays a help window. For specific help :help / displays the help text for the search (/) command (see Figure 1).
You can move through this text using the normal editing commands, such as h, j, k, l, <Page-Up>, <Page-Down>, etc.
As you scroll through the text, you'll see some lines that look like
/<CR> Search forward for the [count]'th latest used
pattern |last-pattern| with latest used |{offset}|.
The text enclosed in vertical bars (|) is a Vim hyperlink. Position the cursor over one of these items (say |last-pattern|) and press CTRL-]. This will make the screen jump to the given subject.
To go back to where you were before the jump, use the command CTRL-T.
To get out of the help system, use the normal Vim exit commands :q or ZZ.
To see how some of the new Vim commands can help you, start by creating a simple program file containing the following text:
#include "even.h"
int even(int value)
{
if (value & 1) == 1) // Deliberate mistake
return (1);
return (0);
}
The first thing to notice is the changing colors of the text. This is called syntax highlighting. Each component of a program (keyword, string, constant, preprocessor directive, etc.) gets a different color. In our initialization file, this was enabled by the :syntax on command. If you do a :syntax off, the highlighting disappears.
The next thing to note as you type in the file is that you don't have to do any indenting. All the lines are automatically indented for you. This is because of the cindent option being turned on by the magic lines:
:autocmd FileType c,cpp :set cindent
This applies to C and C++ files only. (Unfortunately a full discussion of autocommands is beyond the scope of this article. You can do a :help autocmd for full instructions.)
Note: The actual commands in the sample .vimrc file are a little more fancy than the ones presented in this section, but they do the same thing.
Now, let's have a little fun. Position the cursor on the “r” of one of the return statements and type xxxxxx. The return disappears. Now type “u” to undo the last change. The “n” returns.
The old vi editor had only one level of undo. Vim has many. Type “u” again, and notice that you've got “rn” back. Type “u” four more times and the whole word returns.
So how do you undo an undo? Through the new “redo” command:' CTRL-r. By typing this a couple of times, you redo the delete, which causes parts of “return” to disappear.
Now that you have a C file, you should create a header. It would be nice to be able to copy and paste the prototype from the C file into the header. With vi you couldn't do this. With Vim, it's easy.
First, bring up both files in the editor. The command :split even.h splits the current window in two. The top-half gets the file even.h, and the bottom even.c (see Figure 2).
To move the cursor from the top window to the bottom window use the command CTRL-Wj. To move up a window, use the command CTRL-Wk.
To close a window, use the ZZ or :quit command.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Parallel Programming with NVIDIA CUDA
- Readers' Choice Awards 2011
- 100% disappointed with the decision to go all digital.
- Linux-Based X Terminals with XDMCP
- Validate an E-Mail Address with PHP, the Right Way
- You Need A Budget
- The Linux powered LAN Gaming House
- Why Python?
- Python for Android








4 hours 25 min ago
5 hours 45 min ago
8 hours 29 min ago
12 hours 59 min ago
18 hours 6 min ago
19 hours 6 min ago
1 day 4 hours ago
1 day 4 hours ago
1 day 10 hours ago
1 day 14 hours ago