Wikia

Vim Tips Wiki

Tabulated-ish data entry

Talk0
1,613pages on
this wiki
Revision as of 08:46, September 27, 2012 by Marcin Szamotulski (Talk | contribs)

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.

Comment

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 hit <Esc>.

Around Wikia's network

Random Wiki