Vim’s gf command to open include files in C source code

You can open the file whose name is under the cursor with gf, (goto file).

Recently, while browsing the linux source code, I wanted to do a gf on a file whose name appeared inside an include statement. For instance, if the line was:

#include <linux/can.h>

I wanted to open the can.h file by having the cursor on that line and hitting gf, but unfortunately it would just tell me:

E447: Can’t find file “linux/can.h” in path

From :help editing.txt

   If Vim's current path is /u/user_x/work/release and you do
	:set path=include;/u/user_x
   and then search for a file with |gf| the file is searched in:

So if you got your linux source in /path/to/linux/ you would do:
:set path=include;/path/to/linux/

Now if you hit gf again vim will correctly open the file /path/to/linux/include/linux/can.h


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: