That's Vimprovement! A Better vi

Be a better editor—try Vim.
Advanced Tagging

When you jump to a tag using the CTRL-] command, the entire screen jumps. If you use the command CTRL-W CTRL-], the current window is split and you do a tag jump in the new window.

Multiple Tags

Suppose you have five or six small programs in a directory, and you want to jump to the definition of one of the main functions. If you do a:

:tag main

command, the editor will jump to the first definition of main that it can find. This may or may not be the one you want.

The :tselect command displays all the tags that match a given name and lets you select the one you want. For example:

:tselect main
# pri kind tag               file
> 1 F C f    main              a_test.cpp
    int main( int argc, char* argv[] )
2 F   f    main              acp.cpp
    int main( int argc, char* argv[] )
3 F   f    main              add.cpp
    int main(int argc, char* argv[])
Enter nr of choice (<CR> to abort): 3

What if you only know part of a name? In other words, you want to go to the function whose name is “something- process-something-data”. The :tags command can take a regular expression. In this case the command:

:tag /process.*data
goes to the first tag matching this expression. If several functions match the expression, you can use :tselect to go to the right one.

Word Wrapping

Programs contain code and comments. Code has its own structure. You do not want an editor that line wraps when you write code. But comments are merely text and it's okay to wrap text. In fact in most cases, it is highly desirable to have an editor that wraps long text lines.

The Vim editor understands the difference between commands and code. It can be configured to wrap comments and leave the code alone. This is done by putting the following lines in our .vimrc file:

:autocmd FileType c,cpp
\     set formatoptions=croql cindent
\     comments=sr:/*,mb:*,ex:*/,://

(Note: continuation lines in *begin* with \.)

The :autocmd command tells Vim that these commands are to be executed whenever it determines that it is working on a C or C++ file. (File names ends in .c or .cpp.)

The formatoptions tells Vim that we want to automatically wrap comments but don't want code wrapped. The next line:

set comments=sr:/*,mb:*,ex:*/,://

tells Vim what a comment looks like. In this case we've defined the standard C comment (/*, */) and the C++ comment (//). We've also told Vim that if we are in the middle of a C-style comment, to please begin each line with a “*”.

With these options set, when you type /*<RETURN>, Vim automatically puts in a “ *” to begin the next line. Given the limited space allowed in this article, all I can really tell you is that this is a really neat feature, and you should play around with it. If you need more details, you can check out the on-line help text.


Vim is a very versatile editor. In this article we've touched on a few of the more interesting features of this editor. But we only scratched the surface. There are still hundreds of commands and options that we've not discussed. You can find out about these by reading the help text and other documentation that comes with the editor.

By introducing you to some of the new features in Vim we've given you a start toward more effective editing. How far you go from here is up to you.


Steve Oualline is the author of Practical C Programming, Practical C++ Programming and Vim (Vi Improved). He wrote his first program when he was 11 years old and has been working in the software industry since then. He currently resides in San Diego where he spends his days working at Nokia Mobile Phones and his weekends working as a real engineer on a small tourist railroad in Poway, California.


White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState