Tabs in vim

Wed, 2009-01-21

You can open several files in tabs from the command line with vim, just type:

vim -p file1 file2 file3

And each file specified on the command line will be opened in a new tab.

To open a file in a new tab inside vim use :tabe file

To close a tab do :tabc

You can quickly switch between tabs typing gt or gT in normal mode. To switch to a particular tab prepend the tab number to gt, i.e. type #gt, where #gt is the tab number.


Avoid the need to escape parenthesis, brackets… in vim regexes

Sat, 2009-01-17

Vim has a so called “very magic” mode for regexes which allows you to use parenthesis, brackets, the alternative separator (i.e. ‘|’), pluses, etc. with their special meaning but without the need to escape those characters.

(see :help /\v)

Example:

Let’s say you have the following in your buffer:

12345aaa678
12345bbb678
12345aac678

If you execute

:%s/\d\{5\}\(\D\+\)\d\{3\}/\1/

you will get

aaa
bbb
aac

but it required a lot of backslash escaping in the regex. You can avoid the need to escape parenthesis, curly braces, pluses, etc. using vim’s “very magic” mode for regexes. The following would do exactly the same as the previous substitution command but with fewer escaping required:

:%s/\v\d{5}(\D+)\d{3}/\1/


Merging with vimdiff

Thu, 2008-09-11
Open the files with vimdiff:
$ vimdiff file1 file2

To jump to the next difference:
]c

To jump to the previous difference:
[c

If there are only two files you can copy differences with:
:diffget
and
:diffput

If you are doing a diff with vim between more than two files you also have to specify from which buffer to :diffget or from which buffer to :diffput
:diffget b#
where b# is the buffer number. In vertical window split, buffers are numbered from left to right starting at 1.


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


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 ;-)


Test post with vimblog.vim

Sun, 2008-06-29

This is just a test post using vimblog.vim.