Vim Tips Wiki
(Use s normal command rather cl (it is shorter).)
(One can also use "tY)
Line 12: Line 12:
 
{name:"#", price:#, leftover:#},
 
{name:"#", price:#, leftover:#},
 
</pre>
 
</pre>
in t register (<code>"tyy</code>). <code>#</code> is a marker location for data fields. Any other unused symbol can be used instead.
+
in t register (<code>"tyy</code> or <code>"tY</code>). <code>#</code> is a marker location for data fields. Any other unused symbol can be used instead.
 
Then make the following mappings:
 
Then make the following mappings:
 
<pre>
 
<pre>

Revision as of 09:38, 22 September 2012

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 better way,

First, place

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

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

:imap <tab> <esc>f#s
:imap <enter> <esc>"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.