Xu Wang
Xu Wang

Reputation: 10597

Quickest way to switch order of comma-sparated list in Vim

Supose I have a function such as

myfunc(arg1 = whatever, arg2 = different)

I would like to transform it to

myfunc(arg2 = different, arg1 = whatever)

What is the quickest command sequence to achieve this? suppose the cursor is on the first "m". My best attempt is fadt,lpldt)%p.

Upvotes: 3

Views: 252

Answers (4)

Caek
Caek

Reputation: 592

With pure vim, with your cursor at the start of the line:

%3dB%pldt,lp

This is the quickest I could think of on the spot (12 strokes). This should work for all names as long as there is always a space around the equal signs.

%     " Jump to closing brace
3dB   " Delete to the beginning of 3 WORDS, backwards
%     " Jump to the beginning brace
p     " Paste the deleted text from the default register
l     " Move right one character
dt,   " Delete until the next comma
l     " Move right one character
p     " paste the deleted text from the default register

You could also turn this into a Macro to use at any time.

Upvotes: 0

Peter Rincker
Peter Rincker

Reputation: 45147

I wrote a plugin for manipulating function arguments called Argumentative. With it you just execute >, and the argument your cursor is on will shift to the right. It also provides argument text object in the form of i, and a,.

Upvotes: 0

kev
kev

Reputation: 161834

There is a vim plugin: vim-exchange

  • visual select arg1 = whatever
  • press Shiftx
  • visual select arg2 = different
  • press Shiftx

Upvotes: 2

Zach
Zach

Reputation: 4792

I would recommend you change it a bit so it will work from wherever the cursor is and so that it will work on any arguments:

0f(ldt,lpldt)%p

All I changed from your method was I added 0 to move the cursor to the beginning and I changed fa to f(l so that it will work regardless of argument name.

Now you can either put this into a macro, or, if you use it a lot, you can make it a mapping:

nnoremap <C-k> 0f(ldt,lpldt)%p

I arbitrarily chose Ctrl-k here put you can use whatever you like.

Upvotes: 0

Related Questions