In this course, we look at the essential commands for navigating around a file and then we will work through an exercise that you can also follow along with. Then we move on to how to delete text in Vim, covering how to delete text through a range of commands, and then once again looking at a real-world exercise to give you a practical understanding of the concepts covered.
Learning Objectives
- Learn how to navigate around files
- Learn how to delete text
Intended Audience
This course is ideal for anyone who needs to edit files in a command-line environment.
Prerequisites
If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.
Resources
If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.
In this lesson we're going to talk about more editing techniques. Specifically you're going to be learning about deleting texts. First, I'm going to be opening the deleting lesson dot text file from the course downloads. You can follow along with me now or wait to practice during the next practice exercise or shocks if you're an overachiever, go ahead and do both. So right now, I'm going to navigate to where I extracted the contents of the course download archive. And for me, that's in my downloads folder and then my Vim class folder. And then finally, I'll go ahead and open up that the deletinglesson.txt file. You already know how to enter insert mode and add text to a file. The next most common editing task you'll find yourself doing is deleting text from a file. The simplest of the deletion commands is lowercase X. It deletes the character at your current cursor position. On the third line in this file, there's a spelling mistake, the word mistake should only have one K in it. So I'm going to move to one of the additional case and delete it. First off let's move down to lines by pressing j and j. I can move to the K by pressing L to move the character or lowercase or uppercase W to move over by word but I'm going to use the dollar sign to jump to the end of the line because that extra character is near the end of the line. So I like to do some of these larger jumps when possible especially when it makes me efficient. And the more you learn about them and the more navigating techniques you have you'll find yourself doing the same thing. So next, I just need to press the h key a couple of times to put my cursor onto one of the additional case, and now I can delete it by typing X or lowercase X. On the very next line there are more typing errors. First, I'm going to move down by one line by pressing j and I wanna fix the mistake starting at the beginning of the line because it will be easier for me to read the sentence left to right. So I'll just jump to the beginning of the line by pressing zero. So now I'll just press l until I'm at the letter I want to delete and then I'll hit x to delete that letter. And I'm just going to keep doing this until the line is fixed. So there's an additional letter, press x. There's an additional letter, my cursor is under it now, so I will press x and it is deleted. In addition to the lowercase X command you can use uppercase X. What this does is deletes the character right before or to the left of your cursor. So if you move down to the next line and place your cursor at the end, we'll do that with j and dollar sign, you're placed over the period. You can press uppercase X to delete the character that is right before, or just to the left of your cursor position, which in this case is D. So I'm gonna do Shift + X and D is gone. Now, if you keep doing this you'll just delete everything in the line and I'll just press and hold it until it's all gone but you'll notice that it does not delete the period. And that's not because it's a period or any specific character. It's just because that's the character that the cursor is under an uppercase X deletes the character that is to the left or before the cursor, so I will not delete the current cursor position. So if you wanna delete this last character that's under your cursor position, you would just type x to do so. The next line of text contains several repeated words. I'm just going to position my cursor at the first of the repeated words here. I'll just do j to go down one line and I'll just use w to move over to that repeated word. To delete a word, just use dw. You might not realize it but Vim is about to get really interesting and powerful from this point forward. This simple command demonstrates a pattern that will come up again and again with Vim. With this concept you can actually start to think in Vim if you will. If you break down this simple two character command you'll learn that it's comprised of an operator and amotion. The operator is d for delete. The motion is w for word motion. Remember that if you just press lowercase W, it will move the cursor to the next word. So dw means delete word. Note that the deletion starts where your cursor is. So if we were to move to the middle of the word and test our emotion with w, we can tell what will be deleted. So I'll just over if you will, to here and type w now you can tell what will be deleted. I'll just put my cursor back there with h and now if I do dw you can see what was deleted. So now, instead of trying to remember a new command for each and every possible thing you would like to delete, you just remember the pattern, operator, motion. Because you've already studied some of the navigation techniques, you know, several motions, let's test some of them out. What happens when you type dl with l being the move right one character motion? Well, it deletes the character under the cursor. Let's try it out dl, so the T was deleted and that was the character under the cursor. Notice that this is the exact same thing that lowercase X does. So you can think of x as being a shortcut for dl. Now let's try dh. This does the same thing as upper case X. What about dj? Let's do it here, d j, that deletes the current line you're on and the line below it because that is where the motion would have taken you. Let's do d k, I'll just move down a couple here with j and do d and k. So likewise d k we'll delete the current line you're on and the one above it because that is where the motion takes you, operator, motion. Going to go up one line here and just position my cursor in the middle of it somewhere and I want you to think about the character or the command that positions you at the very beginning of the line. Can you remember that? Right? It says zero. So to delete all the way to the beginning of the line from your current cursor position, just type d 0. And of course, you remember to move to the end of the line, you would use dollar sign. So to delete to the end of the line, use d $. Let me move down a couple of lines here. And just like x is a shortcut for dl, capital D is a shortcut for d dollar sign. So if I were to type capital D the entire line gets removed because that motion would be from the beginning of the line all the way to the end of the line. Let's do it on the next line. I'll go down here and I'll just position my cursor at this next word and do Shift + D for capital D and it deletes from the current cursor position all the way to the end of the line. Again, that's a shortcut for d dollar sign. This really brings us to another interesting point about Vim, and that is, there are often several ways to accomplish the exact same task. It's my job to teach you the underlying concepts that you can use to think clearly about Vim like this operator motion pattern. I'll also share common shortcuts with you as well but it really boils down to personal preference. Use whatever works for you, What's easier for you to use and obviously what you can remember. Okay, let's get back to the leading stuff. So one common task you'll want to do is delete the current line you're on and to do that use d d. So I'll just move down one line, for example and if I want to delete the entire line no matter where my cursor is I'll just type d d and that line is deleted. Going to move down my cursor here with a couple of js and let's say you wanted to delete three lines. Well to do that, you type 3 d d and those three lines are deleted. This brings us to another important and powerful Vim concept. If you proceed a command with a number, that command will repeat that number of times. So thinking back about the operator motion pattern, we can extend this to count operator motion. So to repeat the d w delete word command five times, you would type 5dw. If you don't specify a number before command it defaults to one time. So dw executes one time and deletes one word and it's the exact same thing as the Command + 1dw. Let's take a step back to motion. Let's say you want to move forward three words, to do that, you can type 3w, pretty cool, right? So think about the operator motion pattern. If 3w is emotion, it would stand a reason that you could use an operator in combination with that motion. So we can also delete three words by using d3w and it reads just like English, delete three words. So it's pretty easy to remember. So if you want to delete three words you can just run d3w as I just described or you can repeat the dw command three times by using 3dw. So just use whatever makes sense to you. What do you think would happen if we typed 2d3w? Well the command d3w would be repeated twice. So 2d3w deletes six words. If you wanna delete six words, you'll probably use 6dw or d6w but I wanted to show you the possibilities here. This is how you can think in Vim, as I like to say. And to bring up an earlier point it really doesn't matter if he used 6dw or d6w, just use whichever one that makes the most sense to you and that you'll remember. Let's get back to deleting lines. Let's say you want to delete several lines but you're not quite sure exactly how many lines you want to delete. Let me find an example in this file. I'll just Control + F here a few times and go down to this line that starts with "the big", and I'll just type z, press enter to move the text up my screen so we can view it a little better here. Now, if we look at this chunk of text here it's not really easy to see how many lines are in this paragraph. So you can delete the first line with d d here. I'll just do that, d d just like normal. Now, instead of repeating d d, until you're done you can use the dot command, the dot command repeats the previously issued command. To execute the dot command just type a period, you know, do that now and it repeats the previous command. So if you wanna delete this entire chunk of text, you can just keep typing period until you're done deleting texts. Okay? Let's say you're done working on this file. So you want to save your changes and quit. You already know that you can use colon w q, to do that all at once. But let's say you want to make sure your changes are saved before you continue editing for example. To do that, simply type colon w and press enter. Your file will be saved and you'll be placed back into normal mode where you can continue editing. When you're ready to quit, you can use colon w q to write and quit or if you don't really want to save your changes you can use colon q exclamation mark, as you already know. However I really haven't explained the exclamation mark earlier because I wanted to get you moving along quickly but while we're working on ways to think in Vim, I figured now is a good time to quickly cover it. Let me backspace here for a second and type a q and actually tell you that you can quit Vim by typing colon q. If there aren't any changes to the file, Vim will quit. However, if there are changes that haven't been saved, Vim will not quit but instead show you a warning message that says something along the lines of, no rights since last change. So let's escape here to get back to normal mode and let's make a change, move down to the next line and let's say, we want to delete three words. We'll do 3wd to delete three words. Great. So now when we type colon q and press enter, it says, hey, you have changes that you have not saved. By the way there are three main things an exclamation mark does in Vim. The first one is to force an action. You can think of colon q exclamation mark as force quit. For example, if you know, you've made changes to a file and you also know that you don't want to save those changes you can use colon q exclamation mark. However, if you want to make sure that your changes have been saved before you exit then just use colon q. If Vim quit at that point, you can be assured that all of your changes have been saved. As you may recall, we briefly saw how you can use an exclamation mark to toggle a Vim setting. You can also use an exclamation mark to execute external commands and we'll be getting into more depth for those uses of the exclamation mark later in this course. But for now, just remember that it can mean force a command. I don't want to save my changes. So I'm just going to use colon q exclamation mark and press enter. Okay. It's time to wrap things up for now. So whether you realize it or not you've learned a lot in this lesson. In addition to learning how to delete texts with a D operator, you started to see the power of Vim by easily repeating commands, by proceeding them with numbers and by using the dot command.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.