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

Sat, 2008-07-26

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