开发者

How to delete from the cursor to the end of a parenthetical expression in Vim?

开发者 https://www.devze.com 2023-03-31 02:25 出处:网络
Say I have this line of text in Vim: (foo bar (baz) qux) ^ and my cursor is on the space between the words foo and bar, as indicated. I often find that, in situations like this, I want to delete the

Say I have this line of text in Vim:

(foo bar (baz) qux)
    ^

and my cursor is on the space between the words foo and bar, as indicated. I often find that, in situations like this, I want to delete the entire right-hand side of the outer parenthesized expression (that is, to the right of my cursor), while leaving the left-hand side intact. That is, I'd like to end up with:

(foo)

Usually, I’d accomplish this with dt) (“delete until )”), but the addition of a nested parenthetical complicates things: That command would leave me with (foo) qux). I could also use d2t), but I’d prefer not to have to manually count the number of nested parentheses. I could also use di), but that deletes the entire text inside of the parentheses, leaving me with ().

Is there a Vim 开发者_开发技巧motion with the balance-awareness of the i- and a-modified motions that is also relative to the current cursor position?


You can use the ]) motion with d.

d])


mxF(%d`x

Breaking it down:

mx

Set a mark x (pick whatever letter you like)

F(

Find previous ( character

%

Jump to matching )

d`x

Delete from here to mark x

That works for your specific case; I'm not sure how general it is. If the previous ( is not on the current line, use ?(<return> rather than F(.

EDIT:
I didn't mention d]) because I didn't know about it.

My solution won't work for this case:

( (before) foo (after) )
              ^

because it jumps back to the nearest (, not the nearest enclosing (.

0

精彩评论

暂无评论...
验证码 换一张
取 消