Delete a block of text in Vim

vim visual mode delete column
vim visual block
vim delete line
vim select block
vim visual block insert
vim delete range of lines
vim undo
vim cheat sheet

So I can delete a text+line using dd (normal mode) and all the below text moves up a line.

I can go into visual mode using Ctrl+v

If I then say do 0 > C+v > jjj > $ > d the text of 4 rows is deleted but the lines are not deleted.

How do I delete a block of text and delete the lines at the same time so any preceding lines of text move up to the cursor?

For something like this I usually use shift+v, jjj...d, but you could delete using text objects as well. See :h text-object. A few examples:

di" - delete inside " dap - delete around paragraph

And you could of course use other commands than d, such as c or v. Something I use all the time is ci( and ci" for editing content inside () and "".

More cool examples using text-objects and visual mode can be found here: What is your most productive shortcut with Vim?


You could use as well, i.e. 4dd as mentioned by FDinoff, or a range, mentioned by Jens. However in most scenarios I personally believe using visual line (shift+v) is more flexible, and you don't have to count lines or anything. It's easy to remember, you see the result instantly, you won't miss counting lines and it'll work even if you're at the top/bottom on the screen.

How to delete blocks of text which contain a specific string?, This took quite a while for larger files. Thatswhy I'm looking for another approach. How can I delete blocks of text in vim when they contain a  Delete a block of text in Vim. So I can delete a text+line using dd (normal mode) and all the below text moves up a line. If I then say do 0 > C+v > jjj > $ > d the text of 4 rows is deleted but the lines are not deleted.

Use either 4dd to delete 4 lines of text.

Or

Use linewise visual block. <S-v> then move to the last line you want to delete then press d

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  To delete multiple lines in vi, use something like 5dd to delete 5 lines. If you're not using Vim, I would strongly recommend doing so. You can use visual selection, where you press V to start a visual block, move the cursor to the other end, and press d to delete (or any other editing command, such as y to copy).

If the block is really large, and you can't be bothered to count the number of lines to delete, but you know the first and last line numbers (:set number helps), you can always go to ex mode and

 :3,1415d

to delete from line 3 through line 1415.

Block Inserting and Deleting Text in VIM, To delete text from several lines, again, use [Ctrl]+[v] and the navigation keys to block select the text to remove. VIM Block Select. Now use the [x]  To delete all the lines from the beginning of the file to your current cursor position in vim, use this command: :1,.d That command can be read as “From line 1 to the current position, delete”, or if you prefer, “Delete from line 1 to the current line position.”

<C-v> puts you in "visual block mode". In that mode, you act on a rectangle (the "block" in "visual block mode") that may or may not cover the lines you want to delete. d only acts on the characters contained in that block, leaving the lines as is.

What you want is "visual line mode" where you specifically act on lines. From normal mode or from any other visual mode, hit V (Shift + v), move your cursor to define your selection then hit d.

Column (block) insert/delete on VIM – something I did not know, I know this is not new information at all, but every time I have to insert some text at some specific index for a group of consecutive strings I have  A protip by badunk about vim, vi, block, and delete. Delete lines in a block of code in vim. vim vi And you wanted to delete the get request to get to here:

delete 4 lines of text, starting at the line the cursor is on:

4dd

or use the ex command d with line addressing, for example:

:3,24d 

would delete lines 3-24.

.

.

.

Here's some useful special characters when using line addressing with ex:

 .       : current line
 $       : last line
 /text/  : next occurrence of text
 ?text?  : previous occurrence of text
 *       : all text currently on screen
 %       : entire file
 +n      : next n lines
 -n      : previous n lines

Getting started with Vim visual mode, Visual mode makes it easier to highlight and manipulate text in Vim. Vim's visual mode has three versions: character, line, and block. Then the visually identified text can be deleted, copied, changed, or modified with any  1) Delete a single line. To delete a single line in Vim editor, follow the steps below. Place the cursor to the beginning of the line. Press the ESC key. Next, press dd i.e quick press letter ' d ' twice in quick succession.

Vim Cheat Sheet, shift text right; < - shift text left; y - yank (copy) marked text; d - delete marked text Tip Registers are being stored in ~/.viminfo, and will be loaded again on next  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. While selecting text, you can perform searches and other advanced movement. Press d (delete) to cut, or y (yank) to copy. Move the cursor to the desired paste location.

vi/vim delete commands and examples, vim delete word commands. When you're in command mode in the vi editor and you want to delete the current word, use the vi delete word  Copy, Cut and Paste in Visual Mode # Vim’s visual mode allows you to select and manipulate text. Place the cursor to the line you want to begin copping or cutting.

How can I select and delete lines of text in vi?, You can also use V in view mode to launch "VISUAL LINE" mode. Select your lines and type d to delete them. Vim tip: Visual block editing One of the features I find very convenient in Vim editing in visual blocks. With CTRL+V (while in normal mode) you can activate the visual block mode, by using the standard movement keys (h, j, k, l or the arrow keys).

Comments
  • @hammar - how do I turn my C+v in OP into pictures of little buttons - I see it on some posts?
  • Use <kbd>...</kbd> tags, e.g. <kbd>Ctrl</kbd> + <kbd>V</kbd>. Don't overdo it, though. Long Vim commands are often easier to read with the normal code markup.
  • @hammar - ok - I do have a tendency to overdo new tricks (check timss answer out ...is that ok or overdoing it?)
  • I'd say that's pushing it a little. But that's subjective, of course. Anyway, we're getting off topic here. Ask on Meta Stack Overflow if you want some more opinions.
  • +1 thanks for that reference - stared and saved to read later
  • Props for the formatting, though I think it's often better to use the normal notation for Vim commands instead of <kbd> markup. There is no unshifted " key or ( key so it doesn't really make sense to use <kbd> for them.
  • @timss - some <kbd> markup would have been nice - think you might have over-reacted!
  • @whytheq I can't really tell what's best since d etc. is both a command but also a mapping. Anyway, I think it's all right now.
  • @timss - for all the markup madness I'd better mark this as the answer
  • 4dd is the obvious simple answer - thanks! seems like just v followed by selecting the lines and pressing d is ok aswell - my mistake was using C+v
  • +1 nice summary - that was my mistake and that is my solution (4dd in normal mode is probably the simplest solution though ...although good for me to practice visual line mode)