Skip to content

Instantly share code, notes, and snippets.

@nicalpi
Created July 24, 2013 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicalpi/6070841 to your computer and use it in GitHub Desktop.
Save nicalpi/6070841 to your computer and use it in GitHub Desktop.
Is it possible to VimGolf this?
# To help me importing some CSV
# Using vim (to avoid eval hell)
# can we 'easily' move from something like
legacy_venue_id,venue_name,legacy_region_id,venue_address,venue_oot,venue_db,venue_notes
# To something like
legacy_venue_id = row[0]
venue_name = row[1]
legacy_region_id = row[2]
venue_address = row[3]
venue_oot = row[4]
venue_db = row[5]
venue_notes = row[6]
@nelstrom
Copy link

How about this?

:s/,/\r/g                  -  replace commas with newlines
<C-o>                      -  jump back to top of block
:let i=0                   -  create a variable `i`
qw                         -  start recording a macro
A{ = row[<C-r>=i<Enter>]}  -  append ` = row[i]` at end of line, using actual value of variable `i`
:let i=i+1                 -  increment variable `i`
q                          -  stop recording the macro
jV}                        -  move down one line, then select to end of block
:normal @w                 -  execute the macro on each selected line

There's a similar example in Practical Vim, tip 70, Evaluate an Iterator to Number Items in a List.

@nelstrom
Copy link

I'm sure that if you posted this example to http://vimgolf.com/ you'd get some solutions that required fewer keystrokes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment