Introduction to Deleting in Vim
In this lesson you'll learn how to delete items in Vim, including various ways that you can remove items from a page.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide, I'm going to walk through all of the various Delete options that you have available in Vim, and I definitely recommend for you to reference the cheat sheet, which I'm showing you right here.

large

Because as you can see, the number of Delete options is pretty impressive, so you have all kinds of various ways that you can use and delete items in the side of Vim. And also remember that this does also cut items, so it's not simply a matter of your traditional kind of delete or backspace behavior, this also gives you the ability to remove items and move data around files and perform various tasks, like that.

So, let's in this guide, we're going to get through a number of them and we'll see how far we can get in the time allotted. But I'm gonna start with the basics, so, we've already talked about how we can select items to delete so say that I want to delete this starting character, then the way that I can do that is by just pressing V and I can just select this item, press the D character and that has been removed.

I'll press Undo to get back, and then also, say that we want to, say we're in the middle of this and we want to delete everything from where the cursor is all the way to the end of the line. The way that you can do that is by pressing D and then the Dollar symbol.

large

So this may seem similar to C + W, except there are some subtle differences. First, this will actually put this on the Vim clipboard, but also, notice that this did not switch me into Insert mode. You use C + W whenever you want to switch into Insert mode. You use this one whenever you want to delete an item or you want to grab it in the clipboard and move it someplace else. So I could take that and come down and paste it right here, and we'd have access to it in other spots in the file, so that is D + Dollar sign.

Now, one way that helps me remember that is remember that dollar sign is our character for getting to the very end of the line, same thing with the D command. So that's another thing that, this took me a little while to get so if this part's fuzzy to you, do not worry, because it took me forever to really start to memorize all these commands and the thing that helped me more than anything was just practicing that.

I forced myself to build multiple applications using Vim and every single time I got stuck, I would go and I'd update my cheat sheet. So this cheat sheet that I am providing you, this is not something that I created for this course. This is actually something that I have used for a very long time on my local computer and every single time that I would find some other new way of accomplishing some type of behavior, I would come and I would put it right here so that I would remember it.

So you're not just looking at cheat sheet I wrote in a short period of time. This is actually kind of a compilation of a few years' worth of notes. So, that is how I can delete all the way to the end of a line.

Now what happens if, let me pick out a good word here, so, this word right here, it's not a real word, but, let's say that I'm in the middle of this word and actually, you know what? A better way of doing this would be, this is more practical for method names. So say that I have a method name right here of another_name.

Now I could press C + W and delete it that way, but I may not always wanna do that. So let's say that I'm somewhere in the middle of here, I can press D and then the little hat character and this is going to delete everything all the way from where my cursor is, all the way to the left. And I always forget the name of this one, just so you can see it. It's in the notes as well, but this is D and then the little, I've heard it called a little hat. I know there's a better name for it than that.

You can put it in the notes in the comments if someone knows it. But that is what you usually use for exponentiation and writing scientific papers and that kinda thing. So right here that is going to delete everything from where the cursor is, all the way to the left. So that's something that is pretty practical because there's many times where I'll be all the way to the right hand side and I want to delete everything to the left using the D and that little exponentiation character allows you to do that, I'm gonna reverse that.

Another way of doing that is, and this one may be a little easier to remember, is, D and then Zero and the reason why I usually will use that one as opposed to the other option, is because the other option looks more at white space, it's not completely identical. Where usually whenever I'm wanting to perform a delete like that, I usually want to go all the way to the left and the Zero character, if you remember, Zero takes you to the very beginning of the line, just like the Dollar symbol takes you to the end, so it's a little bit easier for me to remember just D + Zero deletes all the way from right to left, so that is how you can remove those items.

Now if you want to delete a word, but you don't want to switch into Insert mode, so say I'm right in the middle of this and I don't wanna type C + W 'cause C + W transfers me into Insert mode, I can actually type D + W and it will do the exact same thing. So, that's kind of a helpful tool.

large

It's something that I probably don't use quite as much as C + W, 'cause usually when I'm deleting something like this, I usually want to actually delete the word and replace it with another one, but that is something that if you find yourself deleting a lot of items, just kind of one word at a time like that, then that can be a handy way, and also note how with that case, I didn't have to switch into visual mode.

So, I didn't have to do something like V and then click all the way here and then press D, it's a lot more efficient just to press D + W and that removes that word, so, that's just kind of an alternate way of doing that. So, we've made it our way about halfway through the Delete commands, I'm going to take a break on the video and we're gonna come back and start getting into some of the more advanced Delete shortcuts, so I will see you in that video.