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?
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
.
This one swaps the word your cursor is on with the next one - just press F9 in command mode:
:map <F9> "qdiwdwep"qp
Map a key combination to perform the command:
:s/^assert_equal \(.*\), \(.*\)$/assert_equal \2, \1
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With