tags:

views:

151

answers:

6

say I have this line

= function (x, y, word);

and I want to convert it to

 word = function (x,y);

Thus far, I have been manually selecting the word, then 'x', and then paste it at the beginning. And then I would remove unnecessary comma.

Is there a more efficient way to accomplish the same thing?

A: 

Try this: :dw to cut the current word, move to beginning of line, then :p to paste the buffer there.

John Feminella
Does `:dw` remove the comma automagically, then?
T.J. Crowder
+8  A: 

Don't create weired functions or macros, as many advanced users may suggest you, but learn simple commands, which can help you when you would need to make similar, but slightly different substitution.

My solution would be: place cursor on the comma, and type: xxdw^Pa <C-[>

Description:

  • xx - delete comma and space
  • dw - delete word
  • ^ - place cursor on the beginning of text in line
  • P - place deleted text before cursor
  • a - add space after word
  • <C-[> - escape to return to normal mode, you can also press <ESC> if you like, or don't press at all

And how to place cursor in comma? Learn about f,, F,, t,, T,, w, b and e to move faster around your text.

MBO
Woot, that is true VIM Fu!
Adrian
+1  A: 

I'd suggest recording a macro: (starting at the beginning of the line) qq2f,2xdw0Pa <esc>0jq, then running that macro wherever you need it: @q.

Randy Morris
+4  A: 
:%s/\(.*\),\([^)]*\)/\2\1/

EDIT:removed /g

EDIT2: the %s is only if you want to do this for the entire file. if you just want to do this for the current line then replace % with . (a dot)

Nir Levy
:s/\(.*\), \([^)]*\)/\2 \1/g would make the result a bit more pretty.
Randy Morris
William Pursell
@rson. right, only that developers do not always add the right space so lets make it: s/\(.*\), ?\([^)]*\)/\2 \1/
Nir Levy
A: 

Place cursor over word and type:

"0diw    delete word and store it in register 0
dF,      delete backwards up to and including ,
^        place cursor at first character in line
"0P      paste word

I would suggest to map this to a key.

Habi
A: 

Or you could use a regular expression.

    :s/\(^.*\), \(\a\+\)\();\)/\2\1\3/

(Match up to the last comma) -> \1

(match last argument) -> \2

(Match closing brace and semicolon) -> \3

The reorder the matched terms as you need.

martsbradley