有没有一个好的Vi(m)命令来调用参数在函数调用?奖励积分为Emacs [英] Is there a good Vi(m) command for transposing arguments in a function call? Bonus points for Emacs

查看:150
本文介绍了有没有一个好的Vi(m)命令来调用参数在函数调用?奖励积分为Emacs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  foo = bar(abc,def,true,回电话); 

有没有一个很好的命令来移动到第一或第二位置,留下逗号不变? p>

PS作为奖励我的朋友想知道这是否在Emacs中有效。

解决方案

在Vim中,如果将光标放在第一个单词的开头,并执行 dWWP ,那么它将具有所需的效果。这是一个细分:

  dW删除当前单词,包括逗号和以下空格
W移动到开始下一个单词
P在光标前插入删除的文本

在要交换的对之后还有其他参数 - 如果只有两个参数需要修改,或者您想要交换最后两个参数,因为它将在结束括号后粘贴文本。



或者您可以使用正则表达式替换:

 :%s /(\([ ^,] \ + \),\s * \([^,)] \ + \)/(\2,\1 / 
/ pre>

这将在打开括号后面找到前两个参数,并交换它们。



更新



搜索vim.org发现 swap参数插件,其中你可以做你想要的,可以处理上述两种方法之一的情况。


For example if I have some code like:

foo = bar("abc", "def", true, callback);

Is there a nice command to move true to the 1st or 2nd position leaving the commas intact?

P.S as a bonus my friend want to know if this works in Emacs too.

解决方案

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

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/ 

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.

这篇关于有没有一个好的Vi(m)命令来调用参数在函数调用?奖励积分为Emacs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