Home > Software engineering >  How to delete from a cursor untill the end of a close bracket (bracket is not in the same line of th
How to delete from a cursor untill the end of a close bracket (bracket is not in the same line of th

Time:11-19

Sometimes I need to delete from half till end of the scope, how to do that in vim I know that I can delete everything inside {} with di{ but I want to delete just half

Here is a ScreenShot for more details:

Code Preview

or how to dele this part of code:

CodePreivew2

I tried V/} Enter n n till I reach to the parent {} then d

I also know that I can do vt} if } is in the same line

but I need to select lines until } while it's not in the same line

CodePudding user response:

You can do this with search:

d/}<Enter>

CodePudding user response:

The notion of "motion" comes from vi but the notion of "text object" came later and, while text objects are for all intent and purpose a special kind of motions, there is no 1-1 relationship to expect between regular, one-way motions and special two-way text objects. So yes, there is a two-way "inner braces" text object but there is no built-in one-way counterpart.

The classic approach is to:

  1. visually select the text object ("scope" is not a Vim concept),
  2. go back to normal mode, which leaves the cursor at the end of the desired text object,
  3. use the desired operator from the cursor to the last position.

In concrete terms:

viB<C-c>d''

That is basically the best we have if we constrain ourselves to the built-ins. I think it is fine and gets the job done but you might find it too verbose. If that's the case, one approach would be to make a dedicated pair of visual mode and operator-pending mode mappings:

xnoremap EB iB<C-c>v''
onoremap EB <Cmd>normal vEB<CR>

which would allow you to do dEB to cut the text from the cursor to the end of the "inner braces text" object.

Notes:

  • creating mappings that go the other way shouldn't be too hard,
  • the formula above is quick and dirty, it will certainly need a bit of work to deal with edge cases,
  • one could imagine defining one-way counterparts of all text objects in a for loop for maximum street credibility.

See the sadly under-developed :help omap-info, and this great article.

  • Related