Vim for C Programmers

You don't have to move to an integrated development environment to get luxury coding features. From variable autocompletions all the way up to integration with ctags and make, Vim makes a C programmer's life easier and more productive.
Vim and Syntax Highlighting

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.

Vim and Variable Name Completion

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 and Source Code Formatting

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.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Please Help

neo_nuggets's picture

Hello sirs,
I have Vim and Code Blocks and am very much new to Vim and would like to know how does one build and run a script? (Yes...I am a newbie).

Nice article, concise and

Anonymous's picture

Nice article, concise and useful.

Seems like the author was trying to educate newcomers without trying to impress readers with unix-speak. I hope I can find more of his articles.

(windows programmer).

Try c.vimYou can get this

Shankar's picture

Try c.vim
You can get this from vim.org
An amazing plugin for working with C files in gvim.

Editing between marks

Gary T's picture

Excellent article, here's another little tip for marks and replacing.

Mark two points in the file with say ma and mb, you can then replace between the marks with

:'a,'bs/foo/bar/

or a single mark and then you can do current position to the mark with

:.,'as/foo/bar/

I find this incredibly useful, hopefully you will to.

Adding a small note

jagadeeshbp's picture

The article was quite marvellous and useful.

I just wanted to add, for those who are new to vim, that most of the settings like "se nu" can be made to persist across logins by just putting that setting in .vimrc file in the user's home directory.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState