ViM tips for source code file editing
You can find lots of tutorials on ViM. This page contains a few tips that may help you when editing C source code files.
If you are looking for an IDE like environment with ViM, you can check Turning vim into an IDE through vim plugins.
First advice is to use :help word (colon included) for
searching through ViM's on-line help. So if you want to learn about how to
split your terminal into separate windows, use :help windows
In my experience, whatever I was looking for, I found in ViM. The only thing missing is running a shell in a terminal window, editing the output, and using it again as shell input -- that is what Emacs supports.
^X means Ctrl+X where X is small 'x'.
- always use 'smartindent'. Sometimes set by default. Put "set smartindent" to ~/.vimrc, or type ":set si" if in ViM. With this setting, ViM will greatly help with indenting. And yes, indenting by a tabelator is a good thing in C source code.
- Pressing K while for a word under the cursor will invoke a manual page for that word. Sometimes nK works to use a specific manual page section.
- [i within a word will search the current file upwards and also through all include files (recursively) for the word and displays the last line found with the word on the status line. Very often it is a definition or a declaration of such a variable, function, etc.
- [^I similar to [i but it will jump there. See below on how to jump back.
- gf will display a file under the cursor ("go file"). Also works for include files as ViM knows where such files are located by default. You can also help ViM with other directories that should be searched for files (not just for gf), use set pa=... in your .vimrc file.
- If you jump somewhere, including searching for a string via / or displaying another file via gf, ViM keeps a stack of such positions. You can go up and down the stack with ^O and ^I. So if you use [^I to jump somewhere, use ^O to jump back.
- In the insert mode, ^P will complete the current word with whatever it can find in the current file and include files (upwards). So, if you start with PTHREAD_M, you have included <pthread.h>, and press ^P, ViM will complete to PTHREAD_MUTEX_INITIALIZER. Press again, it will offer another matching word.
- % will jump to a matching parenthese
- * will search for a word under the cursor. # will search backwards. After the first search, you can use n to search for next occurences.
- >> indents a current line to the right by a shift width (a tabelator by default). Use set sw=N to change that to N spaces. Also works with <<.
- Visual blocks and indenting: make a block (V then move the cursor), then use > or < to move the whole block horizontally. Also try v a ^V. After an indentation you can use a period to repeat the last action. To indent back, use u for undo or < and >.
- [{ jumps to the beginning of a program block. Similarly ]}. Great if you want to jump to the begining of a cycle, function, etc.
Last changed: