在 Vim 中选择整个函数定义 [英] Selecting entire function definition in Vim

查看:17
本文介绍了在 Vim 中选择整个函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

近一个星期以来,我一直在尝试使用 Vim 进行任何文本编辑工作.我想知道选择 C ​​函数定义的最快方法.

I've been trying Vim for any text editing work for almost a week now. I want to know the fastest way to select a C function definition.

例如,如果我有一个这样的函数:

For example, if I have a function like this:

void helloworlds( int num )
{
    int n;
    for ( n = 0; n < num; ++n ) {
        printf( "Hello World!
" );
    }
}

我怎样才能删除包括函数名在内的整个定义?

How would I be able to delete the whole definition including the function name?

推荐答案

在 Vim 中很常见,有很多方法!

As is common in Vim, there are a bunch of ways!

请注意,前两个解决方案取决于不存在空行.

Note that the first two solutions depend on an absence of blank lines.

  • 如果你的光标在函数名的那一行,试试d}.它将删除下一个块的所有内容(即您的函数体).

  • If your cursor is on the line with the function name, try d}. It will delete everything to the next block (i.e. your function body).

在函数体内,dap 将删除段落".

Within the function body itself, dap will delete the 'paragraph'.

您可以使用 da} 删除花括号块.(如果你喜欢这种语法,我推荐 Tim Pope 的出色的 surround.vim,它增加了更多的功能类似的感觉).

You can delete a curly brace block with da}. (If you like this syntax, I recommend Tim Pope's fantastic surround.vim, which adds more features with a similar feel).

您也可以尝试使用正则表达式删除直到下一个最左缩进的右花括号:d/^}回车

You could also try using regular expressions to delete until the next far left-indented closing curly brace: d/^}Enter

]][[ 移动到下一个/上一个第一列花括号(相当于将 /? 与我上面提到的正则表达式一起使用.结合 d 运动,您可以获得相同的效果.在插件中像Python-mode,这些运算符被重新定义 准确地表示您正在寻找的内容:从一个函数移动到另一个函数.

]] and [[ move to the next/previous first-column curly brace (equivalent to using / and ? with that regex I mentioned above. Combine with the d motion, and you acheive the same effect. In addons like Python-mode, these operators are redefined to mean exactly what you're looking for: move from function to function.

如果您在标题/名称或块之前的行上,da} 应该可以解决问题.

If you're on the header/name, or the line before the block, da} should do the trick.

如果你在一个块之下,你还可以利用 Vim 搜索的方便的偏移"功能.d?^{?-1 将向后删除到第一列左花括号第一次出现之前的一行.这个命令的输入有点棘手.也许你可以用它制作一个 快捷方式.

If you're below a block, you can also make use of the handy 'offset' feature of a Vim search. d?^{?-1 will delete backwards to one line before the first occurrence of a first-column opening curly brace. This command's a bit tricky to type. Maybe you could make a <leader> shortcut out of it.

我不会在 Vim 中进行太多 C 编程,但是肯定有插件可以帮助完成这样的事情.尝试 Vim 脚本 或他们的 镜像在 GitHub.

I don't do much C programming in Vim, but there are surely plugins to help with such a thing. Try Vim Scripts or their mirror at GitHub.

这篇关于在 Vim 中选择整个函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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