image
Exercise: Essential Navigation Commands
Exercise: Essential Navigation Commands
Difficulty
Beginner
Duration
42m
Students
461
Ratings
5/5
starstarstarstarstar
Description

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.

Transcript

The goal of this exercise is to practice moving around within a file. The first thing you want to do is open the nav.txt file that came in the course download. So the first thing you need to do is open a command line session on your machine and next use Vim to open the nav.text file. To do that navigate to the location of the file and just keep in mind that this might be slightly different for you depending on where you extracted the contents of the file. This example assumes the course download was saved into your downloads folder and extracted from there. So to get there just type CD downloads, Enter. CD vimclass, Enter. And them nav dot txt, Enter. Now you are going to practice moving the cursor by one line or one character at the time. Let's press the J key 10 times to move down 10 lines. One, two, three, four, five, six, seven, eight, nine, 10. Now press lowercase K four times to move up four line, so we'll just do one, two, three, that is four case. Now let's press the L key eight times, one, two, three, four, five, six, seven, eight. And that moves us to the beginning of the word, to. Now let's press the J key and you can see that the cursor is positioned on the next line, but it's also positioned at the beginning of the line because it's a blank line. However, if you press J again, you'll notice that you are the same distance over to the right or in the same column as you were a couple of lines ago. So when the cursor moves down it tries to maintain the same column position if it can. Now let's move the cursor to the beginning of the line by pressing the H key so that it repeats. So I'll just press and hold it, and now we're at the beginning of the line. Let's move the cursor to the very top of the file by pressing and holding the K key. Next let's practice moving the cursor by page, so hold down the Control key and press F as many times as it takes to get to the bottom of the file. Now, this is going to be different for everyone since everyone probably has a slightly different screen size that they are using. Now, let's just move up one page by typing Control and B. Next let's practice moving the cursor by word, the first thing we want to do here is place our cursor on the line that begins with far away. And one way to do this is just to press and hold the K key until you are at that approximate position and then you can fine tune it by pressing J or K to get on the proper line. Now let's press lowercase W four times, one, two, three, four. You'll notice that it stops at the first comma in the line. Now let's press lowercase W five more times, one, two, three, four, five. Again it stops at the comma. Now let's move back to the first word in the sentence which is, far, by pressing the lower case B eight times. And you'll notice that it stops at the comma, the cursor stops at the comma and you keep pressing B eight times and you are eventually placed back at the first word in the sentence. Now let's practice moving forward with the uppercase W, so let's press Shift + W and if we do this three times you'll notice that the cursor does not stop at the comma. We can continue moving to the word far by pressing uppercase W four more time. So we'll do one, two, three, it's going to skip the comma. That's a fourth uppercase W and it skips the comma and goes directly to the word far. Now let's return to the first word of this line by repeatedly pressing uppercase B. "B", uppercase B. It skips the comma B and B. And now we're back at the beginning of the line. Next up let's practice moving the cursor to a specific line. So let's go to the first line in the file by pressing GG. And now let's jump to the very bottom of the file by pressing shift + G, which is uppercase G. Let's go back to the first line by pressing one GG, and it does the exact same thing as GG, because by default, it defaults to the first line. So it's a little redundant so you don't wanna ever use one GG, just simply press GG. But if you forget that's okay, you can use one GG because that makes sense. Now, if you wanna go to line 29, we can type two nine GG and now we are placed on line 29. You can also do the same thing with uppercase G, so to go to line three we can do three shift + G and now we're online three. And again, we can use line mode to jump around as well. So we'll use colon four, four and press Enter, and now we're online 44. We can confirm this by pressing Control + G, and if you don't have the ruler option set it will show you what line you are on. And if you already do have the ruler option set you can look in the bottom right-hand corner of your screen and you see 44, which means you are on line 44. Now we can move to the last line of the file by typing colon dollar sign and if we press Control + G here you can see it displays 100% because we're at the very end or 100% of the way through the file. Okay, that brings us to the end of all the instructions for this practice exercise. However, I would like to encourage you to stick around in this file and practice some more navigation skills and just practice the commands you've learned on your own. The W, the B, the jumping to the line numbers, going to the top of the file, going to the bottom of the file and so on. So I'll leave that up to you to how much fun you're gonna have with that, but please do explore. And when you are all done and you don't wanna save any changes to this file, just type colon Q exclamation mark, hit Enter and you are done.

About the Author
Students
13927
Courses
61
Learning Paths
13

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.