• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

How to delete lines in Vim / Vi

Thumbnail for Andrew CarlsonAndrew Carlson

Andrew Carlson

Software Engineer, Warp

Published: 12/1/2023

About Terminus

The Short Answer

  • dd deletes the whole line under the cursor.
  • 5dd deletes multiple (5) lines, starting at the cursor.
  • d$ deletes to the end of the line, starting at the cursor.
  • dG deletes all lines starting from the line under the cursor

However, those only work in normal mode. Ex mode has some more capable and interesting ways to remove lines!

Review of Vim Modes

Our tools make us superhuman. For programmers, that’s our editor. Vim’s main distinctive feature is that it’s a “modal editor,” which means it has distinct modes with different purposes and controls. Learning to use these modes is like having several knives for slicing different foods. You can use a chef’s knife for everything, but no chef would recommend that! You’ll prep much faster with a full set of knives.

When asking how to delete a line in Vim, the best answer depends on which mode you use. The best mode for the task will depend on what you’re deleting. Are you deleting the line the cursor is on? Are you deleting the first line to match a certain pattern? Some deletion tasks are a lot of work in normal mode but a piece of cake in ex mode. Let’s define the modes we’ll discuss here, i.e. the “knives in our set”.

  1. 1. Insert mode - This is the mode most similar to other text editors. It’s very literal. You press the “f” key and the letter “f” appears in the text.
  2. 2. Visual mode - This mode selects ranges of text. This is kind of like when you click and drag to select text in other programs.
  3. 3. Normal mode - This mode is all about using commands to do things based on where the cursor is. Each key no longer inserts the letter it represents, but represents a distinct command.
  4. 4. Ex mode - This mode doesn’t deal with the cursor too much anymore, but the file as a whole. It treats the file as a stream of text lines that you can transform with commands.

Deleting Lines in Insert Mode

Press: Backspace or Delete (a lot)

There are no surprises here. Deleting a line in insert mode is the same as deleting any other piece of text: mash the backspace/delete key until that line is gone!

Deleting Lines in Visual Mode

Press: Vd

Select the line, then press delete.” That’s what most people will think to delete a line. They might grab the mouse and drag it over the line to select it. Vim also has the idea of selection, though it can do it faster than other editors which use the mouse.

Vim does this with “visual mode”. The idea is that you enter visual mode from normal mode by pressing v and then selecting a section of text by moving the cursor over that text. Subsequent commands will apply to the selected text. The command to delete is d. So, you can move to the beginning of the line, press v, move to the end of the line, and press d. Altogether that might look like 0v$d. However, Vim provides a shortcut to select the line under the cursor: (capital letter) V! So, to delete the line under the cursor, you can press Vd.

For example, when the cursor is on line 5 and you press V to select it:

Thumbnail for

Pressing d deletes the selected line:

Thumbnail for

Deleting Lines in Normal Mode

Press: dd

You’ll spend most of your time in normal mode. It involves movement of the cursor as well as actions based on where the cursor is. Each key is a command rather than the literal insertion of a letter as in insert mode. d is delete. However, you would never just press d. Pressing d means delete something, but delete what? You need more keypresses to specify what to delete. This is the power of normal mode. You don’t just delete the character behind the cursor or the stuff that’s selected. You combine commands like d with movement for more powerful deletion. But what sort of things can we press after d? You can use any of Vim’s commands for movement:

Word navigation wWeEbB, e.g. dw to delete a word.

Character searching fFtT, e.g. df( to delete between the cursor and the next ( character.

Text object navigation }])

Line navigation 0^$

Buffer navigation gG

Alternatively, pressing d again (dd altogether) will tell Vim to delete the line under the cursor.

So, dd deletes the line under the cursor. There is more! In general, normal mode commands are repeatable by entering an integer before the command. For example, j moves down a line, but 10j moves down 10 lines. This flexibility applies to dd too. You can press 5dd to delete 5 lines, the first line being the one under the cursor.

For more sophisticated movement, you can rely on the other movement commands using d + movement. For example, to delete the line under the cursor plus 4 lines above you can press d4k. Or, to delete all lines from where the cursor is until the end of the file press dG. In this file, to delete the function pythagorean_theorem we can jump to line 7 with 7G and then press dG.

Thumbnail for

