Advanced Delete Commands in Vim
In this guide we'll walk through advanced commands you can run to delete items in Vim, including how to remove all the comments from a page in a Rails application with a single command.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So we've made it about halfway through the delete commands in Vim, and hopefully you can kind of see that, not only is Vim a great editor in terms of giving you efficiencies, but it also gives you all kinds of various options for performing various tasks. For example, I've shown you a number of ways that you can delete items. You could delete something from the beginning to the end by pressing, if I start at the beginning, I could press D and then $ and that deletes everything. I could also simply press Shift+V and then Delete, and that also accomplishes the same goal.

So much like Ruby programming, there's not really one way of doing everything in Vim. You have your own options, and that's one thing I like about it. I know developers that use Vim religiously, and when we talk about how we perform various tasks, it turns out we do very different commands. We run very different commands, and it's perfectly fine.

What works for the other developer, it fits in with their workflow, and what I do fits in with what I like to do. And by the way, I'm also always looking for better and more efficient ways of doing the various tasks that I do in Vim. So that's part of the reason why I like to talk to developers about it.

So, continuing on down the list, let's go down. We've talked about how we can delete an entire line, also, by pressing V+V and then we can paste that anywhere since deleting is also copying, which is just really cutting.

Now a very cool thing that, this is something that I use quite a bit, is say that you're on this line right here, we're on line 18. If I want to remove this line 19 so that 18 is actually connected to 20, I could do something like this where I could come down, switch into insert mode, and then come up and hit Delete.

I also could just hit D+D, and that would accomplish the same goal, however, there is an even better way of doing this. If I'm on a line right here, like this, and press Shift+J, this will do the exact same thing for me, but I didn't even have to change lines.

large

Once again, Vim gives you the flexibility to be as efficient as you want to be. So here we didn't even have to switch lines, I pressed two keystrokes and it went down and removed that line, brought it up, and now those two items are connected, and also notice it kept me in command mode. That's something that I haven't really addressed a ton, but hopefully you kind of noticed it as I have navigated though these files is, you want to be in command mode as much as humanly possible, especially when it comes to editing.

If you find yourself in insert mode constantly, then you may not be taking advantage of a lot of the different shortcuts that Vim offers when it comes to editing, deleting, changing items, that kind of thing, just something to keep in mind. I'm not saying you have to be Vim master right from day one, it takes a lot of practice, and one trend that I've noticed is back when I was learning Vim, I was always in insert mode.

I was always here and any time I wanted to edit something, I was using my arrow keys and that kind of thing, and it's fine to do that, especially if you are first starting off, but I think that you'll find the more practice you get with it, the more time you actually spend in command mode just like this. So, just another little side note when it comes to Vim best practices.

So now if I want to get everything done, to switch out words, we've talked about how we can use C+W and how we can also use things like Shift+C or capital C, remember that both of those items delete everything, but they also switch you into insert mode. So that's really the main difference between using C and using D.

Now another thing that, this is something that I use quite a bit, is say that you want to delete this entire line right here, and this is gonna show exactly why I use relative line numbers. So right here, if you remember when I talked about setting relative line numbers, it shows our line number that we're on here but then everything else it relative. So in other words, it shows one to three, which means that this line here is three away from where I'm at.

large

Now if you were curious on why I did that, the reason comes down to how I like to delete items. Yes, I could very easily go and press Shift+V and come down right here, that's fine. However, there are many times where I want to pull in much more than four lines of text, and I may have 20 that I want to pull in and what I can do is, because I have relative line numbers, the D command actually takes an argument.

So, I can do something like this. I can say D 3 hit Return, and it deletes those three lines.

large

So that's something that I find incredibly helpful, and that's the exact reason why I have relative line numbers, because by doing this, I can see exactly how far, in relation to where I'm at right now, all of these other lines are. So here if I want to pull in all of this content plus the method name, I can just do D 7 hit Return, and that's gotten rid of everything for me. I come down to the very bottom here and then if I come down a few more line, hit paste and press the keyword, each one of those items is now here.

So I find that to be a pretty effective way of being able to delete items and to move them throughout the other parts of the application, and that's something that I do a lot when I'm developing and when we actually get into some of the Ruby development things, the advanced Ruby development, you'll see that I use this quite a bit, and that is the reason why I use those line numbers.

So that is everything on that side, now let's talk about how we can delete all items in a application that start with a character. So this is gonna start to kinda get us into seeing the way that Vim leverages regular expressions and different things like that. I'm going to, actually, save and quit out of this file, and I want to show you a different kind of file. So I'm going to, very quickly, create a Rails application. So this is gonna be just Rails new Vim demo, and we're not gonna be building a Rails application.

This is a Vim guide, but I want to show you some practical ways that I use Vim's deleting capabilities here. So one thing that I hate in Rails is all of the comments that they give. So if I come into the Rails application and I open up the Gemfile right here, you can see I have all kinds of comments, and this is fine if you're brand new to rails, but these really, I do not like seeing them. I know what byebug is there for, and I know what Capistrano is there for, and I don't like them kind of giving those placeholders.

large

I understand why they do it, it's good for new students, but it's not something that I personally want to have in there. So one of the first things I do whenever I get a new Rails application, is I will run this command. So what this command is, and I have it included inside of the cheat sheet, it's in the delete section, and it talks about how we can get rid of a certain pattern.

So I'm gonna say Command, which is :g, and then follow that by a / , and then follow that by whatever pattern I want. So, in this case, I just want it to be the hash character followed by another slash, followed by the letter D.

:g/#/d

Now if I hit return on this, you can see that that went through, and it found every single line in the file that had the hash sign in front of it and it deleted it.

large

So this is something that you can do in any kind of language. Vim didn't care that this was Rails, or Ruby, or anything like this. If this was a HTML file and you wanted to get rid of all the comments, you could pass in the start of the way the comments are patterned there, and just pass the pattern in, and it's just regular expression matter.

We could have passed anything in. We could have passed Gem in, and it would have deleted every, single thing here. It's just kind of the way that it works. It's just looking at regular expressions, and that's all it cares about.

So that is a list of some more advanced ways that you can use the delete command inside of Vim along with some practical examples on how you can do it in real-life files.