7 Commands for Programmers
- Syntax coloring
- Automatic indentation
- Indentation commands
- Commands to navigate through the source code
- Getting information through the man command
- The use of tags to go up and down a call stack
- Making programs with the :make command
- File searching with :grep
-
Syntax Coloring
The following command turns on syntax coloring.:syntax on
You can customize the colors used for syntax highlighting as well as the highlight- ing method itself. -
Syntax Coloring Problems
Most of the time syntax coloring works just fine. But sometimes it can be a little tricky to set up.The following sections take a look at some common problems and solutions. -
Colors Look Bad When I Use Vim (UNIX only)
Ever try and read light yellow text on a white background? It is very hard to do. If you see this on your screen, you have a problem.The Vim editor has two sets of syntax colors. One is used when the background is light, and the other when the background is dark.
When Vim starts, it tries to guess whether your terminal has a light or dark back- ground and sets the option ‘background’ to light or dark. It then decides which set
of colors to use based on this option. Be aware, however, that the editor can guess wrong.
To find out the value of the 'background' option, use the following command::set background?
If Vim’s guess is not correct, you can change it using a command such as this::set background=light
You must use this command before executing the command::syntax on
-
I’m Editing a C File with a Non-Standard Extension. How Do I Tell Vim About It?
The answer is to use the option 'filetype'.This tells Vim which type of syntax highlighting to use.With a C file, you use the following command::set filetype=c
If you want to make this setting automatically, look in the help files with this
command::help new-filetype
-
Running the Color Test
If you are still having trouble with colors, run the Vim color test.This is a short Vim program that displays all the colors on the screen so that you can verify the correctness of the Vim colors.
The color test can be started with these two commands::edit $VIMRUNTIME/syntax/colortest.vim
:source %
-
Shift Commands
The Vim editor has lots of commands that help the programmer indent his program correctly.The first ones discussed here merely shift the text to the left (<<) or the right (>>).
By default, shift width is 8.To change the size of the shift width, use the following command::set shiftwidth=4
-
Automatic Indentation
The Vim editor has a variety of automatic indentation options.The major indentation modes are the following: -
C Indentation
The Vim editor knows something about how C, C++, Java, and other structured lan- guage programs should be indented and can do a pretty good job of indenting things properly for you.To enable C-style indentation, just execute the following command::set cindent
With this option enabled, when you type something such as if (x), the next line will automatically be indented an additional level.
When you type something in curly braces ({}), the text will be indented at the start
and unindented at the end.
You can customize the indenta- tion style through the use of several options.
You don’t want to switch on the 'cindent' option manually every time you edit a C file.This is how you make it work automatically: Put the following lines in your .vimrc (UNIX) or _vimrc (Windows) file::filetype on
:autocmd FileType c,cpp :set cindent
The first command (:filetype on) turns on Vim’s file type detection logic.The sec- ond, performs the command :set cindent if the file type detected is c or cpp. (This includes C, C++, and header files.) -
Smartindent
The 'cindent' mode is not the only indent mode available to Vim users.There is also the 'smartindent' mode. In this mode, an extra level of indentation is added for each
{ and removed for each }. An extra level of indentation will also be added for any of
the words in the cinwords option. Lines that begin with “#” are treated specially. If a line starts with “#”, all indentation is removed.This is done so that preprocesser directives will all start in column 1.The indentation is restored for the next line. 'smartindent' is not as smart as 'cindent', but smarter than 'autoindent'. -
Autoindent
Structured languages such as a Pascal, Perl, and Python use indentation to help the pro- grammer figure out what the program is doing.When writing these programs, most of the time you want the next line indented at the same level as the preceding one.
To help you do this, the Vim editor has an 'autoindent' option.When on, it causes lines to be automatically indented.
While in insert mode, the CTRL-D command will cause Vim to back up one shift width (see Figure 7.4). CTRL-D moves the } back one shift width. -
Jumping to a Variable Definition (gd, gD)
The gd command searches for the local declaration of the variable under the cursor.
The gD command searches for the global definition of the variable under the
cursor. -
Jump to Macro Definition ([CTRL-D, ]CTRL-D)
The [CTRL-D command searches for the first definition of the macro whose name is under the cursor.The ]CTRL-D command searches for the next definition of the macro. -
Displaying Macro Definitions ([d, ]d, [D, ]D)
The [d command displays the first definition of the macro whose name is under the cursor.The ]d command does the same thing only it starts looking from the current cursor position and finds the next definition.
The ]D and [D commands list all the definitions of a macro.The difference between
the two is that [D starts the list with the first definition, whereas ]D starts the list with the first definition after the cursor. -
Matching Pairs
The % command is designed to match pairs of (), {}, or []. Place the cursor on one, type % and you will jump to the other. -
Shifting a Block of Text Enclosed in {}
Suppose that you want to indent the text encoded in {} one level. Position the cursor on the first (or last) {.
Execute the command >%.
Unfortunately this shifts the {} in addition to the text. Suppose you just want to shift
what is in the {}.Then you need to do the following:
- Position the cursor on the first {.
- Execute the command >i{.
This shift right command (>) shifts the selected text to the right one shift width. In this case, the selection command that follows is i{, which is the “inner {} block” command.
- Indenting a Block Using Visual Mode
To indent a block using visual mode, follow these steps:
- Position the cursor on the left or right curly brace.
- Start visual mode with the v command.
- Select the inner {} block with the command i}.
- Indent the text with >.
-
Finding the man Pages
The K command runs a UNIX man command using the word under the cursor as a subject. If you position the cursor on the word open and press K, for example, the man page for open will display. -
Tags
The Vim editor can locate function definitions in C and C++ programs.This proves extremely useful when you are trying to understand a program.
The location of function definitions (called tags in Vim terminology) is stored in a table of contents file generated by the program ctags. (This program comes with Vim.). To generate the table of contents file, which is named tags, use the following com- mand:$ ctags *.c
The CTRL-] command jumps to the tag of the word that is under the cursor.This
makes it easy to explore a tangle of C code.
The CTRL-T command goes the preceding tag.This command takes a count argument that indicates how many tags to jump back. -
Help and Tags
The help system makes extensive use of tags.To execute a “hyperlink jump,” you press CTRL-] (jump to tag).You can return to a preceding subject with CTRL-T (jump to pre- ceding tag) and so on. -
Searching for a Given String
The :grep command acts much like :make. It runs the external program grep and cap- tures the output. (This command does not work all that well on Microsoft Windows
because it does not have a grep command.You can get one from the GNU people (see http://www.gnu.org).