Vim for C Programmers
Exuberant ctags (see the on-line Resources) is an external program that can generate tags for Vim to navigate source code. If all of your source code is contained in only one directory, simply go to the directory in the shell and enter:
$ ctags .
This generates a tags file called tags. Vim reads this file for jumping to functions, enums, #defines and other C constructs.
If the source code is distributed across several directories, ctags has to generate tags for all of them relative to a certain directory. To do this, go to the root directory of the source code and execute:
$ ctags -R .
Check whether the tags file has been generated. You also can open and read the tags file in Vim.
Now, let us move on to navigating the source code using tags. Navigating the source code using ctags is one of the most fascinating tools that a programmer has. You can read the code so nicely and quickly that you wonder how it would have been without ctags.
Once the tags file has been generated, open the file in Vim as normal, except that if the file is deep inside, open it from the root directory. For instance, your source code is organized like this:
common
|
----> gui --> wxpython
| |
| ------>Tk
|
----> backend --> networking
include
user
If you want to edit tcp.c under the common/backend/networking directory, you should open it like this:
$ vim common/backend/networking/tcp.c
and:
$ vim tcp.c
The tags file is situated in the directory above common, and Vim automatically knows the location of the tags file this way.
Alternatively, you can open the file using the second method mentioned above and execute this from inside of Vim:
:se tags=../../../tags
The first method is easier for navigation. Once you open the file, you can jump from one function definition to another easily by using the key combination Ctrl-].
If you want to go to the definition of anything, be it a function, macro or anything else, simply press Ctrl-] when the cursor is positioned on it. Thus, from invocation, we can move to the definition. It takes you there no matter which file contains it. Assuming that we call drawscreen() from tcp.c, it automatically takes you there, even if the file is contained under common/gui.
If you want to go back to what you were reading, press Ctrl-T, and you return to where you left. You can jump to another invocation from there by pressing Ctrl-] again. You can continue this process ad infinitum, and you can keep coming back by pressing Ctrl-T.
Another way to find a function definition if you know only a part of the name is:
:ta /function
This command takes you to the first match if there are multiple matches. You can go to the next match with :tn.
If there are multiple definitions and you want to choose among them, you can press G Ctrl-] or type :tselect <tagname>. This way you can modify the source code by navigating with tags without even knowing which file contains what.
Cscope is another powerful source code navigation tool with which we can perform a variety of searches. Here is a sample output of the Cscope menu:
Find this C symbol: Find this global definition: Find functions called by this function: Find functions calling this function: Find this text string: Change this text string: Find this egrep pattern: Find this file: Find files #including this file:
Now, Vim has integrated Cscope into its repertoire, making it convenient for programmers to use the same features in Cscope from the cool comfort of Vim. All you have to do is establish a Cscope connection by issuing :cs add cscope.out.
As we discussed before with ctags, Cscope generates an index called cscope.out that can be generated by using the shell command:
$ cscope -Rbq
This generates the file cscope.out. It is to be executed from the source code root directory à lá Ctags. You then open the file as before, relative to the source code root directory, and make a Cscope connection with the command :cs add cscope.out. You can verify existing Cscope connections by typing :cs show.
. What you can search for from inside of Vim can be seen using :cs<CR>. For instance, to go to a particular file, or a header of a source file, simply type :cs f f stdio.h for opening stdio.h or :cs f f foo.c.
For searching for functions called by a function foo.c, type :cs f d foo.c. This lists out the functions called by foo.c. For functions calling foo.c, type :cs f c foo.c.
To search for an egrep pattern, type :cs f e varName and so on. For a list of the available options, type :cs. It displays a range of available options.
Now, if you have both ctags and Cscope, you can type :cstag /foo to search for a function or enum or whatever that contains foo.
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 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Validate an E-Mail Address with PHP, the Right Way
- Build a Skype Server for Your Home Phone System
- Tech Tip: Really Simple HTTP Server with Python
- Why Python?
- A Topic for Discussion - Open Source Feature-Richness?
- Not free anymore
3 hours 44 min ago - Great
7 hours 31 min ago - Reply to comment | Linux Journal
7 hours 39 min ago - Understanding the Linux Kernel
9 hours 54 min ago - General
12 hours 23 min ago - Kernel Problem
22 hours 26 min ago - BASH script to log IPs on public web server
1 day 2 hours ago - DynDNS
1 day 6 hours ago - Reply to comment | Linux Journal
1 day 7 hours ago - All the articles you talked
1 day 9 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!
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.