Updated: 4 November 2026 · Applies to: Vim 9.1 (Debian 13, Ubuntu 26.04, AlmaLinux 10) and Neovim 0.10.4 and 0.11.6

To delete all text in Vim, press Esc to be in normal mode, then type ggdG: gg jumps to the first line and dG deletes everything down to the last line. You can also type :%d and press Enter. Save with :w, or press u to undo. For smaller deletions: dd deletes the current line, dw a word and x one character.

ggdG     delete all text (normal mode)
:%d      delete all lines (command line, same result)
dd       delete the current line
dw       delete to the start of the next word
x        delete the character under the cursor
u        undo the last change

Every command below was run on a test file with Vim 9.1 on Debian 13, Ubuntu 26.04 and AlmaLinux 10 and gave the same result. The same keys work in Vi on these systems (it is Vim) and in Neovim.

Delete all text in a file

  1. Open the file: vim /etc/nginx/conf.d/test.conf
  2. Press Esc once or twice, so you are in normal mode (no -- INSERT -- at the bottom).
  3. Type ggdG. The window now shows one empty line. The command-line forms :%d and :1,$d do the same: % and 1,$ both mean "from the first to the last line".
  4. Type :wq and press Enter to save and quit, or :q! to quit without saving.

Deleted text is not gone yet: Vim keeps it in a register, and p or P pastes it back. If you want to keep what you copied earlier, delete into the "black hole" register instead: :%d _. To empty a file without opening it, run > file.conf or truncate -s 0 file.conf in the shell.

Delete lines

  • dd: the current line. 3dd: the current line and the two below it (same as d2j).
  • dG: from the current line to the end of the file. dgg: from the current line to the top.
  • :2,4d: lines 2 to 4. Type :set number to see line numbers.
  • :5,$d: line 5 to the end. :.,$d: the current line to the end (. is the current line).
  • :.,+1d: the current line and the next one.

Delete lines that match a pattern

:g/PATTERN/d deletes every line that matches. On a small config file:

# sample config
server_name example.com;

listen 80;
# old setting
root /var/www/html;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log off;
  • :g/^#/d deleted the two comment lines (lines starting with #), leaving 7 lines.
  • :g/^$/d deleted the empty line. :g/^\s*$/d also deletes lines that contain only spaces or tabs.
  • :g/log/d deleted the two lines containing log.
  • :g!/log/d (or :v/log/d) did the opposite: it kept only those two lines:
    error_log /var/log/nginx/error.log;
    access_log off;

To delete a word everywhere without removing the lines, substitute it with nothing: :%s/index\.php //g turned index index.php index.html; into index index.html;. To remove spaces at the end of every line: :%s/\s\+$//e.

Delete words and characters

Examples on the line server_name example.com;, with the cursor on the first letter of the word named:

  • x deletes the character under the cursor; 3x deletes three. X deletes the character before the cursor.
  • dw on server_name deletes the word and the space after it: example.com;
  • diw on example deletes just the word: server_name .com;. daw also takes the space: server_name.com;
  • D (same as d$) on example deletes to the end of the line: server_name
  • d0 deletes everything before the cursor on that line.
  • dt. deletes up to the next ., leaving .com;; df. deletes the . too, leaving com;

The pattern is always d plus a movement, so d} deletes to the end of the paragraph and di" deletes the text inside quotes (it turned root "/var/www/html"; into root "";).

Delete a selection (visual mode)

  1. Move to the first line you want to remove and press V (whole lines), v (characters) or Ctrl+v (a rectangular block).
  2. Move with the arrow keys or j and k to extend the selection.
  3. Press d. In the test, Vjjd on line 2 removed lines 2 to 4.

Delete while typing (insert mode)

In insert mode, Backspace deletes the character before the cursor, Ctrl+w the word before it and Ctrl+u everything before it on the line. If Backspace stops at the point where you started typing, run :set backspace=indent,eol,start (Vim 9.1 on Debian 13 and Ubuntu 26.04, and the default /etc/vimrc on AlmaLinux 10, already use this setting).

Undo a deletion

Press u to undo and Ctrl+r to redo. In the test, ggdG followed by u brought all nine lines back. If you have not saved, :q! also leaves the file as it was on disk.

Vi, Vim or Neovim?

On Ubuntu 26.04 with both Vim and Neovim installed, the vim and vi commands opened Neovim (0.11.6); all commands above behaved the same there. On AlmaLinux, vi is a smaller Vim build from the vim-minimal package; ggdG and :%d work in it as well. If you prefer a simpler editor for quick config changes, see Linux Nano Editor Explained for Beginners.

More Vim help: How to Reload Vim: Reload .vimrc Without Restarting and Reload the Current File, How to Enable Syntax Highlighting in Vi and Vim and PacVim: A Game That Teaches You Vim Commands.

Official documentation: Vim help: deleting text.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket