用于向函数添加参数的Vim快捷方式

时间:2021-10-12 16:05:13

Is there a Vim shortcut for jumping to the argument list of the current function? I often find myself needing to mess with the argument list of a function, and it's kind of annoying to have to do ?def or ?function or 10k or what-have-you until I finally get to it, then /( or t( or 5e to get to the right position in the argument list, and so on. It would be great if I could just hit ,a for example and instantly get put into insert mode at the end/beginning of the argument list.

是否有Vim快捷方式跳转到当前函数的参数列表?我经常发现自己需要弄乱一个函数的参数列表,并且必须这样做是很烦人的吗?def或?函数或10k或者你有什么东西,直到我最终得到它,然后/(或t(或者5e到达参数列表中的正确位置,依此类推。如果我能够点击,例如并且在参数列表的结尾/开头立即进入插入模式,那将是很好的。

Possible approaches:

  • Folding
  • Tag support (ctags)
  • 标签支持(ctags)

Also, I'm using Python, so solutions based on curly braces unfortunately won't work.

此外,我正在使用Python,因此基于花括号的解决方案不幸无法使用。

If no such shortcut exists, I'll just write one and post it here as an answer. :-)

如果不存在这样的快捷方式,我只需写一个并将其作为答案发布在此处。 :-)

3 个解决方案

#1


1  

The fool proof way of getting to the begining of a function is to use [[. So you use

到函数开头的简单方法是使用[[。所以你用

map ,a ma[[kf(a

地图,马[[kf(a

so it can take you to the function definition, search for the first occurance of "(" and then put you in the insert mode.

所以它可以带你到函数定义,搜索第一次出现“(”然后让你进入插入模式。

#2


3  

Disclaimer, I don't know Python, I assume a Python function can be identified by "function" or "def" from your question. Just change the regex in consequence.

免责声明,我不知道Python,我假设Python函数可以通过你的问题中的“函数”或“def”来识别。只是改变正则表达式。

May be something like:

可能是这样的:

   :nnoremap <buffer> [m :call search('def\|function', 'b')<cr>f(

?

NB:

  • I have used search() in order to not mess up the search history ; searchpair() may be a better choice as it will only jump to the definition of the function we are within, instead of the previous function.
  • 我使用了search()以免弄乱搜索历史; searchpair()可能是一个更好的选择,因为它只会跳转到我们所在函数的定义,而不是前一个函数。

  • As this is intended to work with Python only, I use <buffer> in order to not mess up the key-binding in non-Python files; this mapping is best defined in a python ftplugin.
  • 由于这只适用于Python,我使用 以便不搞乱非Python文件中的键绑定;这个映射最好在python ftplugin中定义。

HTH,

#3


1  

map ,a ma[{F(a

地图,马[{F(a

Hit ,a to go to the argument list, then `a to return to where you were when you invoked ,a. Caveat: [{ jumps back to the last unmatched { character, so if you're inside a loop or other control structure, it will take you to the beginning of that, instead.

点击,a进入参数列表,然后“a”返回到调用时的位置,a。警告:[{跳回到最后一个不匹配的{字符,所以如果你在一个循环或其他控制结构中,它将带你到那个开头,而不是。

I don't know of a way to get to the beginning of the function in a fool-proof way. If you're consistent about your tabbing, you may be able to do something like this:

我不知道如何以一种万无一失的方式进入函数的开头。如果你对你的标签保持一致,你可能会做这样的事情:

map ,a ma?function :nohlf(a

map,ma?function:nohlf(a

where, if you don't use a single tab before you define your functions, you'd change to appropriate value.

其中,如果在定义函数之前未使用单个选项卡,则会更改为适当的值。

#1


1  

The fool proof way of getting to the begining of a function is to use [[. So you use

到函数开头的简单方法是使用[[。所以你用

map ,a ma[[kf(a

地图,马[[kf(a

so it can take you to the function definition, search for the first occurance of "(" and then put you in the insert mode.

所以它可以带你到函数定义,搜索第一次出现“(”然后让你进入插入模式。

#2


3  

Disclaimer, I don't know Python, I assume a Python function can be identified by "function" or "def" from your question. Just change the regex in consequence.

免责声明,我不知道Python,我假设Python函数可以通过你的问题中的“函数”或“def”来识别。只是改变正则表达式。

May be something like:

可能是这样的:

   :nnoremap <buffer> [m :call search('def\|function', 'b')<cr>f(

?

NB:

  • I have used search() in order to not mess up the search history ; searchpair() may be a better choice as it will only jump to the definition of the function we are within, instead of the previous function.
  • 我使用了search()以免弄乱搜索历史; searchpair()可能是一个更好的选择,因为它只会跳转到我们所在函数的定义,而不是前一个函数。

  • As this is intended to work with Python only, I use <buffer> in order to not mess up the key-binding in non-Python files; this mapping is best defined in a python ftplugin.
  • 由于这只适用于Python,我使用 以便不搞乱非Python文件中的键绑定;这个映射最好在python ftplugin中定义。

HTH,

#3


1  

map ,a ma[{F(a

地图,马[{F(a

Hit ,a to go to the argument list, then `a to return to where you were when you invoked ,a. Caveat: [{ jumps back to the last unmatched { character, so if you're inside a loop or other control structure, it will take you to the beginning of that, instead.

点击,a进入参数列表,然后“a”返回到调用时的位置,a。警告:[{跳回到最后一个不匹配的{字符,所以如果你在一个循环或其他控制结构中,它将带你到那个开头,而不是。

I don't know of a way to get to the beginning of the function in a fool-proof way. If you're consistent about your tabbing, you may be able to do something like this:

我不知道如何以一种万无一失的方式进入函数的开头。如果你对你的标签保持一致,你可能会做这样的事情:

map ,a ma?function :nohlf(a

map,ma?function:nohlf(a

where, if you don't use a single tab before you define your functions, you'd change to appropriate value.

其中,如果在定义函数之前未使用单个选项卡,则会更改为适当的值。