ChatGPT解决这个技术问题 Extra ChatGPT

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

vim

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?

If the window is large enough, relative line numbering is helpful here. Place the cursor at the top of the window, find the relative line number of the last line to be deleted (say, n), add one, then type that number, then type dd. E.g. if n=50, then 51dd. Alternatively, use a motion command -- 50d<down arrow>
"Find the relative line number" - or, use :set rnu and Vim will do this instantly! The line you're on will still be numbered absolutely, but lines above and below the cursor will symmetrically be numbered 1, 2, ... (and update with cursor movement, file-level events, etc.)
for variable multiple lines between 2 patterns, I finally use sed in linux CLI… see stackoverflow.com/a/6287940/6614155

p
paxdiablo

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.


This freaking rocks! I hate how long I just do something (like counting lines) before I go looking into a better solution. Thanks!
really awesome I am always use the visual mode to delete the block but this way is better +1
I found this more helpful than visual mode for deleting 500k+ lines
Really practical answer. I use it to delete the mails in my server from the file /var/mail/username in bulk, and leaving the last mails intact. I do a mark, then I enter for example 50000 followed by gg to go to line 50.000, then I delete all lines in-between. works like a charm!
@horta, I probably wouldn't be using vim as a tool for editing gigabyte files - I'd tend to opt for text processing tools such as awk and its brethren :-)
M
Mat

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.


Strange. I know it Works For Me (tm) here on Linux (vim 7.3.50) and with gVim on Windows. And judging by the other answers, works for other people. Maybe some settings in your .vimrc is making it act up?
OK, for some reason I didn't realize that you could enter visual line mode using Shift-v ... I was trying to use the CTRL key as well. So this works and is probably the most convenient way.
You may want to clarify that there are two visual modes - Visual Line (CTRL-v) and Visual Block (Shift-v).
I do this a lot. But I always consider it cheating and less-than-ideal vim.
There are actually three visual modes: Visual Mode (Character based {v}), Visual Line Mode (Line based {S-v}) and Visual Block Mode (Allows selection of blocks {C-v})
M
M'vy

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                  

da[, da{, da" all useful for programmers
thanks! dap is very easy to remember as "delete a paragraph"
d
daviewales

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

R
René Nyffenegger

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.


Thanks for the tip. Unfortunately I am working with code here, and usually need more fine-grained control than that.
I'm working with code too, and I separate logical blocks by an empty line. This tip just made my day. I can now delete large blocks of logic with three keystrokes.
it works because 'd' means delete, 'p' in this context means paragraph
J
Jim Mitchener

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.


T
Tom Regner

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


i use dap very often now that i know about it and prefer it to dis and dip, but, it always deletes a block and the blank line that comes after the block, but is were a way to delete the block and the blank line that comes before it?
In the middle of a text with paragraphs there is no syntactic difference in the result, is there? If there is I suggest to write up what you want, with examples, in a new question.
R
Rumple Stiltskin

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


I didn't realize how useful :se rnu was until I started using it - great for knowing to 50dd as well as navigation, 50j
J
John P

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.


J
Judge Maygarden

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.


maybe weird, but from this I figured out how to select text from some desired midpoint in a line and select multiple lines below at the same point to the end of each line. Exactly what I was looking for.
Z
ZyX

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.


R
Rohit Pruthi

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


It might be even easier to use dG, which deletes to the end of the document, as G defaults to going to the last line.
J
Jon Nadal

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: /^$

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*$\\|\%$

Delete: d