vim - How to delete a large block of text without counting the lines?

Vim

Vim Problem Overview


In vim, I often find myself deleting (or copying) large blocks of text. One can count the lines of text and say (for example) 50dd to delete 50 lines.

But how would one delete this large block of text without having to know how many lines to delete?

Vim Solutions


Solution 1 - Vim

Go to the starting line and type ma (mark "a"). Then go to the last line and enter d'a (delete to mark "a").

That will delete all lines from the current to the marked one (inclusive). It's also compatible with vi as well as vim, on the off chance that your environment is not blessed with the latter.

Solution 2 - Vim

I'm no vim guru, but what I use in this circumstance is "visual mode". In command mode, type V (capital). Then move up/down to highlight the block you want deleted (all the usual movement commands work). Then remove it with x or d.

Solution 3 - Vim

You can use the visual mode also (some commands are usable with the delete option also) vip vap to select paragraph, v2ap to select two paragraphs dap works, d2ap also. You can delete within blocks of [ ] like da[

For reference: the types of objects. From vim documentation : section 4. http://vimdoc.sourceforge.net/htmldoc/visual.html

4. Operating on the Visual area				*visual-operators*

...    
The objects that can be used are:
aw	a word (with white space)			
iw	inner word					
aW	a WORD (with white space)			
iW	inner WORD					
as	a sentence (with white space)			
is	inner sentence					
ap	a paragraph (with white space)			
ip	inner paragraph					
ab	a () block (with parenthesis)			
ib	inner () block					
aB	a {} block (with braces)			
iB	inner {} block					
a<	a <> block (with <>)				
i<	inner <> block					
a[	a [] block (with [])				
i[	inner [] block					

Solution 4 - Vim

There are many better answers here, but for completeness I will mention the method I used to use before reading some of the great answers mentioned above.

Suppose you want to delete from lines 24-39. You can use the ex command

:24,39d

You can also yank lines using

:24,39y

And find and replace just over lines 24-39 using

:24,39s/find/replace/g

Solution 5 - Vim

It sort of depends on what that large block is. Maybe you just mean to delete a paragraph in which case a dip would do.

Solution 6 - Vim

If you turn on line numbers via set number you can simply dNNG which will delete to line NN from the current position. So you can navigate to the start of the line you wish to delete and simply d50G assuming that is the last line you wish to delete.

Solution 7 - Vim

There are several possibilities, what's best depends on the text you work on.

Two possibilities come to mind:

  • switch to visual mode (V, S-V, ...), select the text with cursor movement and press d

  • delete a whole paragraph with: dap

Solution 8 - Vim

If the entire block is visible on the screen, you can use relativenumber setting. See :help relativenumber. Available in 7.3

Solution 9 - Vim

Counting lines is too tedious for me, but counting 'paragraphs' isn't so bad. '{' and '}' move the cursor to the first empty line before and after the cursor, respectively. Cursor moving operations can be combined with deletion, and several other answers used a similar approach (dd for a line, dG for the end of the document, etc.)
For example:

/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. */

Lorem *ipsum(void) {
  return dolor(sit, amet);
}

If your cursor starts above the comment block, 'd}' deletes the comment block, and 'd2}' deletes both the comment block and the code block. If your cursor starts below the code block, 'd{' deletes the code, and 'd2{' deletes both. Of course, you can skip over one block by moving the cursor first: '{d{' or '}d}'.

If you're consistent with your whitespace, or you can count the paragraphs at a glance, this should work. The Vim help file has more cursor tricks if you're interested.

Solution 10 - Vim

You could place your cursor at the beginning or end of the block and enter visual mode (shift-v). Then simply move up or down until the desired block is highlighted. Finally, copy the text by pressing y or cut the text by pressing d.

Solution 11 - Vim

Alongside with other motions that are already mentioned here, there is also /{pattern}<CR> motion, so if you know that you want to delete to line that contains foo, you could do dV/foo<CR>. V is here to force motion be line-wise because by default / is characterwise.

Solution 12 - Vim

You can also enter a very large number, and then press dd if you wish to delete all the lines below the cursor.

Solution 13 - Vim

Deleting a block of text

Assuming your cursor sits at the beginning of the block:

V/^$<CR>d (where <CR> is the enter/return key)
Explanation
  • Enter "linewise-visual" mode: V
  • Highlight until the next empty line: /^$<CR>
  • Delete: d

Key binding

A more robust solution:

:set nowrapscan
:nnoremap D V/^\s*$\\|\%$<CR>d
Explanation
  • Disable search wrap: :set nowrapscan
  • Remap the D key (to the following commands): :nnoremap D
  • Enter "linewise-visual" mode: V
  • Highlight until the next empty/whitespace line or EOF: /^\s*$\\|\%$<CR>
  • Delete: d

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustin EthierView Question on Stackoverflow
Solution 1 - VimpaxdiabloView Answer on Stackoverflow
Solution 2 - VimMatView Answer on Stackoverflow
Solution 3 - VimM'vyView Answer on Stackoverflow
Solution 4 - VimdaviewalesView Answer on Stackoverflow
Solution 5 - VimRené NyffeneggerView Answer on Stackoverflow
Solution 6 - VimJim MitchenerView Answer on Stackoverflow
Solution 7 - VimTom RegnerView Answer on Stackoverflow
Solution 8 - VimRumple StiltskinView Answer on Stackoverflow
Solution 9 - VimJohn PView Answer on Stackoverflow
Solution 10 - VimJudge MaygardenView Answer on Stackoverflow
Solution 11 - VimZyXView Answer on Stackoverflow
Solution 12 - VimRohit PruthiView Answer on Stackoverflow
Solution 13 - VimJon NadalView Answer on Stackoverflow