Navigating text

We've covered some basic movements (by characters, words, and paragraphs), but Vim supports a lot more options for navigation.

Check the following if you want some movement within the current line:

  • As you already know, h and l move the cursor left and right, respectively
  • t (until) followed by a character allows you to search the line for that character and place the cursor before the character, while T allows you to search backward
  • f (find) followed by a character allows you to search the current line for that character and move the cursor to the character, while F allows you to search backward
  • _ takes you to the beginning of the line and $ takes you to the end of the line
A word consists of numbers, letters, and underscores. A WORD consists of any characters except for whitespace (like spaces, tabs, or newlines). This distinction helps with more precise navigation. For instance, farm.add_animal(animal) is a single WORD, while farm, add_animal, and animal are individual words.

For free-form movement, you're already familiar with these bits:

  • j and l move the cursor down and up, respectively

  • w moves you to the beginning of the next word (W for WORD)

  • b moves you to the beginning of the previous word (B for WORD)

  • e moves you to the end of the next word (E for WORD)

  • ge moves you to the end of the previous word (gE for WORD)

  • Shift { and Shift } takes you to the beginning and the end of a paragraph

Here are some new free-form movement options:

  • Shift ( and Shift ) takes you to the beginning and the end a sentence

  • H takes you to the top of the current window, and L takes you to the bottom of the current window

  • Ctrl + f (or the Page Down key) scrolls the buffer one page down, and Ctrl + b (or the Page Up key) scrolls one page up
  • / followed by a string searches the document for a string and Shift ? to search backward

  • gg takes you to the top of the file

  • G takes you to the bottom of the file

This handy visualization is based on the Vim movement cheat sheet Ted Nailed published on his blog sometime in 2010:

You can also move by line numbers. To enable line number display, run :set nu, followed by Enter (or add :set number to your .vimrc file). Vim will dedicate a few columns on the left of the screen to display line numbers:

You can jump to a specific line by typing :N followed by Enter, where N is the absolute line number. For instance, to jump to line 20, you'll run :20 followed by Enter.

You can also tell Vim to open a file and immediately place a cursor at a particular line. For that, add +N after the filename when invoking Vim, where N is the line number. For example, to open animal_farm.py on line 14, you'd run $ vim animal_farm.py +14.

Vim also supports relative line movement. To move down N lines you'll run :+N and to move down you'll run :-N. You can also ask Vim to display line numbers relative to the current cursor position with :set relativenumber. In the following screenshot, our cursor is on line 11, and Vim displays the relative distance to other lines:

For example, you could tell Vim to move to the line containing def main(animals): by typing :+5, followed by Enter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.114.38