Esta postagem também está disponível em: Portuguese (Brazil)
Since my second or third year in college I was already flirting with Vim, as if Vim was THAT girl – you know – the girl of my dreams: I admired it, I knew it could make my life happier and also less stressful. On the other hand, I knew Vim would require time, effort and commitment. So after some “one week stands”, last year I finally decided to commit to Vim. Well, even though the honeymoon is over already, I’m still learning more about vim every day, and admiring it more and more.
Enough with the girlfriend metaphor, one of the first things that anyone seriously considering Vim should try to learn is how Vim’s configuration system works. Every single piece of customization for Vim is organized in simple plain text files, written in a simple but powerful language called Vimscript. A big part of the power and general awesomeness of Vim comes from the great deal of extensibility provided by Vim scripts (that’s how files written in the Vimscript langauge are known). In fact, there are hundreds (if not thousands) of useful plugins for Vim – along with some not so useful but still awesome ones.
I use lots of plugins and have them organized in a nice way, but that’s another post. Today, I want to start slowly and just show you what I use as my “basic” Vim configuration file – how my .vimrc looks like.
A file called .vimrc sitting on the user’s home directory is the main point where Vim looks for configuration directives upon startup. The file just below is not EXACTLY my whole .vimrc, but a big part of it (the plugin-independent part). There it goes:
Even though it might seem like this file does A LOT of things, still it can be summarized to some highlights:
- It segregates all metafiles: Vim has some nice features – persistent undo, persistent views and backups. With them you can, respectively, undo changes after closing and reopening a file, jump to the point where you were before closing and recover in case of a crash. Sadly, these features can pollute your project’s directories with metafiles (the best-known plague being those files which names end with a tilde). The configuration in my .vimrc puts ALL THOSE METAFILES in a centralized directory defined in the VIMROOT variable (first line of the script).
- Solves the tabs vs. spaces flamewar, in favor of spaces: That’s what those 6 lines right in the beginning of the script do. The configuration follows these principles:
- No actual tab characters (\t) are ever going to be inserted in the text being edited.
- One level of indentation (conceptually) corresponds to 4 spaces.
- The tab characters present in the files opened by vim will be displayed with a width of 4 spaces.
- Pressing the TAB key on the keyboard will insert 4 spaces.
- Except when we are editing Makefiles: then an actual tab char will be inserted (the Makefile syntax NEEDS an actual tab there).
With this configuration in place, I hope that Vim becomes even more of a pleasure for you to use – as it has been for me. There are no guarantees in life, however, and you might not like my key mappings, for example. In this case, PLEASE feel free to suit them to you liking. And, if you think you found some really interesting variation of my configs, please fork the above gist from GitHub (again – the link – http://gist.github.com/2417865) and submit a pull request :)
EDIT:You’ll almost certainly need the latest version (7.3) of Vim to enjoy all the nice features I wrote about… If you use Linux and your distro doesn’t still include Vim 7.3 in the repositories, then you can download the sources (from here – http://www.vim.org/download.php) and compile Vim yourself – It’s quite easy, really. In case you use Windows there’s also a nice installer in the same address above.
That’s all for now, folks! Soon I’ll post more about my greater Vim setup.