How to delete lines in Vim / Vi

[#short-answer]The short answer[#short-answer

][.inline-code]dd[.inline-code] deletes the entire line below the cursor. [.inline-code]5dd[.inline-code] deletes several (5) lines, starting at the cursor. [.inline-code]d$[.inline-code] is deleted to the end of the line, starting at the cursor. [.inline-code]

  • dG[.inline-code] removes all lines starting from the line below 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]Review of Vim Modes[#review-of-vim-modes]

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

When asking how to delete a line in Vim, the best answer depends on the mode you use. The best mode for the task will depend on what you are removing. Are you deleting the line the cursor is on? Are you removing the first line to match a certain pattern? Some removal tasks are a lot of work in normal mode, but a piece of cake in ex mode. Let us define the modes that we will discuss here, that is, the “knives as a whole”.

  1. Insert Mode – This is the mode most similar to other text editors. It’s very literal. Press the “f” key and the letter “f” appears in the text.
  2. Visual mode: This mode selects text ranges. This is kind of like when you click and drag to select text in other programs.
  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 different command.
  4. Ex Mode – This mode no longer deals too much with the cursor, but with the file as a whole. Treats the file as a flow of lines of text that you can transform with commands.

[#deleting-lines-in-insert-mode]Delete lines in insert mode[#deleting-lines-in-insert-mode

] Press: [.inline-code]Backspace[.inline-code] or [.inline-code]

Delete[.inline-code] (a lot)

No surprises here. Deleting a line in insert mode is the same as deleting any other piece of text: smash the backspace/delete key until that line disappears!

[#deleting-lines-in-visual-mode]Line deletion in visual mode[#deleting-lines-in-visual-mode

]Tap: [.inline-code]

Vd[.inline-code]

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

Vim does this with “visual mode”. The idea is that you enter visual mode from normal mode by pressing [.inline-code]v[.inline-code] and then selecting a section of text by moving your cursor over that text. Subsequent commands are applied to the selected text. The command to delete is [.inline-code]d[.inline-code]. Therefore, you can move to the beginning of the line, press [.inline-code]v, move to the end of the line, and press d[.inline-code]. Altogether, that could look like [.inline-code]0v$d[.inline-code]. However, Vim provides a shortcut to select the line below the cursor: (uppercase) [.inline-code]V[.inline-code]! Therefore, to delete the line under the cursor, you can press [.inline-code]Vd[.inline-code].

For example, when the cursor is on line 5 and you press

[.inline-code]V[.inline-code] to select it:Pressing [.inline-code]d[.inline-code]

deletes the selected line

:[#deleting-lines-in-normal-mode]Delete lines in normal mode[#deleting-lines-in-normal-mode]Press: [.inline-code]

dd[.inline-code]

You will spend most of your time

in normal mode

. It involves cursor movement 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. [.inline-code]d[.inline-code] is delete. However, I would never just press [.inline-code]d[.inline-code]. Pressing [.inline-code]d[.inline-code] means deleting something, but deleting what? You need more keystrokes to specify what to delete. This is the power of normal mode. It doesn’t just remove the character behind the cursor or things that are selected. It combines commands like [.inline-code]d[.inline-code] with motion for more powerful removal. But what kind of things can we push after [.inline-code]d? [.inline-code] You can use any of the Vim commands for movement: Word navigation

[.inline-code]wWeEbB[.inline-code], for example, [.inline-code]

dw[.inline-code] to delete a word.

Search for [.inline-code]fFtT[.inline-code] characters, for example, [.inline-code]df([.inline-code]

to delete between the cursor and the next [.inline-code]([.inline-code] character.

Navigating Text Objects [.inline-code]}]) [.inline-code]

Buffer navigation [.inline-code]gG[.inline-code]Alternatively, pressing [.inline-code]d[.inline-code] again ([.inline-code]

dd[.inline-code] completely) will tell Vim to remove the line under the cursor.

Therefore, [.inline-code]

dd[.inline-code] deletes the line under the cursor. There’s more! In general, normal mode commands can be repeated by entering an integer before the command. For example, [.inline-code]j[.inline-code] moves down one line, but [.inline-code]10j[.inline-code] moves down 10 lines. This flexibility also applies to [.inline-code]dd[.inline-code]. You can press [.inline-code]5dd[.inline-code] to delete 5 lines, with the first line being below the cursor.

For more sophisticated movement, you can rely on the other motion commands using [.inline-code]d + movement[.inline-code]. For example, to delete the line under the cursor plus 4 lines above, you can press [.inline-code]d4k[.inline-code]. Or, to delete all lines from where the cursor is to the end of the file, press [.inline-code]dG[.inline-code]. In this file, to remove the function

[.inline-code]pythagorean_theorem[.inline-code] we can jump to line 7 with [.inline-code]7G[.inline-code] and then press [.inline-code]dG[.inline-code]. Note: For all these commands, you can replace [.inline-code]d[.inline-code] with [.inline-code]c[.inline-code]

to enter insert mode after deletion. How is this different from using [.inline-code]d[.inline-code] followed by pressing [.inline-code]i[.inline-code]? Makes the subsequently inserted text part of the same action as the delete, allowing both to be repeated with a period repetition [.inline-code]. [.inline-code].

[#deleting-lines-in-ex-mode]Deleting lines in Ex[#deleting-lines-in-ex-mode]

This mode is the most powerful and mysterious. It rarely deals with the cursor (although sometimes it can). To enter ex mode, press [.inline-code]:[.inline-code] 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 [.inline-code]d[.inline-code] command means delete here. The simplest range is a line number. [.inline-code]:15d[.inline-code] means deleting line 15. You can convert it to an inclusive range with a comma, so :[.inline-code]15,18d[.inline-code] means removing lines 15, 16, 17, and 18. Going back to the previous example. You can remove the [.inline-code]pythegorean_theorem[.inline-code] function with [.inline-code]:8,10d[.inline-code] without moving the cursor!

There are some special characters for certain lines. [.inline-code]$[.inline-code] represents the last line of the file. . is the line below the cursor. [.inline-code]'<[.inline-code] and [.inline-code]’>[.inline-code] represent the first and last lines of the selected range if you entered EX mode from visual mode. So

, [.inline-code]:.,$d[.inline-code] means to delete the line under the cursor to the end of the file, similar to [.inline-code]dG[.inline-code] in normal mode.

#using Using Deletion as a Means of Searching 🔍 [#using-deletion-as-a-means-of-searching]How

do

these tools fit into the larger engineering landscape?

If I was asked to find a needle in a haystack, I would set fire to the haystack. The hay would burn and the needle would not. The search becomes much easier when only the needle remains. However, I wouldn’t have thought of this approach unless I had started a few fires before

the removal is like burning something you’re not looking for. Suppose you are managing a Linux server that uses Fluentd to collect database logs. The daemon is sending its logs to [.inline-code]syslog[.inline-code] among many other daemons on the server. You can view logs in Vim with [.inline-code]vim /var/log/syslog[.inline-code].

Suppose you do not need to read any messages from [.inline-code]cron[.inline-code] or [.inline-code

]systemd[.inline-code]. Browsing logs becomes much easier if you delete log messages from those sources. An ex mode removal command may be the best solution! Instead of using line numbers to specify a range, you can specify a regexp pattern in which to apply the delete command. Do this with [.inline-code]:g/pattern/d[.inline-code]. Therefore, since the relevant log messages include the process name, you can do [.inline-code]:g/CRON\\|systemd/d[.inline-code] to delete them.

Let’s say we want to be even more specific, and we only want to see log messages from our log collection service, [.inline-code]fluent-bit[.inline-code]. We want to remove all log messages that do not match “fluent-bit”. You can deny a pattern with [.inline-code]:v/pattern/d[.inline-code]. So, by doing [.inline-code]:v/fluent-bit/d[.inline-code] we get a well-filtered result.

[#going-beyond-deletion]Going Beyond Deletion[#going-beyond-deletion]

You will find many tasks that are simplified with “smart removal”. For example, maybe during development you added a bunch of printing instructions for debugging and you should remove them all before committing and submitting. Something like [.inline-code]:g/console.log/d[.inline-code] can make it very easy. The more you master your removal tools, the more opportunities you will find useful.

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