How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Delete All Lines in a File with Vi or Vim
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Server Management > How to Delete All Lines in a File with Vi or Vim
Server Management

How to Delete All Lines in a File with Vi or Vim

how7o
By how7o
Last updated: May 22, 2026
4 Min Read
Delete all lines in Vim with :%d
SHARE

To delete all lines in a file using Vi or Vim, type :%d in normal mode and press Enter. % stands for the whole file, d for delete. The longer form :1,$d means the same thing — line 1 to the last line, deleted.

Contents
  • The shortest form
  • How it parses
  • Equivalent forms
  • Deleting a range, not the whole file
  • Save the empty buffer
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on Vim 9.1 and BSD vi. Originally published 2022-06-19, rewritten and updated 2026-05-17.

The shortest form

:%d

Press Esc first to make sure you’re in normal mode (not insert mode), then type :%d and hit Enter. The buffer is now empty. Save with :w to commit the change to disk.

How it parses

  • : — enters ex (command-line) mode at the bottom of the screen.
  • % — line-range shorthand for “the whole file” (equivalent to 1,$).
  • d — the delete command.
Vim delete all lines — :%d, :1,$d, ggdG, range variants

Equivalent forms

:%d           " shortest — % means "whole file"
:1,$d         " explicit — line 1 to last line
ggdG          " normal-mode: jump to top, delete to bottom
:%d _         " same as :%d but discards the deleted text (black-hole register)

ggdG is the keyboard-only version (no colon mode). :%d _ is the safe version that doesn’t clobber your yank register — useful if you’ve already yanked something you want to paste later.

Deleting a range, not the whole file

The :RANGE d form works with any line range. Examples:

:5,10d        " delete lines 5 through 10
:.,$d         " delete from current line to end
:1,.d         " delete from start to current line
:5,$-2d       " delete from line 5 to the second-to-last line (keeps last 2 lines)

Save the empty buffer

:w            " write the (now-empty) buffer to the file
:wq           " write and quit
ZZ            " normal-mode shortcut: same as :wq

Until you write, the file on disk is unchanged. If you change your mind, :q! abandons the empty buffer without saving and the file stays as it was.

Frequently asked questions

What does the % in :%d mean?

In ex (command-line) mode, % is shorthand for the entire file — equivalent to 1,$ (line 1 through the last line). It’s the same % you use in :%s/foo/bar/g for a file-wide search-and-replace.

Why use :%d _ instead of :%d?

Both delete every line. The difference is the register: bare :%d copies the deleted lines into the unnamed register ("), which means a later paste will dump the entire old file. :%d _ sends the deleted text to the black-hole register _, discarding it. If you have something useful in your yank register and want to keep it, use :%d _.

Is there a way to delete all lines without entering ex mode?

Yes — in normal mode, ggdG does the same thing. gg jumps to the first line, dG deletes from the cursor to the last line. It’s three keystrokes and a habit-builder if you’re learning vim motions.

How is this different from echo > file.txt at the shell?

echo > file.txt truncates the file from outside vim, but if you have the file open in vim, vim still holds the old contents in its buffer — saving from vim will overwrite your shell truncation. To empty the file from inside vim, use :%d and then :w to write the now-empty buffer to disk.

Related guides

  • How to Check the Linux OS Name and Version from the Command Line
  • How to Zip Multiple Files and Directories in Linux
  • How to Add and Delete Users on a Linux Server from the Terminal

References

Vim manual: :help :delete, :help :range, :help registers. Online: vimhelp.org/change.txt.html#:delete.

TAGGED:BashLinuxvim

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article DataTables server-side Ajax pagination with Laravel How to Create Ajax-Based Pagination in DataTables
Next Article Disable and enable a form input with JavaScript and jQuery How to Disable or Enable an Input with JavaScript or jQuery
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

You Might Also Like

mysqldump all databases — export and import commands
Server Management

How to Export and Import All MySQL Databases at Once

6 Min Read
Install HandBrake CLI on Linux with Flatpak
Server Management

How to Install HandBrake CLI on Linux (Flatpak)

5 Min Read
Change SSH port on Linux — firewall, SELinux, sshd_config, systemctl restart
Server Management

How to Change the Default SSH Port on Linux

7 Min Read
Stop cron job output and the resulting email spam
Server Management

How to Stop Cron Output (and the Spam Emails)

5 Min Read
How7oHow7o

Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

  • Private by design — your files never leave your device
  • Instant — no signup, no install, no waiting
  • Always free — no trials, no watermarks, no paywalls

Tools

  • PDF Editor
  • Image Upscaler
  • JPEG Compressor
  • Password Generator
  • QR Code Generator
  • Video Trimmer
  • See all tools →

Pranks

  • Cracked Screen Prank
  • Fake Blue Screen (BSOD)
  • Fake Disk Format
  • Fake Low Battery
  • Fake Nintendo Switch Update Screen
  • Fake PlayStation Update Screen
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?