Tips and tricks for vim featured image

In todays blog post I would like to take the opportunity to share some knowledge I have gained with vim. In particular with customising vim to make it more user friendly.

Out of the box the default configuration wraps long lines across multiple lines and line numbers aren’t shown. Resolving these two things makes a huge difference to readability when reviewing large volumes of text such as log files. In order to configure vim we need to edit the .vimrc file. The default location for this file is the users home directory. If you don’t have one, don’t worry we are going to cover the steps to create one. Open your terminal in your home directory and type the following command:

vim .vimrc

Switch into insert mode (i) and type the following. Don’t forget to write the file (:w).

set nu

If you open hello.txt in the same terminal you’ll notice line numbers aren’t displayed. This is because vim hasn’t loaded our new configuration file (.vimrc). Sourcing the .vimrc file will load the changes. We will need to source the .vimrc file anytime we make changes to it.

source ~/.vimrc

Now we can see the line numbers display down the left hand side.

An example of how long lines will wrap by default in vim. In a brief example like this your eye is able to follow to structure of the document naturally however in a large log file with very long lines it can quickly become difficult to navigate around the document.

Let’s change how long lines of text will be displayed. Open your vim config file again and add

set nowrap

This next tweak improves the default search facility in vim making it much easier to search through files for particular character sequences. The gif below shows the default behaviour. To search type / followed by the sequence you want to find, then press enter. If vim finds a match it will jump to that location, we can improve this significantly.

Add the following two lines to your .vimrc file:

set hlsearch
set incsearch

The first line will set highlight search which will set a background colour for all search matches (defaulted to yellow), the second option means we don’t have to hit return to perform the search. Instead the search will be performed as we type.

 

By now we are fairly familiar with editing the .vimrc file so we can take a look at another feature of vim. Mapping keys. Just like a GUI based editor such as Microsoft word uses Cmd+C to copy on mac or Ctrl+C on windows, vim has shortcuts and we can assign our own. Add the following shortcut to your .vimrc file.

let mapleader=”,”
nmap ev :e $MYVIMRC

Ok, theres quite a lot here so let’s take a look at whats going on. ‘let’ allows us to assign a variable, in this case we are assigning ‘,’ to the mapleader variable. The second line uses nmap. This allows us to map key presses to some action, in this case opening our .vimrc file. The ‘n’ prepending map declares the mode in which the mapping will take effect, in this case n means ‘normal’. Which means if we are in insert mode the key mapping won’t take effect. Now anytime we are in normal mode we can type ,ev which will open our .vimrc file.

Whilst editing text in vim in order to leave insert mode you are required to press the esc key. Many vim users will add a shortcut to make this a bit easier. Add the following to your .vimrc file.

imap jj

Now when you are in insert mode and wish to change mode to normal you can just type jj and the mode will be changed.

The last thing I wanted to discuss is navigating files. Many vim users reach for plugins to navigate files such as Nerdtree but Vim has a very powerful inbuilt file explorer. To open vim in explore mode from the command line all you need to do is open a directory eg vim ~/. To open the explore from within vim first make sure you are in normal mode then type:

:Explore

We have only scratched the surface of configuring vim but I think these few changes really make vim much more usable. To learn more about vim check out the wiki page or better yet have a go at the vim tutorial by typing vimtutor on your command line.