Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Vimorama 2000

[Dec. 20, 2000] Some VI-related stories from LinuxToday

[Aug 19, 2000] Introduction to Programming in C/C++  with Vim  Written By: Kmj

... ... ...

Ctags is a program that comes with vim. Basically, it's purpose is to help a programmer get to various parts of his program with relative ease. The typical method of running ctags is by simply typing the following in your source directory:

[/home/someuser/src]$ ctags *

This will create a 'tags' file in you current directory with the following information for all C or C++ files in you directory. This file contains a listing of the following objects:

Vim then uses this file to help you locate these tagged items. This can be done in a few ways. First, one can open vim to the location of a tagged object. To do this, run vim with the '-t' flag and the tag-name as an argument. For example:

[/home/someuser/src]$ vi  -t  foo_bar

would open to the file containing the definition of foo_bar at that line.

If you are already in vi, you can enter the command by the tag-name:

:ta foo_bar

This may take you out of the file you are currently in, unless the 'autowrite' option is enabled. (It  is off by default.) For more information on autowrite, type ':h autowrite' from with vi command-mode.

The final way to jump to a tagged location is by typing 'Ctrl-]'  in command mode while the cursor is on a specific word. For example, if you are  looking at my program, and you come across a point where I call foo_bar(), you can type 'Ctrl-]', while the cursor is somewhere on that word, and it will jump to that definition. Note that 'Ctrl-]' is the escape character for telnet, so this may cause some issues if your editing files remotely. Type ':h^]' for more information.

Ctags can also be used with other languages (java, fortran, ...and more) and editors (emacs, NEdit, ...and more). When set up properly, this tool can make  your job tremendously easier, especially when you have to jump into a large ongoing project head-first.

For more information: View the man page, man ctags, or view the vim help, :h ctags.

c-style indenting

Vi has various methods of implementing auto-indenting. The best for C and C++ programmers is, obviously, cindent mode. This is a very versatile tool which gives the programmer much control over the look and feel of his source code, without any effort (except the effort of initial setup, of course). To enable c-indenting, just type ':set cindent' from the command mode.  The most important thing to note is that cindenting makes use of shiftwidth, not tabstops. The default shiftwidth is 8. In order to change this,  enter ':set shiftwidth=x' where x is the desired number of spaces to shift.

The default cindent options tend to be nice, but if you find your program indenting in some way that is annoying, you can modify the behaviour. To set the cindent options, type ':set cino=<string>', where <string> is a list defining exactly how you want your cindent options to behave. There are quite a few different types of indenting which can be done, and vim's help does a good job explaining them, so I won't go over them here. To view the help for the possible values of cinoptions, type ':h cinoptions-values'. To view the current values, simply type ':set cino'. Most likely there will be none, since everything starts at a default value.

For more information, check out the following: ':h shiftwidth', ':h cindent', ':h cinoptions', ':h cinoptions-values', ':h cinkeys', and ':h cinwords'.

syntax highlighting

Programmers who are used to integrated development environments know the beauty of syntax highlighting. It doesn't just make your code more readable; it also helps prevent annoying commenting and string errors. Vim has syntax highlighting for a number of languages, including C and C++ of course. To enable it, type ':syntax on'. Using it is as simple as that if you're happy with the default values.  Vim's syntax highlighting tools can be quite complex, with a number of different things to play around with. To view information on syntax hilighting, type ':h syntax', which will lead you to vim's extensive help system. Syntax hilighting with color terminals and with gvim is nice, but if you don't have color, vim uses underlining, boldface, etc. To me, this is pretty ugly.

For more information:  ':h syntax', ':h syn-qstart', ':h syntax-printing'

edit-compile-edit, a.k.a. Quickfix

This is a really nifty feature.  Basically, by typing one command, you can cause vim to attempt to make the program you're working on, then open to whatever file first compiler error is in at the line of that error. The command to execute is ':mak' (or ':make'). Vim will run whatever program is denoted by the value of 'makeprg'. The default value for 'makeprg' is 'make'. You can change this, if you wish, by typing ':set makprg=<string>', where <string> denotes the desired command. Vim uses the 'errorformat' value to figure out how to understand the output from the compiler. Since different compilers have different output format's, you'll probably have to enter the format string. The method used is rather similar to C-style formatting with scanf. The most important format specifiers are %f, meaning filename, %l, meaing line-number, and %m, meaning message.

GCC's format string: %f:%l:\%m

This can become quite complex with different compilers, but fortunately, vim has a world of information in their help at ':h errorformat'.

For more information, check out: ':h quickfix', ':h mak', ':h makeprg',  ':h errorfile', ':h errorformat'.

useful keystrokes

There are certain command-mode keystrokes that are especially useful for programmers. Below is a small subset of these:

Moving around within functions:
    [ [   = Go to previous first-column '{';   equivalent to ?^{
    ] ]   = Go to next first-column '{';   equivalent to /^{
    [ ]   = Go to previous first-column '}';   equivalent to ?^}
    ] ]   = Go to next first-column '}';   equivalent to /^}

    {   = Go to previous blank line.
   }   = Go to next blank line.

    gd   = Go to definition of current local variable  (current = cursor is on it)
    *   = Go to next instance of current word
    #   = Go to previous instance of current word
    ''   = Go to location where last search was started.

Parenthesis Matching:
%   Takes you to the matching parenthesis, curly brace, or bracket, depending on what you are on. This always comes in handy as a quick double-check.

Substution:
Vim has powerful substution capabilities,  with a very simple interface.  No annoying GUI to get in the way (though you may need to keep a cheat-sheet handy). To search for and replace text, use the following command:
:  [address] s/<pattern>/<string>/[g|c|N] (where N is an integer value).

This finds one (or more) instance of the grep-style regular expression represented by <pattern>, and substitutes it with <string>.  'address', 'g', and 'N' are modifiers which determine which and how many occurrences of <pattern> are replaced.
g = Global:    Replace all occurrences of <pattern> on the line.
c = Cond.      Ask before making each replacement.
N = Nth    Replace only the Nth occurrence of <pattern> on the line.
(No modifier implies N=1, the first occurrence on that line)

[address values]    -May be one specifier or two separated by a comma.  (below,  x represents an integer).
. = The current line
$ = The last line in the file
% = Entire file
x = The xth line of the file
+x = x lines after the current line
-x = x lines before the current line

The comma may separate any of the above in order to specify a range. All lines within the given range will undergo the substitution. The best reference I have found for  substituting can be found at the Vi Helpfile, linked below.

Miscellany

Vim has so many nifty little things, it would be impossible to list them all. Here are a few more things that are worth taking a look at.


Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: February 28, 2008