4

I have cursor at first t and I want to delete all this code from t to };\n without lines counting.

How can I achieve it? Is there cleaner way than d/};$/e?

tabControl.PropertyChanged += (s, e) =>
{
    ...
};

If it matters, this is C#.

George Lanetz
  • 300
  • 5
  • 18

2 Answers2

4

Here is a cleaner and simple way to get the desired output in 3 steps:

1. First click dd in normal mode to delete the first line :

tabControl.PropertyChanged += (s, e) =>

As a result the line below will be moved up where the cursor is placed

{

Notice that the cursor must be placed in the opening { so you can use the % command as you will see in the 2 step

2. Type Shift+v % (for selection) so you can be sure that the exact block {} will be selected

{
    ...
};

Note: it is V (Uppercase) not v there is a difference between the two commands

v Start Visual mode per character.

V Start Visual mode linewise.

3. Type then d : to delete the selected block

so the sequence will be dd Shift+v %d

you can also check the help to see what % really do by typing in the command line

:help %

% : Find the next item in this line after or under the cursor and jump to its match. |inclusive| motion. Items can be: ([{}]) parenthesis or (curly/square) brackets

I tried to underline some useful commands but if you understand exactly what the V and % do you can select directly the whole block you would like to delete or to modify

  • Are you sure you can't just do `d%`? – Nic Jun 24 '16 at 02:31
  • the `%` is only useful if you are sure that the cursor is above one of these characters { } [ ] ( ) and if you read the question you will notice it is not the case. So `d%` will do nothing ! – Meninx - メネンックス Jun 24 '16 at 02:36
  • Oh, my understanding was that it would seek forward to the nearest valid character, then do its magic. I could have sworn I'd seen that behavior... somewhere, though I can't find it anymore. thanks! – Nic Jun 24 '16 at 02:41
  • @QPaysTaxes It will do that, but only for characters on the same line. – Brian McCutchon Sep 10 '17 at 23:55
2

The fastest way is probably Vj%d, but see below for shorter options that work in many cases.

  • V = enter Visual line mode.
  • j = go down one character.
  • % = go to matching brace.
  • d = delete the selection.

Alternatively, if there are no empty lines in the lambda, but there is an empty line after it, I would use d} (} is go to 'end of paragraph', which usually means the next empty line) or dap (neumonic: 'delete all paragraph', works from anywhere in the 'paragraph', assuming your function has empty lines before and after). dap is really useful for deleting short blocks of code.

Note also that your solution will not work if there is a nested lambda terminated by };.

Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45