Patricia Sauer - Authentic Programming

Moving the cursor by single characters in vi

several people moving on a public open space

Oftentimes, you will be editing existing text instead of adding new text or writing new files. In command mode you can move the cursor to the desired position before switching to insert mode to edit your file.

Single character movements

As we already know, in vi, single character movements are possible using the arrow keys. So let's have a look at some other key mappings for moving the cursor:

CommandResult
hMove the cursor to the left
jMove the cursor down
kMove the cursor up
lMove the cursor to the right

When you cannot go further in one direction, a beep will occur. For example, it is not possible to move to the left/right to wrap around to the previous/next line. In this case, you need to use j/k to navigate to the below/above line. Additionally, it is not possible to move beyond a tilde.

Multiple character movements

As we don't just want to move the cursor character by character but want to become pros using vi and want to be faster than our colleagues. Let's have a look at how we can multiply character movements and become faster 🏃🏻‍♀️

Movement commands can be preceded by numbers to multiply the movement. For example, it is possible to type

CommandResult
4hMove the cursor 4 characters to the left
2kMove 2 lines up

Recap

In this article we learned that

  • we can move the cursor using the keys h, j, k, and l to move the cursor to the left, down, up, and to the right
  • we need to be in command mode to move the cursor using the keys h, j, k, and l
  • we cannot wrap around to the previous/next line by using h or l
  • we have to use j or k to move to the previous/next line
  • character movements can be preceeded by numbers to multiply the movements like "2k" for moving two lines up

Watch it on YouTube

You can watch me explaining how to navigate by single characters in vi and vim in this video:

Liked this article?

Buy Me A Coffee

© 2018 - 2024 Patricia Sauer