Vim Command to Delete All Lines

It might surprise you to learn that vim doesn't have complex commands like "delete all lines in a file." You might not be surprised if you've read a couple of vimshots already. 🥁

Instead, we'll string together a few simple commands to accomplish this. We will use "linewise-visual-mode" to select all of the lines in the file and then delete them. Enter "linewise-visual-mode" by pressing capital "V" in normal-mode. In "linewise-visual-mode" pressing "d" will issue the "delete" command and delete all of the text you've selected.


Note: pressing lowercase “v” in noraml-mode will send you to “visual-mode”. In visual mode you select character-by-character rather than line-by-line. You can still accomplish the task at hand in visual-mode but it’s a little less efficient.

The Setup

gg Jump to the Fist Line

Change Modes

V Start Linewise-Visual-Mode

Two & Done

G Jump to Bottom of File

d Delete Selected Text

Alternate Approach

It only takes 2 commands to delete all lines in the file. A motion "G" and an operator "d". If we enter the "motion" first and then the "operator", vim will apply the operator to the text that you just moved over.

I like to use "linewise-visual-mode" to highlight the text I'm about to delete but it is an optional step. If you like to work without a net, vim won't stop you. 😉


The Setup

gg Jump to the Fist Line

Don’t Blink

d Delete the Text I’m about to Move Over

G Move to the Bottom of the File