Vim: changing/deleting up to the end of a “block”

2019-03-12 21:35发布

When you have a block of text delimitated by brackets or quotes, you can use

ci"
da(

and so on to change that block of text. But is there a way to change or delete from the cursor to the end of that block (in the way cw does it for words)?

标签: vim editor
4条回答
虎瘦雄心在
2楼-- · 2019-03-12 21:58

See the "Text object selection" section of the Vim help. You can define the selection sequences there with delete and change operations. It shows how to select blocks delimited by any of the following (and more):

  • {}
  • ()
  • Quotes (' or ")

For example:

d} and c} will delete and change from the cursor to the end of the paragraph.

查看更多
smile是对你的礼貌
3楼-- · 2019-03-12 22:06

Benoit's answer of using t f T and F is the best way that I know of. When it comes to deleting to the end of a parenthesised block you can use ]). This will take into account any nested parenthesis. There is also a corresponding [(, ]} and [{.

查看更多
smile是对你的礼貌
4楼-- · 2019-03-12 22:15

Use ct) to “correct till closing parenthesis”.

Vim motions with t, f, T and F are very, very useful. :help t, :help f.

Update: If there are nested parentheses where you are:

  • vi)o`` will select till closing parenthese (will select inside parentheses, then switch to other end of the selection and move it to where you were (``)
  • vi)`` will select till opening parenthese (same mechanism, but without needing o)

The first one works only because when you are doing vi) a cursor jump is remembered, and `` goes to previous cursor location. It seems that o in visual mode does not affect this.

查看更多
萌系小妹纸
5楼-- · 2019-03-12 22:15

I don't know of a generic way to do what you want but, assuming you are on the k in:

The (quick (brown) fox) jumps over the lazy dog.
  • v2t) would select:

    (The quick (brown) fox) jumps over the lazy dog.
             ^^^^^^^^^^^^^
    

    If you don't want to count you can chain t) to expand your selection: vt)t) would first select:

    (The quick (brown) fox) jumps over the lazy dog.
             ^^^^^^^^
    

    then:

    (The quick (brown) fox) jumps over the lazy dog.
             ^^^^^^^^^^^^^
    
  • vT( would select:

    (The quick (brown) fox) jumps over the lazy dog.
     ^^^^^^^^^
    
查看更多
登录 后发表回答