Vim Tips Wiki
Register
Advertisement

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created September 21, 2012 · complexity basic · version 7.0

Sometimes I need to enter manually many lines with identical structure and a few differing data fields. Here is what I mean.

{name:"apple", price:50, leftover:1},
{name:"pear", price:10, leftover:0},
{name:"plum", price:11, leftover:0},
{name:"none", price:0, leftover:10000},

Method of choice for dealing with this was copying sample line several times and then modifying variable fields by navigating to them with cursor keys or mouse, which feels like it takes surprisingly lot of time and effort. I found a better way,

First, put the line

{name:"#", price:#, leftover:#},

in the t register ("tY). # is a marker location for data fields. Any other unused symbol can be used instead. Then make the following mappings:

:imap <Tab> <C-O>f#s
:imap <Enter> <C-O>"tpf#s

To start entry, type O and then enter. This will create the template line and put cursor on first data field in insert mode. Enter data and without leaving insert mode hit tab. This will bring cursor to the next field. When all fields are done, without leaving insert mode hit enter. This will create new template line. Continue entry.

Do not forget

:iunmap <enter>

when you are done. You may also want to unmap Tab, but I never use it anyway.

Comments[]

One can also use the normal o operator to input several lines of the same shape. For example to put 15 lines:

{name:"#", price:#, leftover:#},

Hit 15o in normal mode, type the above line and press Esc. You can then record what you have types for later usage with let @t=@., this will copy the ". register to the named register "t.

Advertisement