Vim: tips and tricks

01 Apr 2012

Vim is an editor built on vi, at the moment it's my favorite shell editor. It is very tweakable and there are many plugins to autocomplete words and functions. In this post I will discuss some features of vim and how they work. Vim is more than just a basic editor.

Nagivating

You can easily navigate with vim.

  • Go to the start of a page double tap

     g 
    

  • Go to the end of your page

    G
    

  • Jump to the text on the beginning of this line

    ^ or home-button
    

  • Jump to the end

    $ or end-button
    

  • Jump to your last edit

     "
    

Copy,Cut and Paste

Cut and paste:
  • Position the cursor where you want to begin cutting.
  • Press v (or upper case V if you want to cut whole lines).
  • Move the cursor to the end of what you want to cut.
  • Press d.
  • Move to where you would like to paste.
  • Press p to paste after the cursor, or P to paste before.
To copy you use the same steps, but instead of pressing d you use y. To cut a line you can just double tap d it. Tap p to paste. Something handy is that you can also enable your mouse in vim (even in xterm):

:set mouse=a

Find and replace

You can do find and replace. A vim substitution regex looks like this:

:range s/pattern/string/option

ranges this is the first character after the ":" :

  • number an absolute line number
  • .
  • the current line
  • $ the last line in the file
  • % the whole file. The same as 1,$
  • 't position of mark "t"
  • /pattern[/] the next line where text "pattern" matches.
  • ?pattern[?] the previous line where text "pattern" matches
  • \/ the next line where the previously used search pattern matches
  • \? the previous line where the previously used search pattern matches
  • & the next line where the previously used substitute pattern matches
Options:
  • c confirm each substitution
  • g replace all occurrences
  • i ignore case
  • I do not ignore case
So if we have a text:
Johnny was a little boy but Johnny is no more, for what Johnny thought was H2O was H2SO4!
So if we want to replace "Johnny" with "Bobby" we need to type:

:%s/johnny/Bobby/gi

This means, replace, in the whole file, all character sequences that match "Johnny" with "Bobby" ignoring capitalization. If these words were in the text they would match:

johnny
Johnny
Johnnytonny

You may also write the previous expression like this:

:%s:johnny:Bobby:gi

Here we replace "/" to ":". If we want only words to match, so no character sequences (e.g. Johnny but not Johnnytonny). We need to use the word boundary:

:%s:\<johnny\>:Bobby:gi

If you want to learn about patterns and regexes visit vimregex.com.

Layout and highlighting

It might be that you want some syntax-highlighting or you want to have more space from the sides, or you don't want text wrapping,.. . This can all be done by issuing these commands:
  • If you want syntax-highlighting:

    :syntax on
    

  • The colors are rather dark by default, so change the scheme by issuing:

    :set background=dark
    

  • Do not wrap code:

    :set nowrap
    

  • Use auto-indentation:

    :set auto-indent
    

  • Show partial commands

    :set showcmd 
    

  • Set the textwidth to 80

    :set textwidth=80  
    

  • Show matching brackets

    :set  showmatch 
    

  • If you want line-numbers:

    :set  number 
    

Abbreviations

You can use abbreviations to automatically insert another word or sentence instead of the provided word:

:ab rtfm read the fine manual

Whenever you type 'rtfm' followed by punctuation such as a space or comma, the 'rtfm' will be expanded to 'read the fine manual'. This also happens if you type 'rtfm' then press Esc or Enter. To remove one abbreviation:

:una rtfm

To clear all abbreviations:

:abc

Dictionary

You can use Ctrl+k after typing a few characters to complete a word. First of all you will need to add a directory. On Debian machines you can find a dictionary here:

/usr/share/dict

To add the american-english dictionary to vim:

:set dictionary+=/usr/share/dict/american-english

Now start typing a word, hit ctrl+x and then ctrl+k you will get a sugestion from the dict file.

Vimrc

Some of the functions you might want to have on by default, instead entering them every time you start a new session. The easiest way to do this is to define a file called

.vimrc

in your homefolder. All of the commands like:

set background=dark
syntax on

can be defined in here (every one on a new line). They will automatically be enabled when you start vim.

Plugins

There are many vim plugins available to provide auto complete. One such plugin is called Omni complete. To enable Omni complete issue:

filetype plugin on
set ofu=syntaxcomplete#Complete
ab rtfm read the fine manual

You will also needing special files to load auto completion for your specific language. These can be downloaded from vim.org. You will need to place them in your home folder under:

~/.vim/autoload/

Or for global enable on Debian:

/usr/share/vim/vim72/autoload/

To enable auto complete for python:

:set omnifunc=pythoncomplete#Complete

So now when you have written a function, you can just type the first letters of the funtion's name and hit ctrl+x and ctrl+o and it will auto complete: See for specific languages:

Final word

There are many things you can do with vim, I barely scratched the surface here. If you want more info or deeper understanding visit: