Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a paragraph as quickly as possible

Tags:

linux

vim

I want to delete the following codes in my config file:

server {
    listen 80;
    server_name xxx;
    location / {
        try_files xx;
    }
}

I know I can use 7dd, but this is not handy enough- if the section is too long, counting the rows would be inconvenient.

Is there a better way to do this?

Sometimes, I have to delete a whole function, any ideas for that?

like image 670
guosheng1987 Avatar asked Aug 07 '12 06:08

guosheng1987


2 Answers

As in common in Vim, there are a bunch of ways!

Note that the first two solutions depend on an absence of blank lines within the block.

  • If your cursor is on the server line, try d}. It will delete everything to the next block.

  • Within the entry itself, dap will delete the 'paragraph'.

  • You can delete a curly brack block with da}. (If you like this syntax, I recommend Tim Pope's fantastic surround.vim, which adds more features with a similar feel).

  • You could also try using regular expressions to delete until the next far left-indented closing curly brace: d/^}Enter

  • ]] and [[ move to the next/previous first-column curly brace (equivalent to using / and ? with that regex I mentioned above. Combine with the d motion, and you acheive the same effect.

  • If you're below a block, you can also make use of the handy 'offset' feature of a Vim search. d?^{?-1 will delete backwards to one line before the first occurrence of a first-column opening curly brace. This command's a bit tricky to type. Maybe you could make a <leader> shortcut out of it.

Note that much of this answer is taken from previous answer I gave on a similar topic.

like image 114
David Cain Avatar answered Oct 14 '22 18:10

David Cain


If there is a blank line immediately after these lines, Vim will identify it as a paragraph, so you can simply use d} to delete it (assuming the cursor is on the first line).

like image 32
Prince Goulash Avatar answered Oct 14 '22 17:10

Prince Goulash