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
- Open the file:
vim /etc/nginx/conf.d/test.conf - Press
Esconce or twice, so you are in normal mode (no-- INSERT --at the bottom). - Type
ggdG. The window now shows one empty line. The command-line forms:%dand:1,$ddo the same:%and1,$both mean "from the first to the last line". - Type
:wqand 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 asd2j).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 numberto 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/^#/ddeleted the two comment lines (lines starting with#), leaving 7 lines.:g/^$/ddeleted the empty line.:g/^\s*$/dalso deletes lines that contain only spaces or tabs.:g/log/ddeleted the two lines containinglog.: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:
xdeletes the character under the cursor;3xdeletes three.Xdeletes the character before the cursor.dwonserver_namedeletes the word and the space after it:example.com;diwonexampledeletes just the word:server_name .com;.dawalso takes the space:server_name.com;D(same asd$) onexampledeletes to the end of the line:server_named0deletes everything before the cursor on that line.dt.deletes up to the next., leaving.com;;df.deletes the.too, leavingcom;
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)
- Move to the first line you want to remove and press
V(whole lines),v(characters) orCtrl+v(a rectangular block). - Move with the arrow keys or
jandkto extend the selection. - Press
d. In the test,Vjjdon 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.