Introduction to VIM
Getting started with VIM
One of the main things you need as a developer is a good editor. The purpose of which is simple, to make it as easy as possible for you to deal with the tasks of editing text files. vim is also cross-platform, and you can bring it with you (along with your plugins), and not have to relearn this essential tool.
This is an introductory intro. I've seen often that people, have just learned the basics of vim to get by, but missing a lot of extra features that make vim an indepensible part of a developer's toolkit. The aim here is to introduce these features, and give pointers to other tutorials to learn more about them.
For those just starting to use vim, start with vimtutor which is run from the command line. It comes installed with vim, and one should really go through it, to get used to modal editing, especially with moving around.
Command Mode
A major stumbling block and also annoyance is vi's (and as a result vim's) dual mode. People try to edit as they normally would, and find themselves all confused as weird things happen.
The key here is that vim, has two different modes. One is the normal editing mode that people are familiar with. Ie. editing mode is similar to what you do in other editors, where you type, delete and move around with cursor keys.
Command mode is where you are actually give commands to apply to your text. Normally if you were to delete the next 3 words, you would either, move the cursor past 3 words and delete it with backspace (one character at a time), or select either with the keyboard or mouse and then deleting it with the delete key. With command mode, you would type 3dw, which means "3 times, delete word" or "delete the next 3 words". Or if you wanted to delete the third word, you would type 2wdw. When you learn more commands and apply it without much thought, you find that this dual mode is extremely productive. See the links at the end of this document to learn more useful commands.
Macros
Now that we issue commands, it's also extremely useful to record them and repeat them, the next time we want to do a repetitive task.
To start recording, press Esc to go into command mode, and press qa. Now every command and keystroke you issue (even editing and adding new words will be recorded. When you are done, press Esc. a here can be any upper or lower case character. You can map multiple macros.
To recall your macro, simply type @a. Again where a was your designated macro key map. To repeat the previous one, type @@. A macro is also a command, so you can add a multiplier to it. For example 25@a, will repeat the macro 25 times.
Moving around
This is covered in vimtutor. Even on small 500-1000 line files, knowing how to set marks and jumping back and forth is important, but I'm repeating it here for convenience.
Line
Jumping to a line is easy. There are two ways.
- line number and then SHIFT-g
- :12 (where 12 is line number to go to)
Marks
To set a mark. In command mode, key in ma where a can be almost any character on your keyboard. To jump back to your marks, type `a again where a references the mark you set previously.
Tag lists
If you're programming, you often want to jump to specific functions and classes. ctags [1] and the taglist.vim, will give you this feature in vim. Download the script and put it in ~/.vim/plugin directory.
Visual Mode
Pressing v while in command mode, will allow you to select parts of your document. This makes it very easy to apply various vim commands to selected parts of your document. This is the easiest way to cut and paste.
Cut & paste using visual mode
- v and arrow keys to select
- y to yank(copy) or x to delete(cut)
- p to paste
Folding
Another useful feature is folding. Folding is when you hide, lines of text/code without deleting them. You can use them to fold functions, see how an article would read with parts cut out and so on.
Usage
Easiest way to use a folds, is with visual mode:
After selecting, type zf to fold.
To open a fold, move to the folded part, and type zo in command mode. A fold will also automatically unfold, if you make changes to the folder line in insert mode, or you are doing a global search.
Split Windows
Often in files, you have to look at two different parts of the same file, or even two different files. CTRL-w CTRL-s will split the window horizontally, and CTRL-w CTRL-v will split the window vertically.
This is an extremely useful function when you need to say lookup, the originating function or class, that is causing problems in code in a different part of the file. Or when writing documents, you are fixing up cross-referenced text for consistency.
Spell Checker
Another useful feature with the use of a plugin, vimspell.vim. Download and copy the plugin to your ~/.vim/plugin directory.
This plugin depends on ispell or the newer aspell[2]. You also would also want to set up your default language, spell checker and colours. Add the following lines into your ~/.vimrc file and change as needed.
let spell_language_list = "en_GB"
let spell_executable = "aspell"
highlight SpellErrors ctermfg=Red guifg=Red cterm=underline gui=underline term=reverse
Resources
There is a lot more in depth tutorials and tips on the web, here are a few.
- Best of VIM Tips
- Vim Cookbook
- Vim Color Editor Howto
- Vim Cheat Sheet (thanks to lowks)
[1] FreeBSD: install ctags from ports (devel/ctags). For taglist you will have to edit taglist.vim and change the location of ctags tool to exctags because, FreeBSD comes with it's own version of ctags which isn't compatible with taglist.
[2] FreeBSD: install aspell from ports (textproc/aspell).



