There are times when editing or viewing text files that it can be handy to display line numbers for the file on which you're working. In this tutorial I'll demonstrate how to show line numbers in Vim/vi/gVim.
Show line numbers in Vim
To turn on the line numbers, you first need to be in Command Mode. If you're not sure which mode you're in, just hit the [ESC] key once to make sure. Then enter the following command.
That's it. The line numbers should now be displayed.
Hide line numbers in Vim
Turning the line numbers off is just as easy. Again, make sure you're in command mode first. Then enter the following.
The line numbers should now be hidden.
Make Vim show line numbers by default
On Linux, the Vim defaults for your user ID are stored in $HOME/.vimrc. (That's /home/yourID/.vimrc). You can edit this file with Vim by using the following command from the terminal.
If this file doesn't already exist, Vim will create it. Navigate to the end of the file (it might be a blank file already), then hit [a] to go into Append Mode. Then just enter a new line:
Then hit [ESC] to enter Command Mode. Enter the following command to save and quit Vim.
On Windows the Vim defaults are stored in C:\Program Files\Vim\_vimrc. Edit the file like I showed above and save it.
Vim will now display line numbers when you start it.