Fastest way to switch comma order in Vim
Let's say I have a function like
myfunc(arg1 = whatever, arg2 = different)
I would like to convert it to
myfunc(arg2 = different, arg1 = whatever)
What is the fastest sequence of commands to achieve this goal? suppose the cursor is on the first "m". My best attempt is fadt,lpldt)%p
.
source to share
There is a vim plugin: vim-exchange
- visual selection
arg1 = whatever
- click Shift x
- visual selection
arg2 = different
- click Shift x
source to share
I would recommend you modify it a bit so that it works from wherever the cursor is, and so that it works on any arguments:
0f(ldt,lpldt)%p
Anything I changed from your method, I added 0
to move the cursor to the beginning, and I changed fa
to f(l
so that it works regardless of the argument name.
Now you can put this in a macro, or if you use it a lot, you can do a mapping:
nnoremap <C-k> 0f(ldt,lpldt)%p
I've arbitrarily chosen Ctrl-k here, you can use whatever you like.
source to share
I wrote a plugin to manipulate the arguments of the Argumentative function . With it, you just execute >,
, and the argument your cursor is on will shift to the right. It also provides an argument text object in the form i,
and a,
.
source to share
With pure vim with your cursor at the beginning of the line:
%3dB%pldt,lp
This is the fastest I could think of on the spot (12 strokes). This should work for all names if there is always room around 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 can also turn this into a Macro for use anytime.
source to share