It is important to be able to see line numbers when working with shell scripts or configuration files. In this Linux quick tip we will show you how to quickly show line numbers in the vi editor. We will also show you how to make displaying line numbers the default behavior for vi. Finally, we will discuss using the line numbers to jump to a specific line in a file.
To show line numbers in vi or vim, simply set the number parameter. To do this enter the
:set number command to turn on the number parameter. This will add a line number to each row down the left hand side.
You can turn the line numbers off by simply using the
:set nonumber command.
Pro Tip: You can use the nl command to add line numbers to any output.
Show Line Numbers in VI / Vim by Default
If you want to see the line numbers in vi by default, you can create a custom configuration file. Simply create a file in your home directory named
.vimrc and add the settings. To see the line numbers by default you simply need to add
:set number to one line of the file, like so:
[savona@putor ~]$ cat ~/.vimrc :set number
Now when you open vi editor, the line numbers will be displayed by default.
Jump To Specific Line in vi Editor
You can jump directly to any line in the file by entering the
:<line number> command. For example, if you wanted to edit line 257, you can use
:257. This comes in really handy when working with large shell scripts or configuration files. Often a script or service will tell you what line to find the error on.
[savona@putor ~]$ ./putorius -qs Changing to tmp directory Moved to /var/tmp Requested action completed in 00:00:01 ./putorius: line 50: GIBERISH: command not found
In the example above we tried to run a script named putorius that encountered an error. The last line of the output points us to line 50 where Bash could not find the GIBERISH command. We can use the above trick to jump right to line 50 and correct this issue.
vi Resources and Further Reading
This site uses Akismet to reduce spam. Learn how your comment data is processed.