使用Vim中的`di{`仅删除函数体。

huangapple go评论57阅读模式
英文:

Removing only the function body using `di{` in Vim

问题

最近我开始学习Vim,卡在了它在更改/删除文本对象方面的行为上,特别是花括号括起的代码块。

以下是代码示例:

function main() {
  console.log("Hello world");
}

如果我将光标移动到单词function的开头并输入以下组合:di{,Vim将删除函数体,符合预期。

然而,如果我将函数放入类中:

class Program {
    main() {
        console.log("Hello world");
    }
}

然后将光标移动到单词main的开头并再次输入相同的组合,Vim将删除类的主体,而不是函数的主体。

有人能解释一下它是如何工作的,是否有任何解决方法,或者有经验的Vim用户在这种情况下做些什么以实现我尝试做的事情吗?

英文:

I've recently started learning Vim and got stuck on its behavior regarding changing/deleting text objects, particularly code blocks surrounded by curly braces.

Here is an example of code:

function main() {
  console.log("Hello world");
}

If I move my cursor at the beginning of the word function and enter the following combination: di{ Vim will delete the function body, as expected.

However, if I move the function inside a class:

class Program {
    main() {
        console.log("Hello world");
    }
}

and then move the cursor at the beginning of the word main and enter the same combination again Vim will delete the body of the class, not the function.

Could somebody explain to me how it works and if there is any workaround or what the experienced vim users do in such situations to achieve what I'm trying to do?

答案1

得分: 4

The di{ will delete everything inside the curly braces the cursor is in (mnemonic: Delete In {). It does not account for syntactic elements like a function name. Vim does, however, honor that some characters appear in pairs, e.g. { and }, ( and ), [ and ], ...

It will reverse-search to the previous opening brace and delete everything to the matching closing brace. It does roughly the same as ?{<CR>d% (try it out and see what happens).

In your example, you are on the function name and want to delete the respective function. Vim, however, only sees the braces here and does exactly what it's told: delete in braces, i.e. the whole class.

Possible workarounds in this situation:

  • move to the next opening brace on the line, then delete: f{di{
  • move to the end of line (the opening brace), then delete: $di{
  • move cursor one line down (to be inside the braces), then delete: jdi{
  • delete everything to the next closing brace: d][

The list goes on ...

There are probably also plugins for whatever language you want to write.

英文:

The di{ will delete everything inside the curly braces the cursor is in (mnemonic: Delete In {). It does not account for syntactic elements like a function name. Vim does, however, honor that some characters appear in pairs, e.g. { and }, ( and ), [ and ], ...

It will reverse-search to the previous opening brace and delete everything to the matching closing brace. It does roughly the same as ?{<CR>d% (try it out and see what happens).

In your example, you are on the function name and want to delete the respective function. Vim, however, only sees the braces here and does exactly what it's told: delete in braces, i.e. the whole class.

Possible workarounds in this situation:

  • move to the next opening brace on the line, then delete: f{di{
  • move to the end of line (the opening brace), then delete: $di{
  • move cursor one line down (to be inside the braces), then delete: jdi{
  • delete everything to the next closing brace: d][

The list goes on ...

There are probably also plugins for whatever language you want to write.

huangapple
  • 本文由 发表于 2023年2月27日 04:00:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定