Vim for C Programmers
Vim is an extremely powerful editor with a user interface based on Bill Joy's almost 30-year-old vi, but with many new features. The features that make Vim so versatile also sometimes makes it intimidating for beginners. This article attempts to level the learning curve with a specific focus on C programming.
A typical programmer's routine involves compiling and editing programs until the testing proves that the program correctly does the job it is supposed to do. Any mechanism that reduces the rigor of this cycle obviously makes any programmer's life easier. Vim does exactly that by integrating make with Vim in such a way that you don't have to leave the editor to compile and test the program. Running :make from inside of Vim does the job for you, provided a makefile is in the current directory.
You can change the directory from inside of Vim by running :cd. To verify where you are, use :pwd. In case you are using FreeBSD and want to invoke gmake instead of make from the command line, all you have to do is enter :set makeprg=gmake. Now say you want to give some parameters to make. If, for instance, you want to give CC=gcc296:
:set makeprg=gmake\ \CC=gcc296
does the job.
Now comes the job of inspecting the errors, jumping to the appropriate line number in the source file and fixing them. If you want to display the line numbers in the source file, :se nu turns on this option, and :se nonu disables line number display.
Once you compile, Vim automatically takes you to the first line that is causing the error. To go to the next error; use :cn to take you to the next line number causing the error. :cfirst and :clast take you to the first error and the last error, respectively. One you have fixed the errors, you can compile again. If you want to inspect the error list again, :clist displays it. Convenient, isn't it?
If you want to read some other source file, say foo.c, while fixing a particular error, simply type :e foo.c.
One shortcut provided by Vim to avoid typing too much to switch back to the previous file is to type :e # instead of typing the full path of the file. If you want to see all of the files you have opened in Vim at any point in time, you can use :ls or :buffers.
If you have a situation in which you have opened too many files and you want to close some of them, you can issue :ls. It should display something like this:
2 # "newcachain.c" line 5 3 %a "cachain.c" line 1
If you want to close newcachain.c, :bd 2 or :bd newcachain.c does the job.
While browsing C code, you may have situations in which you want to skip multiple functions fast. You can use the ]] key combination for that while in command mode. If you want to browse backward in the file, [[ can be used.
You also can use marks to bookmark certain cursor positions. You can use any lowercase alphabet character as a mark. For instance, say you want to mark line number 256 of the source and call it b. Simply go to that line, :256, and type mb in command mode. Vim never echos what you type in command mode but silently executes the commands for you.
If you want to go to the previous position, '' (two single-quotation marks) takes you there. Typing 'a takes you to mark a and so on.
Especially when editing Makefiles, you may want to figure out which of the white spaces are tabs. You can type :se list, and whatever is displayed as ^I in blue are tabs. Another way to do that is to use /\t. This highlights the tabs in yellow.
Global searches and replaces are common tasks for programmers, and Vim provides good support for both. Simply type / in command mode, and you are taken to the searched keyword. If you prefer incremental searches, à lá emacs, you can specify :se incsearch before you search. When you want to disable it, type :se nois.
Search and replace is a powerful tool in Vim. You can execute it only on a region that you selected using the v command, only between certain line numbers or only in rectangular regions selected by using Ctrl-V command.
Once you select your region or line number ranges, for example using :24,56 to select lines 24–56 (both inclusive), or just select your region and type : :<','> appears. Now type s/foo/bar to replace all occurrences of the string foo with bar.
But, this command replaces only one instance per line. If you want to do this for multiple occurrences per line, type s/foo/bar/g. If you want to replace only some occurrences, you can use the “confirm” option with s/foo/bag/gc.
Sometimes the string contains characters that appear as a substring of other keywords. For instance, say you want to replace the variable “in” and not the “in” in inta. To search for whole words, type /\<in\>/.
Most commonly, you will want to do a global replace, which is every instance in a given file. You can do that by using either :1,$s/foo/bar/g or :%s/foo/bar/g. If you then want to replace this in all the files you have open, you can enter :bufdo %s/foo/bar/g.
Another way of searching is by going to the keyword and typing * in command mode. The keyboard now will be highlighted wherever it occurs in the file. Searching backward is simple too; type ? instead of / while searching.
Once the searching is over, Vim remembers it, so the next time you search for the same keyword, you have to type only / or ?, instead of typing the whole text.
One side effect of searching is that it stays highlighted. This can be a distraction while editing programs. Turn highlighting off by typing :se nohlsearch, :nohlsearch or :nohl
You always can use the Tab key to complete Vim commands you give with a colon. For instance, you can type :nohl<Tab>, and Vim completes it for you. This is applicable generically, and you can press Tab to cycle through Vim's commands until Vim finds a unique match.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Reply to comment | Linux Journal
5 hours 51 min ago - Reply to comment | Linux Journal
6 hours 7 min ago - Favorite (and easily brute-forced) pw's
7 hours 58 min ago - Have you tried Boxen? It's a
13 hours 50 min ago - seo services in india
18 hours 22 min ago - For KDE install kio-mtp
18 hours 22 min ago - Evernote is much more...
20 hours 22 min ago - Reply to comment | Linux Journal
1 day 5 hours ago - Dynamic DNS
1 day 5 hours ago - Reply to comment | Linux Journal
1 day 6 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Please Help
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
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
Try c.vim
You can get this from vim.org
An amazing plugin for working with C files in gvim.
Editing between marks
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
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.