vi/vim FAQ: Can you share some example "vi delete" commands?
The vi editor can be just a little difficult to get started with, so I thought I'd share some more vi commands here today, specifically some commands about how to delete text in vi/vim.
A lot of times all people need is a quick reference, so I'll start with a reference of vi/vim delete commands:
x - delete current character dw - delete current word dd - delete current line 5dd - delete five lines d$ - delete to end of line d0 - delete to beginning of line :1,.d delete to beginning of file :.,$d delete to end of file
If those don't make sense on their own, the next sections will provide a brief description of each vi delete command.
When you're in command mode in the vi editor (just hit the [Esc] key and you'll be there), and you want to delete the character at the current cursor position, just use the vi delete character command -- the lowercase letter 'x' -- like this:
x
As mentioned, that deletes the character at your current cursor position. If you want to delete ten characters you can press the letter 'x' ten times, or you can use this command instead:
10x
When you're in command mode in the vi editor and you want to delete the current word, use the vi delete word command:
dw
Just like the delete character command, if you want to delete the next five words, just precede the dw command with the number 5, like this:
5dw
When you're in command mode in the vi editor and you want to delete the current line, use the vi delete line command:
dd
Just like the delete character and delete word commands, if you want to delete the next five lines, just precede the dd command with the number 5, like this:
5dd
To delete all the characters from your current cursor position to the beginning of the current line, use the d0 command, like this:
d0
Similarly, to delete from your current cursor position to the end of the current line, use the d$ command, like this:
d$
Whenever you're working with vi, the letter '0' typically refers to the beginning of the current line or the beginning of the file, and the '$' character typically refers to the end of the current line or end of file, so I find these commands fairly easy to remember.
To delete all the lines from the beginning of the file to your current cursor position, use this command:
:1,.d
That command can be read as "From line 1 to the current position, delete", or if you prefer, "Delete from line 1 to the current line position."
Similarly, to delete everything from the current line to the end of the file, use this vi delete command:
:.,$d
Again, that can be read as "From the current position to the end of file, delete."
If you'd like to see many of these vi/vim delete commands put to work, I've created this vi/vim delete commands video tutorial on YouTube.
I hope these vi delete commands have been helpful. If you have any questions, comments, or other vi commands you'd like to share, just use the comment form below.
Thanks
Thanks for the vi tips.
Thank you.
Cheers for these deletion tips, I couldn't work out how to delete everything (current line down).
Thank you
5 Stars for deleting shortcut :)
Thank's Alvin !
Useful
A shortcut for this:
:.,$d
can be:
dG
but I don't know if there is a shortcut for :1,.d
Great tips. Just helped save
Great tips. Just helped save me a lot of time while looking through some log files. Was able to delete all the junk and just keep what mattered.
Post new comment