Getting the .vimrc setup the way you like
As with many programs in Linux, Vim has the option to read settings from a run-control file. This can be centralized via the /etc/vimrc
file, or for each user via the ~/.vimrc
file. With this file, especially with our own version, you can customize how Vim appears and controls its functionalities.
Firstly, we will look at line numbering. Often when we edit a file, we do so as the console has reported an error on a particular line just after we have tried running a script or starting a service; we know we have a syntax error. Let's say we want to go directly to the offending line 97
of the test.php
file. Then, we would duly type:
$ vi +97 test.php
This is assuming that we were in the same directory as our file. Similarly, should we want to go directly to the first occurrence of the word install
within the readme
file, we could issue the following command:
$ vi +/install readme
Then, as if by magic, we are transported to the correct line that we require. However, in the case of the word search
, the word that was search
is highlighted in color. If that is not desirable, then we can simply turn off that feature. Within Vim, we can type:
:nohlsearch
If there are settings that we want to make permanent within Vim, we can edit the .vimrc
file in our home directory. This is our own personal settings file and as such, changes made here will not affect anyone else. If we want to affect system-wide settings, then we can use the /etc/vimrc
file. Try adding the following line to the ~/.vimrc
file to persistently disable the highlight search
:
set nohlsearch
With this addition, each time we start Vim, the setting is ready for us. As we view our files though, from within Vim, we may prefer to have line numbering turned on. Sometimes this makes life easier, but other times, we may prefer to have line numbering off, especially in cases where we have lines starting with numbers (because the display can become confusing). To enable line numbering, run the following command:
:set number
To turn line numbering off, we can use the following command:
:set nonumber
As before, we can always put the desired start-up value in the .vimrc
file. However, before we do this, let's look at key mappings within Vim and how we can create a shortcut to toggle line numbering on and off. We would like to create a mapping for the normal mode in Vim. This is the mode when we first enter Vim and we are not editing, just navigating the file; using the Esc key, we can always return to the normal mode. Execute the following command:
:nmap <C-N> : set invnumber<CR>
The nmap
command denotes that we are making a mapping for the normal mode only. We are mapping the Ctrl + N keys to run the sub command :set invnumber
followed by <CR>
.
With this in place, we can now use the combination of Ctrl + N to toggle line numbering on and off. Now we are really starting to make some steam with this product, and you can gain some appreciation of why it is so popular. Before we make the final edit to the .vimrc
file, we will see how to navigate lines by number while in vi or Vim. Making sure that we are in the normal mode using the Esc key, we can use 2G
or 2gg
to navigate to line 2 of the current file; likewise, 234G
or 234gg
would go to line 234 and G
or gg
would navigate to the end of the file. Simple but not simple enough; I would prefer to type the line number followed by the Enter key. For this, we map the Enter key to G. If we choose to use the Enter key without a preceding number, then we are taken directly to the end of the document, just as we would is we used the key G by itself. Execute the following command:
:nmap <CR> G
Now we simply type in the desired line number followed by Enter. This in turn is interpreted as the number followed by G. In this way, we can navigate easily to the correct line. We can persist this setting by adding the following text to the .vimrc
file, which should now read similar to the following text as we review all the settings made within this subsection:
set nohlsearch number nmap <C-N> : set invnumber<CR> nmap <CR> G
Now sit back and enjoy what you have achieved, remembering though that practice is the key to knowledge being retained.