在Vim中选择整个函数定义 [英] Selecting entire function definition in Vim
问题描述
我现在一直在尝试Vim进行任何文本编辑工作。例如,如果我有这样的功能:
void helloworlds(int num)
{
int n; (n = 0; n printf(Hello World!\\\
);
}
}
我将如何删除整个定义,包括函数名称?
在Vim中很常见,有很多方法!
请注意,前两个解决方案取决于没有空白行。
-
如果您的光标是在具有函数名称的行上,请尝试 d } 。它将删除所有到下一个块(即你的函数体)。
-
在函数体中, d a p 会删除'段落'。
一 } 。 (如果你喜欢这种语法,我推荐Tim Pope的奇妙的 surround.vim ,它增加了更多的功能类似的感觉)。 -
你也可以尝试使用正则表达式来删除,直到下一个最左边的缩进的大括号: d
> ] 和 [ [移至下一个/上一个第一列大括号(相当于使用
/
和?
与前面提到的那个正则表达式组合在一起。与 d 在 Python模式等插件中,这些运算符被重新定义为,以表示您正在寻找的内容:从函数移动到函数。 如何删除如果您位于标题/名称或块之前的行, d 包含的标题
一个 } 应该可以做到。
如果你在一个块下面,你也可以利用这个方便的'偏移'功能的Vim搜索。 d ^ { - ? 1 将在第一列开头大括号首次出现之前向后删除一行。输入这个命令有点棘手。也许你可以制作一个< leader>
快捷方式。
插件
我在Vim中做的C编程并不多,但肯定有插件可以帮助解决这样的问题。试试 Vim脚本或他们的 GitHub上的镜像。
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!\n" );
}
}
How would I be able to delete the whole definition including the function name?
As is common in Vim, there are a bunch of ways!
Note that the first two solutions depend on an absence of blank lines.
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).
Within the function body itself, dap will delete the 'paragraph'.
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).
You could also try using regular expressions to delete until the next far left-indented closing curly brace: d/^}Enter
]] 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.
How to delete the whole block, header included
If you're on the header/name, or the line before the block, da} should do the trick.
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.
Plugins
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屋!