How to Go to a Specific Line in Vim/Nvim with GIF Examples

Vim provides several navigational commands to quickly jump from one line to another. These commands allow you to go to the beginning and end of documents, specific lines, search for patterns, go backward, and more.

In this blog post, we will learn how to navigate to your desired line and explore basic to advanced navigation commands in Vim.

Basic Line Navigation Commands

Vim allows you to navigate quickly within a document. It supports several ways to go to a specific line, the first line of the document, the last line of the document, and even customize your navigation within the Vim editor.

Go to a Specific Line Number

You can quickly go to a specific line number in Vim. To do this, you need to first enable line numbers by entering the :set number command. After enabling line numbers in Vim, you can specify the line number you want to go to using the :<line number> command. Vim will take you to that line from your current position.

Let’s say you want to go to line number 7. Use the following command:

:7

This command will take you to line number 7.

Go to a Specific Line Number

Go to the First Line of the File

Vim allows you to go to the first line of a document from any position within the file. This command is straightforward. To go to the first line of the document, use the gg command.

gg
Go to the First Line of the File

Go to the Last Line of the File

You can also quickly go to the last line of the file using the G command. Here is an example of how entering G on the keyboard takes you to the last line of the document.

G
Go to the Last Line of the File

Vim allows you to move up or down a specific number of lines relative to your current cursor position.

For example, if you are currently working on line 10 and you want to move 3 lines up from your current position, use the 3k command:

3k
Navigating Relative to the Current Line

Similarly, to move 3 lines down from your current position, use the 3j command. The 3J command moves you down 3 lines from your current position:

3j

As a powerful text editor, Vim offers the capability to search for text and jump to matching positions. Using advanced commands in Vim, you can go to the next occurrence of a pattern, search for words, and even perform backward searches.

Using “/” to Go to the Next Occurrence

You can use the / command followed by your search term to go to the next occurrence of the pattern you are

searching for. To search for a word and go to its position, use the following command:

/{search term}

For example, if you want to go to the line in your GRUB configuration file where you can change the name of your distribution, use the following command:

/Arch

This command will take you to the next occurrence of the word “Arch”.

Go to the Next Occurrence

Utilizing “?” for Backward Searches

Similar to the previous example of forward search with the / command, you can also perform backward searches and return to the previous line. Vim provides the ? command followed by the search term, allowing you to search backward and go to the immediate matching occurrence.

For example, if you are on line 15 and want to search backward and return to the line where “Arch” occurs, use the following command:

?Arch
Backward Searches

Using “*” to Search for the Word Under the Cursor

The * command allows you to search for the word under the cursor and jump to its next occurrence.

Let’s say you are working with a configuration file and have uncommented some lines to enable certain features. Now, you want to go to the next occurrence of the word “uncomment” directly without searching for it. You can do it with the * command:

*

Here is an example illustrating this process:

Search for the Word Under the Cursor

Go to a Specific Word on a Line

In the previous section, we saw how you can go to a specific line. However, Vim allows you not only to go to a specific line but also to a specific word on that line. To go to a specific word on a specific line, use the following command:

:<line number>/<word>

Conclusion

Vim is a powerful editor, and you can utilize its “go to line” capability to boost your productivity. In this blog post, I have covered basic to advanced commands to go to a specific line, search for words, and navigate within your document. If you find it helpful, don’t forget to share it with your friends and colleagues.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

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