Overview
Usually, a Linux file is displayed without line numbers when opened with vi or vim. To go to a line number first we have to display the line number. The below command can be used to display line numbers in vi or vim editor
:set nu + Enter key
Once the line numbers are displayed in the editor, then type the below command in the editor itself to go to a particular line number
:n + Enter key
where n is the line number you want to go to. Let’s see an example
Example
Below is the example to show line number first and then go to a particular line:
- Create a temp file with some content:
- Check the content of temp file using cat command:
~ $ cat temp.txt
this is line 1
this is line 2
this is line 3
this is line 4
- Now open the temp file with vi or vim and then type “:set nu” and then press Enter key. This will show line numbers in the opened editor like below:
1 this is line 1
2 this is line 2
3 this is line 3
4 this is line 4
~
~
~
~
:set nu
- Now go to a particular line number by using “:n” followed by Enter key where n is the line number you want to go to. Here I have used “:3” to go to line number 3. After typing “:3” do not forget to press Enter key. You will see in your editor that the cursor is at the 3rd line:
1 this is line 1
2 this is line 2
3 this is line 3
4 this is line 4
~
~
~
~
:3
Users can use the above method to visit/update/delete the desired line. Once the operation is completed, the file can be closed with “:q!” (to quit the file without saving changes) or “:wq” (to quit the file with saved changes).