Navigating in Vim

30 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Being able to quickly and accurately move to where you need to be in Vim is an important skill, and one that only takes a little practice before you’ll be zooming to right where you need to be! In this hands-on lab, we will start with the basics of moving around in a buffer or file’s contents, getting to top/bottom, directly to a given line, or move around in large movements such as by screen or buffer. We’ll then move around by paragraph, on a line, and even down to moving around word by word, both forward and backward for all movements.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Move Around in Vim, Change the Cursor Line Positioning, and Navigate to the Buffer Boundaries
  1. Open the Vim editor:

  2. Create a working file:

    :help quotes
    :w ~/quotesdemo.txt
  3. When the file is written to disk, quit Help:

  4. Open the ~/quotesdemo.txt file in a buffer:

    :e ~/quotesdemo.txt
  5. Navigate to the top of the file:

  6. Navigate to the end of the file to see how many lines long it is:


    Note: Pressing Ctrl+G will also display the number of lines in a buffer/file.

  7. Navigate to line 100 of the file:

  8. Noting the percentage shown on the far lower right of the screen, navigate to 50% of the way down the file:

  9. To see if the top line of the screen is the start of a paragraph, press H and then k to move to the top line and move the display up one line.

    Note: If the cursor won’t go to the top of the screen, use the following to turn OFF the scrolloff option:

    :set scrolloff=0
  10. Reposition your cursor to the middle line of the screen by pressing M, then to the bottom line of the screen with L.

  11. Navigate to line 17:

  12. Press O (upper-case "o") to open a new line above the one that begins:

    Coming with a very GUI mindset...
  13. While in Insert Mode, try the various navigation commands you have tried so far, then use the dedicated PgUp, PgDn, Home, End and cursor keys, which is all Insert Mode really allows for navigation.

  14. Quit the demonstration file without saving changes:

Affect Line Display, Show the Control Characters in Your Files, Navigate on Lines and by Words and Characters
  1. Edit the demonstration file you created in Task 1:

    vim ~/quotesdemo.txt
  2. Navigate to the top of the file:

  3. Turn on the line numbers option:

    :set number
  4. Navigate to the first line of the fourth paragraph:

  5. Your cursor should be on the first character of the line that begins:

    Vim is so much better than vi...
  6. Now move your cursor to the end of the line by pressing the $ character.

    Note: Normally the end of a line and the end of a paragraph are the same thing, as a paragraph usually is a sentence or more ending in a Line Feed.

  7. Note that you are not going to the closing parantheses following the word Australia, as you would expect to. Look at the line numbers for a clue.

  8. Set the displaying of un-printable characters on the screen:

    :set list
  9. Note that the $ or LF (Line Feed) character is at the end of each of the lines, indicating the use of a manual ENTER to set the length of the line. This and line numbers tell the whole story.

  10. Place your cursor on the top line of the paragraph that begins with:

    Coming with a very GUI mindset....
  11. Turn the multiple physical lines in the paragraph into a proper paragraph by pressing the J character until there are no remaining $ characters in between Coming with and (Jose Fonseca).

  12. Now use the $ character to go to the end of the "line" you have just created out of five individual lines, then press the ^ character to go to the beginning of the new paragraph/line.

  13. Use the w character to move forward through the paragraph for 10 words, noticing how it treats punctuation as a word. Then use 5w to move forward five words at a time.

  14. Navigate backwards to the beginning of the paragraph using B, noticing how it treats the punctuation.

  15. Look up why this happens:

    :help B
  16. Make sure to read the definitions of what a word and a WORD are and the difference. Also note similar information about b, w, and W.

  17. Exit Help:

  18. Position your cursor at the beginning of the paragraph that starts with Coming with a very GUI.... and then use e to move to the end of the word, then several more, until you reach punctuation, noting where it stops, then use ^ to then repeat those steps with E, noting the (subtle) difference that makes.

  19. Return to the top of the file:

  20. Quit and don’t save the demonstration file:


Additional Resources

After a little while of using Vim, you get tired of using the dedicated PgUp/PgDn, Home, End, and Cursor keys repeatedly to move around while editing.

Having observed more experienced users apparently performing almost magical movements with just a few keystrokes, you decide to gain those skills for yourself.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?