Overview
Usually, a Linux file is displayed without line numbers when opened with vi or vim. The below command can be used to display line numbers in vi or vim editor
:set nu + Enter key
Example
Below is the example to set line numbers in a file:
- Create a temp file with some content:
~ $ touch temp.txt
~ $ echo $'this is line 1\nthis is line 2\nthis is line 3\nthis is line 4' > temp.txt
- 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
Users can use the above method to go to the desired line number. 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).