Skip to content

Linux Foundations / Find your way around Linux ยท Step 51 of 63

Lesson: Vim NavigationSwitch
โ† Linux Foundations

Shell and files

Vim Navigation

Move around a file in Vim by characters, words, lines and screens.

Good Vim users rarely hold down an arrow key. Normal mode has commands that jump straight to where you want to be. Press Esc before trying any of them.

Single steps

  • h moves left
  • j moves down
  • k moves up
  • l moves right

Arrow keys usually work too, but these four keep your hands in place.

Moving by words

  • w goes to the start of the next word.
  • b goes back to the start of a word.
  • e goes to the end of a word.

With the cursor on the first letter:

text
restart the web server
^ press w
        ^ you are on "t" of "the"

The capital versions W, B and E treat anything between spaces as one word, which helps with paths such as /etc/ssh/sshd_config.

Within a line

  • 0 goes to the first column.
  • ^ goes to the first character that is not blank.
  • $ goes to the end of the line.

Across the file

  • gg goes to the first line.
  • G goes to the last line.
  • 42G or :42 goes to line 42.

Line numbers are handy when a program reports an error "on line 42". Show them with :set number.

By screens

  • Ctrl-f forward one screen, Ctrl-b back one screen.
  • Ctrl-d down half a screen, Ctrl-u up half a screen.
  • H, M, L move to the top, middle and bottom of the visible screen.

Two more jumps are worth knowing. { and } move by paragraphs, which are blocks separated by blank lines. % jumps between matching brackets.

Counts

Put a number before a motion to repeat it. 5j moves down five lines and 3w moves forward three words.

After a big jump, such as G or a search, press Ctrl-o to go back to where you were. This is a Vim feature, so other vi programs may not support it.

Motions also combine with editing commands. For example, d followed by w deletes up to the next word. Learning movement now pays off twice.

Try it

  1. Make a long file: seq 1 200 > numbers.txt.
  2. Open it with vim numbers.txt.
  3. Press G, then gg.
  4. Type 100G, then 10j. You should be on the line that reads 110.
  5. Press Ctrl-d and Ctrl-u and watch the screen move.
  6. Quit with :q.

For a live shell to practise in, there is the shell navigation lab.

Check your understanding

Vim Navigation

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.