The course is part of these learning paths
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.
- Learn how to navigate around files
- Learn how to delete text
This course is ideal for anyone who needs to edit files in a command-line environment.
If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.
If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.
If you want to open an existing file, you supply that file as an argument to the VIM command. I'm going to open a file that came in the course download. You can follow along with me if you'd like. Now, if you do that, you'll probably have to pause the video from time to time. If you want to just watch and learn for now, that's fine too, because you'll get a chance to practice these skills on your own very, very soon. I happened to extract the contents of the course download in my downloads directory. So I'm going to navigate there with cd Downloads. Now when I extract the contents of the download, it created a VIM class folder. So I'll change into that with cd v-i-m-c-l-ass and press Enter. The file I want to open is called NAV.TXT so I'll run VIM, space N-A-V period TXT. By the way, I use command line navigation in combination with a relative path to the file but I could have also used an absolute path. For example, I could have typed VIM space /home/Jason/downloads/vimclass/nav.txt and it would have opened the same file. This isn't anything unique to VIM. That's just how it would work with any command. So at any rate, I'm gonna press Enter here at my vim nav.txt file command. Now you have the nav.txt file open. Let's practice some moving around in the file to view its contents. By the way, there are a couple of reasons why I'm teaching you more about navigation before getting deeper into editing skills. You may find you actually spend more time in vim moving around a file. For example, if you're a system administrator, you'll be working with lots of configuration files and many times you just need to go to a specific place in the file and make a small change or add a new line of configuration there. If you're a software developer and you have to fix a bug, you'll need to go to the piece of code you want to fix before you can even edit it. So in many cases, having good navigation skills increases your efficiency over advanced editing techniques. Okay, so on with the lesson. Now remember that when you start vim, you are placed in normal mode which is sometimes called command mode. The command to move the cursor down is lowercase J. So I'm going to press J to move the cursor down to the second line. As I keep pressing J, the cursor keeps moving down the line. To move up a line, press the K key. I'll make my way back up to the top of the file by repeatedly pressing the K key. To move the cursor to the right, press L. To move the cursor to the left, press H. If you want to move several positions but don't want to keep hitting the same key over and over again, you can simply hold it down and it will start to repeat. So I'm just gonna hold down J and as you can see it starts to move down the file. And if you want to return to the top of the file, you can press and hold down the K key. You'll soon see that the cursor starts to make its way up to the top of the file. If you want to make your way to the bottom of the file again, just press and hold the J key. Before we go any further, let's get a visual of this simple navigation technique. Here are three different visual representations of the movement commands we just covered. As you can see, H moves to the left, J moves down, K moves up and L moves to the right. You'll be able to remember these keys fairly easily. The H key is to the left, so it moves to the left. The L key is to the right, so it moves right. It's a bit of a stretch, but you can imagine J looking like a down arrow. You can also think of it as a fishing hook that goes down into the water. Finally, you're left with K, which moves up. I just want to point out that you can do pretty much anything in them without leaving the normal typing position. This not only holds true for these first few commands you're learning but it will hold true for all the commands and techniques that you'll learn. With that said, know that on most systems you can also use the arrow keys to move around the file. For example, you can think of the left arrow key as it being the same thing as the H key. The advantage to staying true to the VI and VIM philosophy of maintaining your normal typing position is primarily efficiency. Sure, you can move your hand away to the arrow keys but you can just as easily leave them in their normal position if you're willing to learn the VIM way. Another advantage of learning these commands is that they will always work. Even if the arrow keys don't work, the H, J, K, L commands will. Let's get back to navigation. The next two most common ways I'd find myself navigating through a file is by using Control+F to page down and Control+B to page up. Let me just hold down the Control key and press F. You can think of this as forward to help you memorize this key combination. If you keep holding down the Control key and press F again, you get another page down in the file. You can do the opposite, which is pressing and holding the Control key, and then typing B, which takes you back one page. Again, you can think of Control+F as forward or page down and Control+B as backward or page up. Let's look at the very bottom or end of this file. I'll just keep hitting Control+F until I'm there. There it is. You'll notice that there are several lines that just contained tildes. Those tildes aren't actually in the file itself. They represent lines beyond the end of the file. This particular file has one blank line at the end of it. That line does not have a tilde. Those tildes allow you to see exactly where the end of the file is. We've looked at how to move by a single line or character as well as how to move by page. VIM understands various text objects such as words, sentences, paragraphs, blocks, et cetera. We can move throughout the file using those objects too. To demonstrate this, I'm just going to move to the top of the file again. We'll just hold down Control and then press B until we're back to the top of the file. I'm actually gonna use K here to get to the very top of the file. Let's say that you want to move right one word. Well, to do that, just press W. It doesn't really matter what the word is. It can be English, it can be code. It can be something else entirely. A word in VIM is simply a sequence of non-blank characters. Note that VIM considers punctuation as a word with lowercase W. You can also move right one word by using capital W. So I'll press Shift and then press W for capital W. Capital W ignores punctuation and uses whitespaces word boundaries. To back up by word, we'll use the B key, that B. Now lower case B considers punctuation a word and capital B ignores punctuation and instead uses whitespaces word separators. Let me show you the difference between lowercase W and B and uppercase W and B. First I'm just going to move down to the line that starts with to move forward in the file, use Control+F. So I'm just going to press J and go here to this. I really want to redraw the screen, or really reposition the text within the view window here that I have. And so one of the ways you can do that is by pressing Z followed by Enter. So what this does is keeps your cursor in the current position but moves the text up on your screen. By the way, you may get a slightly different result if you press Z Enter on your system. And that depends on some configuration settings that we'll get into later when we talk about those in this course. At any rate, it's going to move the line that you're on, either at the very top of the page or near the top of the page while keeping your cursor at the same position. Okay, let's get back to talking about words. Now if I keep pressing lowercase W, you'll see that it eventually will stop at the comma and then also at the dash. I'll keep hitting W a couple more times and it stops at the period. Now, if we use lowercase B, it does the same thing. It stops at the same places. So it stops at the period, stops at the dash, it stops at the comma. So it's using these punctuation as well as spaces as word boundaries. Now, if we were to move forward with the upper case W, it doesn't stop at punctuation. So I'll do that Shift+W. Instead of this time it stopping at the comma, it actually goes to the word use. We'll do this again. Control starts there. And this time it's not going to stop at the dash. It's going to jump all the way over to the next space or just one position after the next space. In the same way with a capital B, so if we move back, it doesn't stop at the punctuation. It stops just after the word boundaries which it considers to be spaces. I'd like to point out something very interesting here. Notice how the lowercase command and uppercase command are very closely related. Lowercase W doesn't do the exact same thing as uppercase W but they're very similar. This is something characteristic of VIM and you're going to see this pattern again and again. I just wanted to plant that thought in the back of your mind so you'll be able to make this simple association when you see it again. And you'll be seeing this a lot, trust me. All right, let me reposition my cursor down the file a bit. I'll use Control+F and we'll go down here to this far, far away line. And again, I'm going to press Z followed by Enter to move the view up in my screen here. Now I'd like to show you how to move to the beginning of a line. So first off, I'm just going to use W a few times to move in a few words. And now if you press the number zero, you'll jump right to the beginning of the line. You'll notice that this line is indented. Let's move in a few words again by pressing W a couple of times here. if you want to go to the very first character in the line, use the caret symbol which is Shift+6 on my keyboard. If you're familiar with regular expressions, you know that the caret symbol represents the beginning. Also in a regular expressions, a dollar sign represents the end and that's exactly how you can position the cursor at the end of the line by typing the dollar sign. And I'll do that here just now. Shift+4 for me is the dollar sign and it places your cursor all the way to the end of the line. The last topic I want to cover in this lesson is how to jump to a specific line. In VIM, there are two ways you can execute the go-to line command. The first way is by typing lowercase G-G and the second way is to type a capital G. If you want to go to a specific line, then type that line number followed by either version of the go-to line command. So to go to line two, type 2gg, and now we're placed on the second line. I'll just move my cursor down a bit with J and then also demonstrate that two+Shift+G also works and it puts you on the second line. So to go to a line 11, I can type 11gg, or 1-1-capital G. So you might be wondering what the difference is between gg and capital G. Well, if you do not specify a line number, gg defaults to the first line in the file. So a really quick way you can get to the very top of the file is to type gg. I'll do that now. GG and we're at the very beginning of the file. Now if you don't specify a line number with capital G, it defaults to the last line in the file. So you can quickly get to the bottom of the file by pressing uppercase G. So I'll do that now, Shift+G and we are at the very end of the file. So you can use lowercase gg or uppercase G in command mode to move to a specific line. You can also use line mode to jump to a specific line as well. First type colon here to get into line mode and next type the line number you want to jump to and press Enter. So to go to the first line in the file, you can type :1, press Enter and you're jumped right to the very first line in the file. And if you want to go to line five for example, we can type :5, press Enter and now your cursor is positioned at line five. Now you can use that with any number in the file. So just colon, a line number, press Enter and your moved to that position in the file. You can also use this line mode to jump to the end of the file without knowing exactly how many lines are in the file. And to do that, you would type colon and dollar sign. Again, if you know regular expressions, you know dollar sign means the end. So this means jump to the end of the file. I'll press Enter and we are indeed placed at the end of the file. If you want to know how many lines are in a file, you can press Control and G. This displays the file name and the file status. For example, if it's modified, or if the files read only, it also displays the cursor position which includes the current line you're on as well as how many total lines there are in the file. You'll also notice how far you are down a file as a percentage. For example, if you're halfway down a file, you'll be at 50%. And if you're on the last line, you'll see 100%. Finally again, VIM tells you what column your cursor is in. By the way, the information is good at the time you type Control+G and it doesn't automatically update. So if we were to move the cursor up a couple of lines, you'll see that Control+G still says we're online 74 of 74. But that's not true. So we'll press Control+G again and now it shows that we're online 67 of 74 and we're 90% through the file. You can even get more detailed information with G Control+G. Now you'll get information like character count and word count. Depending on your configuration, you might have already had the cursor location information on your screen. VIM has an option called ruler that can be turned on or off. To turn the ruler option on, use :set ruler and press Enter. Now the line and column number appear on the lower right-hand side of your screen. Here you can see 67,1 which shows that we're on line 67 in the first column. Now if you have this set ruler option set, you actually see slightly different information when you press Control+G. So if we were to press Control+G now, you can see that it displays 74 lines instead of 67 of 74 lines. And that's because the ruler option is set and that information is already displayed. So if just a minute ago when you type Control+G and you saw this, that means you had the ruler option set. By the way, the set command is used to control VIM options. The ruler option is either on or off. So set ruler turns on the ruler. To turn off the ruler, we can use set no ruler and press Enter. This is a pattern by the way. For options that are either on or off like ruler, use :set and the option name to turn on the option and :set no option name to turn off the option. Instead of explicitly turning an option on or off, you can append an exclamation mark to the end of the option to toggle it. That means if we use :set ruler!, that means it will toggle the ruler on and off. So now the ruler is on and if we do this again, ruler!, the ruler is off. One more time, toggle. Now it is back on. As we go throughout the course, you'll be learning more about the set command and the options that VIM has available for you. But the important points to remember in this lesson is the navigation. And in this lesson, you learned the several commands for navigation. You learn how to move by a single character or line with the H, J, K and L keys. You also learned how to move by word by using lowercase w and b as well as uppercase W and uppercase B. You also learned how to move to the beginning of a line using zero, as well as the first character of a line with a caret symbol. You learn how to jump to the end of the line by using the dollar sign. You also learn how to jump to a specific line in a file first by using the go-to line command which is gg or uppercase G. gg defaults to the first line and capital G defaults to the last line in the file. You also learn how to use line mode to jump to a specific line with colon followed by a line number. You learn how to go to the last line of the file even if you don't know the line number with :$ Enter. Finally, you learned how to display information about the file you're editing and your current cursor position by using Control+G.
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.