[ /howto/vim ]

Vim is a text editor originally released in 1991. It was based off of 'vi', an earlier text editor released in 1976.

a screenshot of vim

Why vim?

To start, I think it's important to understand why you would want to use vim at all. I wouldn't be writing about it if I didn't think it was worthwhile, but other people have done a great job of explaining their reasons already, and I'd rather cover other things.

Giving it a try

Secondly, there's always the chance that vim just isn't for you, and that's fine if that's the case. Nevertheless, you won't know if that's true unless you give it enough of a chance to get past the initial confusion of using a modal editor. This blog is how I really got started with vim. I recommend starting with that before going much further.

This post about VIM is also really helpful.

Your vim

Many people who swear that vim is the best text editor around will freely admit that they find stock vim borderline unusable.

I'm quite comfortable installing it and using it as it is, but whenever I intend to do a lot of text editing on a new computer, I configure vim to be more comfortable. The ease of doing this is what sets vim apart from many other editors.

My pal prurigro is quite proud of his configuration files, into which he has invested a fair bit of energy.

I prefer to stick closer to the most basic configuration, since I find transitioning between a richly customized environment and vanilla config to be a frustrating experience.

That being said, I like to pick out parts of prurigro's config, and find which custom tricks suit my editing style.

Customizing your .vimrc

When launched, vim starts by looking for a vimrc, a text file typically stored in the path ~/.vimrc. There are many configuration variables you can enter into this file which will modify almost any aspect of vim's behaviour.

Any of these variables can be modified at runtime as well. While in normal mode, enter :syntax on, and watch as vim starts highlighting your source code. If the language of your choice is unsupported by stock vim, you can create your own.

You have the option of editing your vimrc on the fly, or you can edit your vimrc file in another window, and preview the changes with a source file.

When you add a line to your config, and you want to see the changes, run :so ~/.vimrc, and see the changes reflected in your current file.

My .vimrc

By default, the <F1> key is set to open up vim's help browser. I find this feature really annoying, since I often end up hitting it by accident when going for either Esc or the backtick/tilde key. Vim lets you remap any keybinding, though, and that's what I do on the first line of my config. No more help browser!

In normal mode, you can use the % key to jump to any matched character (parentheses, square, or curly brackets). I write a lot of Javascript, and some LisP, and I use this a lot to make navigation faster. The second directive in my config makes source files with lots of nested functions much easier to read, by highlighting matching brackets automatically. Matchtime affects the speed at which brackets are matched.

In many circumstances, it is desirable to have your editor wrap text within the boundaries of the window. invokes this behaviour, and causes it to break lines intelligently, according to word boundaries.

I prefer having tabs enter two spaces, rather than an actual tab character. I do my best to keep each line in my source files shorter than 80 characters. Tabs make it such that a line can be 80 character, yet display as though they were longer than that. The rest of the directives relate to your indentation behaviour.

To remove existing tabs from any source file and replace them with your configuration's tab style, simply run the command :retab.

" disable help menu
map <F1> <Nop>

" match brackets
set showmatch
set matchtime=3

" soft wrap text
set wrap linebreak nolist
set showbreak=+ "indicate wrap points with a special character

" nice indentation
set softtabstop=2

set shiftwidth=2
set tabstop=2

set expandtab
set smarttab
set smartindent
set autoindent

Modifying your color scheme

You can load new colorschemes by issuing the directive :colorscheme <name> from within vim, or just use the shorter alias for that command, :colo <name>. Vim only comes with a few basic colorschemes. You can flip through and see which colorschemes are installed by entering the first part of the command, :colo and then hitting tab successively.

If you want to add new custom colorschemes, you just need to save the colorscheme file into ~/.vim/colors/. There are many such colorschemes available across the web. I installed all of the schemes found in this github repository.

Indentation

After spending some time mucking about fixing indentation after pasting bits of code into other files, I was beginning to get annoyed.

I tried disabling smartindent and autoindent and discovered that they were indeed the culprits. When I would paste, it would treat that paste as regular input, and would automatically indent lines which had already been formatted.

As per usual, there was an easy fix. Instead of getting annoyed at vim, I'm annoyed at myself for not having fixed the problem sooner. I found my answer on this page.

You can manually enter paste mode, which disables indentation, if you want. Otherwise, there is a pastetoggle variable, which you can map to any character code.

Since I've disabled the <F1> help browser, I decided to map pastetoggle to that.

set pastetoggle=<F1>

Now when I want to paste code snippets, I prepare vim for the task by hitting <F1>, entering the code, and toggling back with the same key.

When editing the rest of the time, the indentation modes function exactly as I expect.