Home » Linux » Applications » Text Editors » Vim Deleting Lines

Vim / Vi: Deleting lines

This tutorial will teach you how to delete single, blank, or multiple lines in Vim / Vi by using a pattern or range. Vim is ubiquitous, and it’s everywhere. Learning how to edit, remove, yank, paste, and everything else can help you speed through tasks.

Before we get started, let’s turn on line numbers:

Vim - lorem ipsum
Vim – lorem ipsum
  1. Press ESC (pressing escape brings you into normal mode)
  2. Type :set number (brings you into command mode)
  3. Line numbers, voila!
Vim - show line numbers
Vim – show line numbers

Simple Deletion

The two basic usages of the d-shortcut, delete a single line or delete a single word.

  • #dd
  • dw

Let’s remove line 19.

vim delete

With the cursor on the line, a simple press of dd and it’s gone.

vim delete

Make a mistake? Simply press u, and vim will restore your previous action. Make a lot of mistakes? If you want to return to the initial state:

:undolist #Show all the changes made in this session
:undolist
:undolist
:undo 0 #Return to the start.

Next simple edit, let’s remove a word. With the cursor over a word, press dw.

vim dw

And Nullam is null (gone!).

Delete using vim

That’s it, the basics. Delete lines, delete words and undo any mistakes. If you feel you’ve really dug yourself into an odd inescapable situation, exit with :q!.

Multiple Line Deletion

If you need to remove a code module, remove specific variables, or scrub whole sections of a document out, vim has some options.

Delete [n] lines from the cursor

Say you have a module, and you know it’s 10 lines of code:

Delete [n] lines from cursor
Delete [n] lines from the cursor
Navigate cursor to the start of the block

10dd

There we go, 10 lines removed from the cursor.

Deleted [n] lines from cursor

Looks like I did my basic math incorrectly. One line leftover, let’s fix that.

Delete a range of lines

Deleting a specific number of lines is an odd use case. It’s trivial to do the math, but it’s easier to delete from line A to line B. In command mode, use :[from],[to]d.   Let’s remove the paragraph section from before.

Delete range of lines
Delete a range of lines
:61,71d

Deleted range of lines

There we go, removed the entire section. Want to remove everything?

  1. Press gg (Go to start of file)
  2. Press dG (Shift-g, case sensitive)

Want to remove everything including line [n]?

:10 #This takes you to line ten, typing 10 without the colon skips 10 lines
dG

There we go. Everything including the line we started at is removed. If you want to remove everything, including the initial line, to the top of the file.

:10
dgg

That’s it, the top section is removed.   I prefer using a range of lines instead of cutting from A to Z. Even when cutting the top of a log file, I know where I’m cutting from the top to the end. dgg or dG save some time but lose out on precision. Specificity is important. After all, running rm -rf * is great normally, but we really want to make sure we’re not deleting everything. My Lorem Ipsum file here has a lot of empty lines. Let’s remove them.

Delete Blank Lines

Navigate to the top:

:0

And remove all blank lines.

:g/^$/d

There we have it. Then, type in the following:

:g/^$/d

Pattern Deletion

Say you have a variable you don’t need anymore. Or are scrubbing data, and need to remove sensitive words and phrases. Vim allows you to pattern select and remove.

Remove Specific Words

Specific words are the same pattern substitution and deletion. We’ll use %s for substitution, and change out a word for a blank.

:%s/\<Nullam\>//g #Replace Nullam with any word or variable you are removing. Make sure to escape special characters.

That’s it! Removed that word. It’s easier to remove a whole line containing the word.

:g/Nullam/d

Will remove any line containing the word. g standing for global search, d for line deletion. Likely you’re not working with a block of text. Whole line deletion is usually a better use case. If you want to flip the script, and only leave lines containing the word:

:g!/Nullam/d

Will remove everything that does not contain the specified word. Finally, a little bit of clean up. After all of these removals, this is of course completely unusable. Let’s scrap the whole thing.

rm -rf /home/samberry/LoremIpsum

Conclusion

Now you know how to remove unwanted lines, variables, and any amount of fluff in your files. Go ahead and conquer, flaunt your skills.

SHARE:
Photo of author
Author
I'm a writer, in the sense that there are words written and things needing explaining. Years of schooling, running on twelve now, taught me one thing, I like taking the complicated down to complex. So, I'm writing about Linux. One of those things that starts as complicated, and after a few years turns into complex. Until the next new thing rolls out anyways. Working in IT, I learned advocating for your product is the only way to ensure adoption. Providing user manuals, pictures, diagrams, and everything else possible to our community. That's what builds the "user-friendly" experience. Not only design, but inclusion. Talk to me about Linux, I'm here to learn by teaching.

Leave a Comment