Note: For all these commands, you can replace d with c to enter insert mode after the deletion. How is this different from using d followed by pressing i? It makes the subsequently inserted text part of the same action as the delete, which allows both to be repeated by a dot repeat ..

Deleting Lines in Ex Mode

This mode is the most powerful and mysterious. It rarely deals with the cursor (though it can at times). You enter ex mode by pressing : from normal or visual mode. What follows is a range and a command. The range specifies which lines to apply the command to. Again, the d command means delete here. The simplest range is a line number. :15d means delete line 15. You can make it an inclusive range with a comma, so :15,18d means delete lines 15, 16, 17, and 18. Going back to the previous example. You can delete the pythegorean_theorem function with :8,10d without needing to move the cursor!

Thumbnail for

There are some special characters for certain lines. $ represents the last line in the file. . is the line under the cursor. '< and '> represent the first and last lines of the selected range if you entered ex mode from visual mode. So, :.,$d means delete the line under the cursor up to the end of the file, similar to dG in normal mode.

Using Deletion as a Means of Searching 🔍

How do these tools fit into the bigger picture of engineering?

If I was asked to find a needle in a haystack, I would light the haystack on fire. The hay would burn and the needle wouldn’t. The search becomes much easier when only the needle is left. However, I wouldn’t’ve thought of this approach unless I’d started a few fires before

Deletion is like burning something you aren’t looking for. Suppose you’re administrating a Linux server that uses Fluentd to collect database logs. The daemon is sending its logs to syslog among many other daemons on the server. You can view the logs in Vim with vim /var/log/syslog.

Thumbnail for

Suppose you don’t need to read any messages from cron or systemd. Sifting through the logs becomes much easier if you delete the log messages from those sources. An ex mode delete command can be the best solution! Rather than using line numbers to specify a range, you can specify a regexp pattern on which to apply the delete command. Do this with :g/pattern/d. So, since the relevant log messages include the process name, you can do :g/CRON\\|systemd/d to delete them!

Thumbnail for

Suppose we want to get even more specific, and only want to see log messages from our log-collecting service, fluent-bit. We want to delete all log messages that don’t match “fluent-bit”. You can negate a pattern with :v/pattern/d. So, by doing :v/fluent-bit/d we get a nicely filtered result.

Thumbnail for

Going Beyond Deletion

You’ll encounter many tasks that are made simpler with “smart deletion”. For example, maybe during development you added a bunch of print statements for debugging, and you need to remove them all before committing and pushing. Something like :g/console.log/d can make that very easy. The more you master your delete tools, the more opportunities you’ll find them useful.

Vim’s modes each give us unique ways to delete stuff ranging from simple to super stylish. The more ways you know to delete lines, the more creatively you can use these to do all kinds of things. The next time you find yourself using visual mode to delete a section of text, ask yourself if there is a better way to do it in normal mode or ex mode!

Written by

Thumbnail for Andrew CarlsonAndrew Carlson

Andrew Carlson

Software Engineer, Warp

Filed Under

Related Articles

Vim Find And Replace

You can use either the substitute command or the slash and dot method to search for patterns and replace with different values.

VimVi
Thumbnail for Glory KimGlory Kim

Vim Modes

Learn about seven of Vim’s modes

VimVi
Thumbnail for Razvan LudosanuRazvan Ludosanu

Vim / Vi Page Up and Down Controls

Quick reference for Vim's page up and down controls

VimVi
Thumbnail for Glory KimGlory Kim

Copy & Paste in Vim / Vi

Copy (Yank), Paste (Put) and Cut (Delete) in Vim

VimVi
Thumbnail for Jessica WangJessica Wang

Undo & Redo in Vim / Vi

Keyboard shortcuts and summary of how Vim tracks changes

VimVi
Thumbnail for Jessica WangJessica Wang

Show & Hide Line Numbers in Vim / Vi

Toggle absolute and relative line numbers

VimVi
Thumbnail for Amber ChenAmber Chen

Select all in Vim / Vi

Select all and copy, paste, or delete the contents

VimVi
Thumbnail for Glory KimGlory Kim

Searching in Vim

Search forward, backward, case insensitive, and more

VimVi
Thumbnail for Neeran GulNeeran Gul

Go To End of File in Vim / Vi

Select and delete to the end of a file in Vim

VimVi
Thumbnail for Preston Tunnell WilsonPreston Tunnell Wilson

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null