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
Solution:

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:
	/u/user_x/work/release/include
	/u/user_x/work/include
	/u/user_x/include

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

Advertisements

vim tip: expression register + autoincrement + recording ….

Tue, 2008-07-01

Repost from my tumblelog:

I had a line like this:
    PARTITION p_op1 VALUES LESS THAN 150,
and I wanted to generate this:
    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op3 VALUES LESS THAN 450,
    PARTITION p_op4 VALUES LESS THAN 600,
    PARTITION p_op5 VALUES LESS THAN 750,
    PARTITION p_op6 VALUES LESS THAN 900,
    PARTITION p_op7 VALUES LESS THAN 1050,
    PARTITION p_op8 VALUES LESS THAN 1200,
    PARTITION p_op9 VALUES LESS THAN 1350,
    PARTITION p_op10 VALUES LESS THAN 1500,
    PARTITION p_op11 VALUES LESS THAN 1650,
    PARTITION p_op12 VALUES LESS THAN 1800,
    PARTITION p_op13 VALUES LESS THAN 1950,
    PARTITION p_op14 VALUES LESS THAN 2100,
    PARTITION p_op15 VALUES LESS THAN 2250,
    PARTITION p_op16 VALUES LESS THAN 2400,
    PARTITION p_op17 VALUES LESS THAN 2550,
    PARTITION p_op18 VALUES LESS THAN 2700,
    PARTITION p_op19 VALUES LESS THAN 2850,
    PARTITION p_op20 VALUES LESS THAN 3000,

How to do it the vim way:
Copy the line we originally had and multiply the value by 1, so that we have this:

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op1 VALUES LESS THAN 150 * 1,

place cursor at the beginning of the line (cursor position is highlighted): _

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op1 VALUES LESS THAN 150 * 1,

start recording into register a: qa
increment partition number: ctrl+a
Now we have:

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 1,

Move to the comma: f,

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 1,

Move one position left: h

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 1,

Increment the number 1 by 1: ctrl+a

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Copy & paste line: yyp

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Move back to the end of the previous line: k$

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Delete expression into register n: “nd3b

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN ,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Enter insert mode: i
Enter expression mode: ctrl+r =
Paste deleted expression: ctrl+r n
Insert result of expression: <Enter>

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Exit insert mode and move to the beginning of the next line: <esc>2_

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Stop recording: q
Repeat what has been recorded as many times as you want, for instance, 10 times: 10@a

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op3 VALUES LESS THAN 450,
    PARTITION p_op4 VALUES LESS THAN 600,
    PARTITION p_op5 VALUES LESS THAN 750,
    PARTITION p_op6 VALUES LESS THAN 900,
    PARTITION p_op7 VALUES LESS THAN 1050,
    PARTITION p_op8 VALUES LESS THAN 1200,
    PARTITION p_op9 VALUES LESS THAN 1350,
    PARTITION p_op10 VALUES LESS THAN 1500,
    PARTITION p_op11 VALUES LESS THAN 1650,
    PARTITION p_op12 VALUES LESS THAN 1800,
    PARTITION p_op12 VALUES LESS THAN 150 * 12,

Complicated? Maybe, but a lot more fun and much faster than doing it manually ;-)