是否有一个很好的Vi(m)命令用于在函数调用中转置参数?Emacs的奖励积分

sh1*_*mer 11 vim emacs keyboard-shortcuts

例如,如果我有一些代码,如:

foo = bar("abc", "def", true, callback);
Run Code Online (Sandbox Code Playgroud)

是否有一个很好的命令可以将真实移到第一或第二位置,使逗号完好无损?

PS作为奖励我的朋友想知道这是否也适用于Emacs.

Dav*_*rby 19

In Vim if you place the cursor at the start of the first word and do dWWP then it will have the desired effect. Here is a breakdown:

dW   delete the current word, including the comma and the following whitespace
W    move to the start of the next word
P    insert the deleted text before the cursor
Run Code Online (Sandbox Code Playgroud)

This will work if there are further parameters after the pair to be swapped - it will need to be modified if there are only two parameters or you want to swap the last two parameters, since it will paste the text after the closing bracket.

Alternatively you could use a regex substitution:

:%s/(\([^,]\+\),\s*\([^,)]\+\)/(\2, \1/ 
Run Code Online (Sandbox Code Playgroud)

This will find the first two arguments after the open bracket and swap them.

update:

A search of vim.org found the swap parameters plugin, which should do exactly what you want and can handle situations that either of the above methods cannot.


JSO*_*SON 18

我不知道vi的答案,但在Emacs中,transpose-sexps(C-M-t)会在光标的两侧交换两个参数.实际上transpose-words(M-t)是我的第一个猜测,但这留下了引号.