A brief introduction to Vim (Part 2)

 
(Workflow & Setup)
 

How to navigate the cursor and scroll in Vim


About

A table of contents of this series can be found here.
A summary of Vim can be found here.
Here is the first (previous) part of this series: A brief introduction to Vim (Part 1)
Here is the third (next) part of this series: A brief introduction to Vim (Part 3)

Navigation

This section will be about navigating within a file in Vim.

As we learned in the first part of this post series, Vim offers a mode for navigating within the file (the normal mode).

Here is a cheatsheet of what action the keys perform but I will go though the most of it with explaination in this post:

Vim Cheatsheet

Basic cursor navigation

While Vim's default configuration enables you, to navigate the cursor within the file using the arrow keys ( ) on your keyboard this is actually not efficient and recommended, because your right hand has to leave the home row of the keyboard, it is usually sitting on.

And that is the reason why you can and should use the following keys to navigate:

  • h to move the cursor one character to the left (equivalent of )
  • l to move the cursor one character to the right (equivalent of )
  • k to move the cursor one line up (equivalent of )
  • j to move the cursor one line down (equivalent of )

Using these keys to move the cursor will feel quite odd in the beginning and to be honest I focused on other things to pick up at first aswell. But now I wouldn't want to go back to using the arrow keys ;).

Advanced cursor navigation

There are a few keys to navigate to certain positions within the file:

  • Press w (lowercase) to place the cursor at the beginning of the next word (Note that e.g. - counts as a separator)
  • Press W (uppercase) to place the cursor at the beginning of the next WORD (Note that here a word counts as anything as long as it is not separated by a space character)
  • Press b (lowercase) to place the cursor at the beginning of the previous word (Note that e.g. - counts as a separator)
  • Press B (uppercase) to place the cursor at the beginning of the previous WORD (Note that here a word counts as anything as long as it is not separated by a space character)
  • Press e (lowercase) to place the cursor at the end of the current word (Note that e.g. - counts as a separator)
  • Press E (uppercase) to place the cursor at the end of the current WORD (Note that here a word counts as anything as long as it is not separated by a space character)
  • Press 0 to place the cursor at the beginning of the line
  • Press ^ to place the cursor at the first non-space-character of the line
  • Press $ to place the cursor at the end of the line
  • Press :10 followed by the ENTER key to jump to the 10th line of the file
  • Press gg to place the cursor at the first line of the file
  • Press G to place the cursor at the last line of the file (the number after the colon determines to which line you jump)
  • Press H to place the cursor at the first line visible on the screen (H for High)
  • Press M to place the cursor at the center line of the visible screen (M for Middle)
  • Press L to place the cursor at the last line visible on the screen (L for Low)

Scrolling (Screen navigation)

Your file may have a total of 100 lines but your Vim window might only show you 30 of those 100 lines on the screen.

You can move the screen to display a different part of the file (scroll) with the following commands:

  • Press CTRL+E to scroll down by one line
  • Press CTRL+Y to scroll up by one line
  • Press CTRL+F to scroll one whole screen down
  • Press CTRL+B to scroll one whole screen up
  • Press CTRL+D to scroll a half screen down
  • Press CTRL+U to scroll a half screen up

Ask me questions

I will be happy to answer your questions in the comments section below. Also let me know if you have any tips or ideas for me to improve my post!


Write a response

Your email address will not be published. Required fields are marked *