1

enter image description here

My problem

Consider a code block prefixed with a few words (function name, for example). The vim cursor is in the middle of the prefix word. The line ends with a {, which declares a block which spans a few more lines, till a matching }.

I would like to delete the entire block - from the line marked as "24" to the line marked as "7".

What have I tried

  • d% deletes from the cursor position onwards, till the end of the block, but keeps the subgraph c part intact.
  • d8d works, but I need to know how many lines does the block span.
  • d%dd works, but I hope there's a more elegant way that I'm missing

My Question

How can I delete an entire block, including the entire line in which the block starts, with any prefixes before the opening braces?

  • According to your screenshot you use relativenumber, so you do know how many lines the block spans. Why do you not want to use a feature you have specifically enabled (by default it is turned off)? Btw I would press 8dd (i.e. delete 8 lines). – Hotschke Jul 25 '18 at 17:55
6

With the cursor on the first line, the proper operation is dV%. The d% means delete from the next { to the matching }. the V means force the d operator to work linewise, rather than characterwise. See (:h o_v).

2

Press V, which highlights the line under cursor, then repeatedly cursor-down until the block ends (which highlights more lines), then d to delete this block.

The advantage is, that you have a visual feedback on what gets deleted.

  • Thanks. The downside is that the block might be pretty long, and d%dd is faster. – Adam Matan Jul 25 '18 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.