Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

People also ask

How do I delete a section in Vim?

Deleting a single line in Vim editor: First, bring your cursor to the line you want to delete. Press the “Esc” key to change the mode. Now type, “:d”, and press “Enter” to delete the line or quickly press “dd”.

How can you delete a block of text?

Click at the start of the block of text, hold down the Shift key, and click at the end of the block to select the portion of text and finally press either the Backspace key or the Delete key. Double-click anywhere on the word you want to delete and finally press either the Backspace key or the Delete key.

How do you select a block of text in Vi?

Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block. Move the cursor to the end of the text to be cut/copied.


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.


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.


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                  

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