Vim for C Programmers
If there is one feature in Vim for which it wins hands-down compared to any other editor or IDE, it is full-featured syntax highlighting. The colors available in Vim make it a veritable delight to work with source code. It not only makes your life colorful, it also makes it easy to spot errors ahead of compilation. Common errors such as a mismatched ),} or ] in the code are easy to see. It also reminds you if you have left a string hanging without the closing " or '. It tells you the comment doesn't end with */, or that you are nesting comments. Syntax highlighting is smart when it comes to C syntax.
Typically, you wouldn't have to do anything to enable Vim's syntax highlighting; :sy on does the job in case your distribution doesn't enable it by default. As with other commands, you can add this to your ~/.vimrc file.
If colors still don't show up, something is wrong with your terminal. Fix it first. :se filetype on is another thing you can try in addition to :sy enable.
Let us assume that you have colors displayed correctly. Say you don't like a certain color, or because blue is not visible in dark backgrounds, you can't read C comments. To solve the second problem, a simple :se background=dark does the job. If you want to disable syntax highlighting for C comments, type :highlight clear comment.
To change colors, first use the :syntax command to display all the syntax items for the given buffer. Then, identify the syntax group you want to change. If you want strings displayed in a bright white color, which is easy to read against a black background, simply enter:
:highlight String ctermfg=white
or, for gvim users, type:
:highlight String guifg=white
You also can change the syntax color of any group. Typical syntax groups are Statement, Label, Conditional, Repeat, Todo and Special. You can change the attributes of highlighting as well, such as underline and bold. For instance, if you want to display NOTE, FIXME, TODO and XXX with underlining, you can use:
:highlight Todo cterm=underline
or you can both add bold and change the color:
:highlight Repeat ctermfg=yellow cterm=bold
You can create your own set of highlight commands and save it in your ~/.vimrc file so that every time you edit your source code, your favorite colors are displayed.
In addition, Vim has a feature for variable name completion. While typing, simply press Ctrl-N or Ctrl-P in insert mode. Remember, this works only in insert mode. All other commands mentioned above work in command mode. You can cycle through possible completions by pressing Ctrl-N again.
This helps us avoid errors while typing, because structure members and function names often can be misspelled. This works best when Vim can use tags, so make sure a ctags file is in place.
Vim understands C well enough to be able to indent code automatically. The default indentation style uses tabs, which may not be appropriate for some people. In order to remove tabs completely from the source, enter:
:set expandtab :retab
which converts all tabs into spaces in such a way that the indentation is preserved. While typing C text, Vim automatically indents for you. This helps you figure out where you have your matching brace. You can match braces, ), ] and } with the % command in command mode. Simply take the cursor to a brace and press %, which takes you to the corresponding closing or opening brace. This works for comments as well as for #if, #ifdef and #endif.
After finishing typing the program, if you want to indent the whole file at one go, type gg=G in command mode. You then can remove tabs if you want by the above-mentioned method. gq is the command sequence for indenting comments. You can select a region and indent it too with the = operator.
If Vim's default tab indentation is painful to use, you can disable it by setting :se nocindent. Other indentation options are available. You can indent code between two braces and between certain line numbers. You can learn more by typing :help indent.txt.
- Ubuntu MATE, Not Just a Whim
- Canonical Ltd.'s Ubuntu Core
- Build Your Own Raspberry Pi Camera
- Nasdaq Selects Drupal 8
- Non-Linux FOSS: Screenshotting for Fun and Profit!
- Secure Desktops with Qubes: Compartmentalization
- Back from the Dead: Simple Bash for complex DdoS
- Tech Tip: Really Simple HTTP Server with Python
- The Peculiar Case of Email in the Cloud
- Netlist, Inc.'s HybriDIMM Storage Class Memory