« HOME

My Vim configuration

By Nacho Martín on 05 December 2014

I have used Vim almost exclusively for writing code for the past 12 years. When I write prose, I ocasionally use iA Writer, but I tend to introduce :w inavertedly here and there.

I don’t spend much time configuring my Vim, only an evening or two a year, or when I try a new language and I need to install some syntax file. But for the most part of my work, it is quite stable. Recently I did a raid searching for new plugins or stuff I could use and was very productive.

I won’t try to convince you to use Vim. I do not know if Vim is better or not for you. I have two very big barriers that prevent me for even trying other environments. First, I am too used to this way of working, that allows me to quickly open a Vim in whatever terminal in any machine — remote or not — and use most of my configuration. Second: I am too happy with this and I would be complaining for every extra second of time spent in start-up, updating plugins, or whatever wants the IDE to do.

Since I work mainly with PHP (Symfony2) and Javascript, my environment is more optimized for these languages.

Rather than copying my configuration or anyone else’s, I suggest you to copy only the lines you understand and really want. It is way better than cluttering your configuration with options that you will never need or use. Also, try installing plugins and new features one or two at a time. Most of them require you to mechanize some keystrokes, and, like learning new words, it is better to learn and practice small bits before adding more. At least it is for me.

With that said, you may want to check out spf13’s distribution. It has a nice selection of plugins. I think that is better to hand-pick the ones you want to use, one each week, but it is your call and he did a great work.

My Vim configuration is on Github.

Potwiki

Potwiki by Edwin Steiner manages a very simple wiki in text files in some directory. To opening the main page, I type ,ww and my wiki opens. You can create new articles just by writing the title CamelCase and following the link. As simple as that.

Excellent for taking notes that you can manage in a VCS.

There are other wiki engines out there that come with TODO-list management and other goodies, but as I am happy with the simplicity of Potwiki, and also I have no intention of throwing my wiki away or converting it to a different syntax, I don’t care much about them. They are probably great, though, so you may want to check out other options first.

Solarized

Solarized by Ethan Schoonover is a color schema created with a lot of principles in mind. I do not care much about the exact principles behind it, but it is so clear that someone has put so much thought on a color schema that it feels like an insult to him to continue using the color schema “elflord” just because I like the nerdy name.

Also, it comes in two versions: light and dark. I normally use the dark one, but during conferences I can switch quickly to the lighter one, which is more beamer-friendly pressing ,b. For doing this you can add these lines to .vimrc:

" solarized toggle background
function! ToggleBackground()
    if (w:solarized_style=="dark")
    let w:solarized_style="light"
    colorscheme solarized
else
    let w:solarized_style="dark"
    colorscheme solarized
endif
endfunction
command! Togbg call ToggleBackground()
nnoremap <leader>b :call ToggleBackground()<CR>
inoremap <leader>b <ESC>:call ToggleBackground()<CR>
vnoremap <leader>b <ESC>:call ToggleBackground()<CR>

Snipmate & Neosnippet

With sniptmate by garbas you can write snippets in a snippets folder, organized by filetype (.php, .js, .erl…), and they will be available for autocompletion. Very nice for some repetitive tasks, like writing constructor functions, new classes, annotations, or whatever you may need. It comes also with a lot of preinstalled snippets.

Neosnippet by Shoguo does basically the same, but makes these snippets available for Neocomplete (discussed below). Since I was using snipmate, and they are compatible, now I use both.

Neocomplete

Autocompletion, autocompletion, autocompletion. Using several sources: snippets, filenames, previously used words, exuberant ctags for code… This is very very nice. Check it out here.

screenshot

Of course this works better with exuberant ctags. For a Symfony2 project, what works best for me is this .ctags configuration, placed at the root of the project:

-R
--languages=php
--php-kinds=cif
--exclude=.git/*
--exclude=app/*
--exclude=bin/*
--exclude=web/*
--exclude=tests/*
--exclude=*/Test/*
--exclude=*/Tests/*
--exclude=*test*
--exclude=*Form/Type*
--exclude=vendor/*/vendor

This will generate a tags file that will be used for autocompletion, jumping to definitions and things like that.

ctrlp

ctrlp by kien is a finder. You can open it by, of course, pressing CTRL+P and this will open a small window at the bottom with several options to find files.

screenshot

There are several keystrokes worth to remember, but two will unleash 80% of the goodies: CTRL+F to navigate forward in the list of modes and CTRL+B to navigate backwards. The modes are:

Also, if you use ctlrp-funky by tacahiroy you will have a new mode containing the tags of the current file (even if you don’t use crags). For example, the function definitions. If you want to access this useful mode directly you can do it with ,fu.

Tagbar

tagbar by majutsushi allows you to open a side window with the tags of the current file, as some IDEs do. I open it with ,tb This is useful in some cases, if you want to have a map of the file in mind. Still, I normally prefer ctrlp-funky for jumping into tags.

screenshot

NERD Tree

NERD Tree by scrooloose is a filesystem explorer. I was using ranger in the past, but since I am too lazy to install it in every machine I have access to, now I just use NERD Tree and then I have one less thing to install.

screenshot

As usual in the Vim world, NERD Tree has a very complete documentation, but with just one command you have a lot: ,n (my binding for :NERDTreeToggle).

If you feel adventurous you can check out the documentation to find more about file navigation, bookmarking files, and so on.

PHP & Symfony2 specifics

PIV

PIV by spf13 is a full PHP integration environment for Vim, with better completion, indenting, support for PHP 5.3 namespaces and such, and PHPDoc goodies.

The PHPDoc part is tricky. With ,pd you can insert PHPDoc snippets according to the context. This needs configuration and, as far as I know, it can only be done by manually editing the plugin file in .vim/bundle/PIV/ftplugin/php/doc.vim. If you know a better way, please let me know.

PHP namespace

vim-php-namespace by arnaud-lib allows you to insert use statements for the class under the cursor by pressing ,u, which saves time.

Vim Twig

vim-twig by beyondwords provides twig syntax highlighting and useful snippets. No more tweaking Vim to use Jinja syntax for your Twig templates.

PHP CS fixer

vim-php-cs-fixer by stephpy allows you to run php-cs-fixer to your files. php-cs-fixer is a tool created by Fabien Potencier that fixes trivial violations of the PSR-1 and PSR-2 documents.

With ,pcf it will fix the current file, and with ,pcd it will fix the current directory. Sweet.

Vdebug

Vdebug by joonty is a multi-language debugger client for several languages, PHP included.

This (beware, it will be open a big face staring at you) is a good post about how to configure Xdebug and Vdebug to work together and do some debug.

And that is it. I know that there are endless plugins, and many will be very good, but these ones are the ones I use, and, since each plugin takes some time to get used to it, I prefer to try one at a time and make its keystrokes a natural extension of the toolbelt.

Written by @nacmartin

blog comments powered by Disqus

» ALL POSTS