Patricia Sauer - Authentic Programming

Navigating within text blocks in vi

an opened dictionary with most lines blurred but the word "focus" being sharp

We already learned how to navigate character by character, word by word, and within a line in vi and vim. To be able to move even faster, we now want to have a look at how to navigate within text blocks (sentences and paragraphs) and how to use the "go to" command to quickly navigate to a specific line as well as to the beginning or end of the file.

Moving to the beginning of a sentence

The end of a sentence in vi is defined as a period (.), question mark (?), or exclamation mark (!). The ( and ) commands allow you to move the cursor to the beginning of the current or previous sentence (() or to the beginning of the next sentence ()), respectively. This can be useful when you need to quickly edit or delete a sentence in your document.

Moving to the beginning of a paragraph

A paragraph in vi is defined as a block of text that is separated by a blank line. The { and } commands allow you to move the cursor to the beginning of the current or previous paragraph ({) or to the beginning of the next paragraph (}), respectively. This can be especially useful when you need to quickly navigate through a long document and need to move from one paragraph to another.

The "go to" command

The "go to" command can be used in several ways. We want to discuss these in this section.

Moving to a specific line

When you hear "go to" command, the most obvious usage might be going to a specific line. For example, if you want to move to line 50 in your document, you would enter the following command: 50 + shift + G.

In this regard, it is useful to know how to display line numbers in vi. As a short recap of the article: You can display line numbers for the current session only by typing :set nu or :set number in command mode. If you want to have line numbers displayed permanently (i.e. by default in each session), you need to modify your .vimrc file in your home directory by adding the following line "set number". It might be that your home directory does not contain a .vimrc file. In this case, you need to create this file yourself. If you read my articles about vi and vim until now, it shouldn't be a problem for you to use vi to create and modify this file 💪

Moving to the top and end of the file

The "go to" command can be used to navigate to the top of the file as well. To do so, you can use the following command: 1 + shift + G.

Additionally, you can use the "go to" command to navigate to the end of the file. To do so, you can use the following command: shift + G.

Recap

In this article we learned

  • that you can navigate to the beginning of the current/previous/next sentence by using the (/) command
  • that you can navigate to the beginning of the current/previous/next paragraph by using the {/} command
  • that you can navigate to a specific line by typing the desired line number followed by shift + G, e.g. 50 + shift + G
  • that you can navigate to the top of the file by using the 1 + shift + G command
  • that you can navigate to the end of the file by using the shift + G command
  • that you can display line numbers temporarily by using the :set nu or :set number command
  • that you can display line numbers permanently by adding set number to the .vimrc file in your home directory

You can check the following table to get another overview about the commands and their effects.

CommandResult
(Navigate to the beginning of the previous sentence
)Navigate to the end of the next sentence
{Navigate to the beginning of the previous paragraph
}Navigate to the end of the next paragraph
n + shift + GNavigate to line number n
1 shift + GNavigate to the top of the file
shift + GNavigate to the end of the file
:set nu / set numberDisplay line numbers in the current session

Watch it on YouTube

You can watch me explaining how to navigate within text blocks in vi and vim in this video:

Liked this article?

Buy Me A Coffee

© 2018 - 2024 Patricia Sauer