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:
- Press ESC (pressing escape brings you into normal mode)
- Type :set number (brings you into command mode)
- Line numbers, voila!
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.
With the cursor on the line, a simple press of dd and it’s gone.
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
:undo 0 #Return to the start.
Next simple edit, let’s remove a word. With the cursor over a word, press dw.
And Nullam is null (gone!).
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:
Navigate cursor to the start of the block10dd
There we go, 10 lines removed from the 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.
:61,71d
There we go, removed the entire section. Want to remove everything?
- Press gg (Go to start of file)
- 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.