Vim:反转函数的参数顺序?
假设我得到了这个函数调用:
cook("rice", kitchen->pot);
写完之后,我注意到参数顺序是错误的,我应该这样做
cook(kitchen->pot, "rice");
为了解决这个问题,我可以这样做:
1: move cursor over "rice": $b 2: delete a ": da" 3: move to (: % 4: paste "rice": p 5: insert new ,: a, 6: find and delete spare ,: f,x
但是我使用 VIM!我们使用 vim 更快地编辑代码,有什么想法吗?
Suppose I got this function call:
cook("rice", kitchen->pot);
After writing that, I notice parameter order is wrong, I should have done
cook(kitchen->pot, "rice");
To fix that, I could do:
1: move cursor over "rice": $b 2: delete a ": da" 3: move to (: % 4: paste "rice": p 5: insert new ,: a, 6: find and delete spare ,: f,x
But im using VIM! We edit code faster with vim, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有一个类似的问题,它是 Ruby 特定的,但应该适合您:
What is the fast way to revive a comma-separated list in vim?
显然没有灵丹妙药。
就我个人而言,我非常喜欢 Soulemerge 的解决方案。
There is a similar question here which is Ruby specific but which should work for you:
What is the fastest way to reverse a comma-separated list in vim?
Apparently there is no silver bullet.
Personally, I quite like soulemerge's solution.
你的答案确实很有帮助,但我自己找到了一个解决方案:
这适用于大多数类型的参数,特别是对于我的类似 php 的函数调用。
Your answers are really helpful, but I just found a solution by myself:
That will work with most kinds of parameters, and specially for my php-like function calls.
<代码>:地图"qdt,dwep"qp
无需插件。根据 soulemerge 的回答,我发现在处理代码时这会更好一些。换句话说,在以下情况下,
iw
对我来说效果不佳:如果我从
myProp
开始,它只会捕获。< /代码>。此代码仅将您带到逗号,这在交换参数时特别有用。
:map <F9> "qdt,dwep"qp
No plugins required. Based on soulemerge's answer, I found that this works a little better when you're dealing with code. In other words, the
iw
wasn't working well for me in the case of:If I started at
myProp
, it would only capture up to the.
. This code just takes you up to the comma, which is particularly useful if you are swapping parameters.使用omap-param(来自lh-cpp) + 一个东西交换插件,我会输入
f(ldi,lvi,g"
。其中di,
表示删除内部参数,vi,< /code> 表示选择内部参数,而
g"
表示与我们刚刚删除的内容交换选择。但是,这种方法可能看起来有点矫枉过正与简单的方法不同,它仍然可以使用
kitchen->getPot(medium, blue)
等参数。With omap-param (from lh-cpp) + a stuff swapping plugin, I would have typed
f(ldi,lvi,g"
. Wheredi,
says delete inner param,vi,
says select inner param, andg"
says swap selection with the thing we have just deleted.This approach may seem overkill, however unlike naive approaches it will still work with parameters like
kitchen->getPot(medium, blue)
.这里有一个尝试(实际上调用了 Ruby): https://github.com/vim -scripts/reorder.vim
可以通过以下方式调用:
:Reorder 3,2,1
个人认为我们可以做得更好!我可能会尝试为此编写一个插件...认为按键绑定可能是用于向右/向左移动子句的
mcl
和mch
。除了函数参数之外,我还发现自己想要更改英语句子中从句的顺序。 “我还发现自己想改变英语句子中从句的顺序以及函数参数。”
另一个用例是列表中的项目是行分隔的,但最后一个参数单独应该省略尾随逗号。
There is an attempt (which actually calls out to Ruby) here: https://github.com/vim-scripts/reorder.vim
It can be invoked by:
:Reorder 3,2,1
Personally I think we can do better! I may try to write a plugin for this... Thinking the keybinds could be
mcl
andmch
for Move Clause Right / Left.As well as function arguments, I also find myself wanting to change the order of clauses in English sentences. "I also find myself wanting to change the order of clauses in English sentences, as well as function arguments."
Another use case is items in lists which are line-separated, but for which the last argument alone should omit a trailing comma.