 3 spaces:
Another application of ctrl-v is the commenting out/in of lines of code. During the debugging of a program, a programmer will often temporarily "comment out" some lines of code. We can expedite this using ctrl-v, say to comment-out the innermost if-then-else within the while loop in Insert(). First, define region consisting of the first character in each line:
(Don't hit the Esc key yet.)
Now hit I. This puts you into vim's Insert mode, at the top of the highlighted region. Now enter // to comment-out the first line:
Now hit Esc to end Insert mode as usual -- and look, all the lines are commented out!
Later, we will want to re-enable those lines. Again, set up a region using ctrl-v, this time 2 columns wide:
Now hit d, to delete the comment symbols:
First, there is auto-indent mode, which you can set using the vim command
In this mode, whenever you hit Enter to start typing on the next line, vim automatically will indent to the same amount as the previous line.
For example, note the position of the black cursor at the end of the 7th line of code in this screen:
After hitting Enter, the screen now looks like this:
This saved us the trouble of holding down the Space bar for 12 spaces. We can start typing the new line right away, already properly indented.
If we want to ignore this indenting on this line, we just hit the Esc key. If we want to leave auto-indent mode entirely, we type:set noai.
Another powerful auto-indenting comment in vim is the = command. We will not discuss it here, though.
In vim, type
The makefile will be executed, and if there are any errors in syntax, etc. reported by the compiler, vim will relay them to you in the same window as the source code.
For instance, suppose we had forgotten to end the line
X[I] = atoi(AV[I+1])
in GetArgs() with a semicolon. After typing :make we would see the following screen:
When we then do hit the Enter key as requested, vim will move the cursor right to the location of the first error (black cursor on the 6th line of the screen):
Suppose you are typing some program source code into a file, and you have a long variable name, say OverallSalesTotal. Instead of typing that by hand every time, you can save yourself typing by setting up an abbreviation and telling your about it. In vi clones such as vim, for instance, you can do something like
:abb ost OverallSalesTotal
and from then on, whenever you type "ost" (followed by a space), the editor will automatically expand this to "OverallSalesTotal" for you. This not only saves time, but also again allows you to concentrate better on your programming, instead of being distracted.
Another way to do this is to use vim's word completion capability. Suppose I wish to type OverallSalesTotal as above, but I feel it would be too difficult to remember an abbreviation for each of the many variable and function names I have in my program. What I can do instead is use word completion:
I simply type enough of the word to make it unique. It may be, say, that this is OverallSalesTotal is the only word in my file which begins with "Ov". If so, I can type
where here ^n" means ctrl-n. This tells vim to complete "Ov" to the next word in the file which begins with "Ov", which will be OverallSalesTotal.
Of course, word completion is handy even when editing text files, say when composing e-mail. All of the editors mentioned at the outset of this document -- vim, emacs and jEdit -- have this feature. In emacs, for example, it is invoked as C-M / (this is emacs notation for first hitting and releasing the Ctrl key, then the Esc key, then /).
A common problem in editing program files is that we don't match parentheses or braces properly. Suppose for example in Lnk.c we had forgotten to put in the closing brace in the last else clause within Insert():
Upon running :make, we find:
Hmm...gcc thinks that at what we intended as the definition of ProcessData(), Insert()'s definition was not finished yet. So, let's move the cursor to the brace which will had intended to close Insert():
Now, let's use vim's % command to see which left brace this r