views:

223

answers:

6

I frequently must correct the following rails code:

assert_equal value, expected

The two arguments to assert_equal are out of order, and should read:

assert_equal expected, value

In vim, what is the most efficient way of going from the first line to the second?

A: 

Hum... I would say "tdwxx$i, ^["tp but that's not really efficient or easy, just quick enough to type...

Varkhan
+1  A: 

Map a key combination to perform the command:

:s/^assert_equal \(.*\), \(.*\)$/assert_equal \2, \1
Chad Birch
+4  A: 

Via regex:

:s/\v([^, ]+)(\s*,\s*)([^, ]+)/\3\2\1/

If you do it often you can make a map out of it, e.g.:

:nmap <F5> :s/\v([^, ]+)(\s*,\s*)([^, ]+)/\3\2\1/<CR>

Put the cursor on the line you want to flip and hit F5.

Brian Carper
+1  A: 

This one swaps the word your cursor is on with the next one - just press F9 in command mode:

:map <F9> "qdiwdwep"qp
  • "qdiw: Put the word your cursor is on into buffer 'q'
  • dw: Delete all chars to the beginning of the next word (possibly comma + space)
  • e: Go to end of word
  • p: Paste (comma + space)
  • "qp: Paste buffer 'q' (the first word)
soulmerge
+1  A: 

I've always liked regular expression search and replace for these type of tasks:

:s/\(\w*\), \(\w*\)/\2, \1/

Will swap the first word with second in a comma separated list.

skinp
Just remove the range (%). :s will operate on the current line unless you give it a range.
Jeremy Cantrell
A: 

for something this simple, i would just make a little macro

qadf ea, ^[pxxq

then @a away

dysmsyd