How to Go to a Specific Line in Vim? [All Cases]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Vim is a text editor for Unix-based OSes like Linux. Users can create new files, open existing ones, or modify them to update using Vim. For efficient editing, it is crucial to go to the intended line of the file easily, especially for larger files. This article will demonstrate some approaches to going to a specific line in Vim and performing necessary tasks.

Moving Through a File in Vim

Before jumping to the detailed methods, here are some shortcuts to go to the beginning or end of a document through Vim. Remember to switch to the normal mode by pressing ESC before applying them:

Option Function
G Moves to the start of the last line of the file.
]] Moves to the end.
[[ Moves to the start.
Ctrl + Home Shows the top portion of the text.
Ctrl + End Shows the bottom portion of the text.
:1 Moves to the first line.
:$ Moves to the end of the file.

How to Go to a Specific Line?

To go to a specific line, first, switch to the normal mode. Then type :<line number> and press the ENTER button. It will move the cursor to the specified line. Here, I will move to line 5 by executing :5 See in the following image:The cursor moved to the line 5.The image shows that the cursor moved to line 5.

Follow the below section to go to a specific line in different contexts:

1. Opening a File in Vim at a Specific Line Number

To open a file in Vim at a specific line number, follow the syntax: vim +line_number file_name. This command will open the specified file and set the cursor at the beginning of the specified line number. Now, I will execute the below command on the terminal to open the analyze.sh file at line 5:

vim +5 analyze.sh

The file is open and the cursor is at line 5.The image shows the analyze.sh file is opened, and the cursor is set to line 5.

2. Going to a Line Containing Specific Pattern

To go to a line that contains a specific text pattern while opening a file in Vim, follow the syntax: vim +/specific_pattern filename. Here, vim will search for the specified pattern inside the file and set the cursor to the beginning of the first line containing the pattern (in case there are multiple lines containing the same pattern). Now, I will search for the “cho” pattern inside the analyze.sh file. To achieve this, execute the below command in the terminal:

vim +/cho analyze.sh

This will search for any line containing “cho” inside the file:The cursor moved to line containing "cho".The image shows that the cursor is set to the beginning of the first line containing “cho”.

3. Move Cursor Relatively from the Present Line

To move the cursor relatively from your present line, at first, switch to the normal mode. After that, type the line number, then press the K button. In my case, currently, the cursor is at line 5:The cursor is at the line 5.The image shows that the cursor is at line 5.

Now, I want to move the cursor two lines upward. For this, first, I will switch to the normal mode by pressing the ESC button. Then type 2 and press the K button. It will move the cursor two lines upward:The cursor moved two lines upword in vim.The image shows that the cursor has moved two lines upward and is now at line 3.

How to Jump to the First Line?

To jump to the first line, first, switch to the normal mode. Then type gg and press ENTER. Here, ‘gg’ is case-sensitive. In my case, the cursor is at the last line because I have done some editorial work on this line. Now, I want to move to the first line of this file:The cursor is at the last line.To move to the first line of the file, initially switch to the normal mode. After that, type gg and press ENTER:The cursor is at the first line.The image shows that the cursor is now at the first line.

In the following section, I have demonstrated a few other approaches to moving to the beginning of a line or file:

1. By Pressing “0” Jump to the Beginning of Line

To move to the beginning of a line, switch to the normal mode. Then press 0 to move to the beginning of a line. In my case, the cursor is currently in the middle of line 5:The cursor is at the middle of line 5.Now, I will go to the normal mode and press 0 to move the cursor to the beginning of that line:The cursor is at the beginning of the line 5.

2. Using the Home Button Jump to the Beginning of File

To jump to the beginning of a file, press Ctrl+Home. It will move the cursor to the start of the file. In my case, the cursor is at line 5. But, I want to move the cursor to the beginning of the file:The cursor is at the end of line 5.Now, I will go to the normal mode and press Ctrl+Home to move the cursor to the beginning of the file:The cursor is at the beginning of the file.See in the image, the cursor is at the beginning of the file.

3. Using Open Brackets Jump to the Beginning of File

To jump to the start with open brackets, press the “[“ button double time from normal mode. This will move the cursor to the start of the file. In my case, the cursor is at line 6. And, I want to move the cursor to the start of the file:The cursor is at the line 6.To move the cursor to the start of the file, I will first switch to the normal mode. Then, I will press [[ (double ‘[‘ ). It will move the cursor to the start of the file:The cursor is at the beginning of the file.The image shows that the file the cursor is at the start of the file.

Note: You can also type :1 and press the ENTER button from the normal mode in the Vim editor to go to the first line of the file.

How to Jump to Last Line?

To jump to the last line, press the G key while staying in normal mode. This will move the cursor to the end of the file.

In my case, the cursor is at the line 5. First, I will switch to the normal mode:The cursor is at the line 5.Now, I will press the G key to move the cursor to the last line:The cursor at the last line.The image shows that the cursor is at the last line.

Here, are a few other approaches to jumping to the end of a file or line:

1. Jump to the End of Line Using “$”

To move to the end of a line, type $ from the normal mode. Here, I have opened a file on the Vim editor, and the cursor is in the middle of line 5. I want to move the cursor to the end of line 5:The cursor is at the middle of a line.To move the cursor to the end of the line, firstly I will switch to the normal mode and then, type $ . It will move the cursor to the end of the line:The cursor is at the last of the line.The image shows that the cursor is now at the end of the line.

2. Using the End Key Jump to the End of File

To go to the end of a file, press CTRL + END from the normal mode. In my case, the cursor is at the line 3. Now, I want to go to the end of the file:The cursor is at the line 3.To move to the end of the file, I will first switch to normal mode. Then, I will press CTRL + END. It will move the cursor to the end of the file:The cursor is at the end of the file.

3. Jump to the End of File Using Closed Brackets

To jump to the end, Type ]] from the normal mode. In my case, the cursor is at line 2. I want to move the cursor to the end:The cursor at the line 2.To move the cursor, first, I will switch to the normal mode. Then, I will type ]] (double bracket):The cursor is at the end of the file.The image shows that the cursor is at the end.

Navigating Around Vim

Navigating around Vim enables the programmer to go to the intended line to make necessary edits. Here, I will demonstrate some approaches to navigating around vim.

1. Append/Insert Text at the End of a Line

To append text at the end of a line, press SHIFT + A from the normal mode. In my case, the cursor is at the beginning of line 2. Now, I want to append a text at the end of this line. For this, I will first switch to normal mode:Previous position of the cursor.Now, I will press Shift+A. It will turn on the insert mode and move the cursor to the end of the line:The cursor is at the end of the line.The image shows that the insert mode is to append text at the end of a line.

Note: Press Shift+G+A to append text at the end of a file.

2. Move the Cursor to the Next Line

To move the cursor to the next line, press the J key from the normal mode. In my case, the cursor is presently at line 4:Previous position of the cursor.To move to the immediate following line, I will first switch to normal mode. Then press the J key:The cursor moved to the next line.The image shows that the cursor moved to the following line, which is line 5.

3. Delete the Character Under the Cursor

To delete the character under the cursor, press x from the normal mode. In my case, the cursor is at the “s” character of line 11. To delete this character, I will first switch to normal mode:The cursor is on the "s" character.Now, I will press the X key:The "s"character is deleted.The image shows that the “s” character deletion is successful.

Conclusion

In this article, I have demonstrated several approaches to go to different lines in Vim. It will ease the programmers to efficiently manage scripts. Don’t forget to throw your valuable thoughts in the comment box.

People Also Ask

How do I jump to start a line in Vim?

To jump to the start of a line in Vim, press 0. It will move the cursor to the beginning of that line.

How do I jump down in Vim?

To jump a page down in Vim, press CTRL+ F. To jump a page up in Vim, press CTRL+ B.

How do I go to the last line in Vim?

To go to the last line in Vim, press CTRL + END or CTRL + ]] from the normal mode.

What is replace mode in Vim?

The replace mode in Vim is a facility to cutdown a specific part of a file and add new text.

What is Vim normal mode?

Vim’s normal mode is the start-up mode. You can easily switch to Visual mode(pressing the V key) or insert mode (pressing the I key).

How to go to a specific line on Ubuntu?

To go to a specific line type :<line number> and press the ENTER button from the normal mode. It will move the cursor to the specified line.

Related Articles


<< Go Back to  Vim in Linux | Linux Text Editors | Learn Linux Basics

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment