Wikia

Vim Tips Wiki

Tabulated-ish data entry

Talk0
1,599pages on
this wiki
Revision as of 08:00, January 12, 2013 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Recently created tip

We have not yet decided whether to keep this tip as its own page or merge it somewhere else. If you have a suggestion on the tip content, please edit this page or add your comments below (do not use the discussion page).

Please discuss whether to keep this as a new tip, or whether to merge it into an existing tip, on the new tips discussion page.
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.

CommentsEdit

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 | Your ad here

Photos

Add a Photo
103photos on this wiki
See all photos >

Recent Wiki Activity

See more >

Around Wikia's network

Random Wiki