One thing that I’ve noticed about many vim users is that they develop habits in a vacuum from other experienced vim users – something that tends to lead to highly inefficient setups for editing code. Here’s a few simple changes that I’ve seen that can make everyone’s lives much easier. First, I highly recommend reading up on the vimrc – a configuration file that tells vim to do many things for you automatically. Second, I highly recommend looking into various vim builtins and plugins to automate even more things.
Ctags [sf.net] is not specifically a plugin to vim – it’s an external application which scans a source tree and finds source objects (classes, functions, variables, etc). From there it creates a master file which contains an index of where all of these things are defined. This allows you to do something like move the cursor over a particular object and ^], which will take you directly to the definition of the object. Occasionally, ^] will not take you to the correct place – which is where :tnext and :tlist come in. There are also plugins like TagList [vim.org] which I’m not going to cover which help out here.
Possibly the best thing about ctags is that it enables extremely fast location of source functions even across mixed language source trees – such as with Python, Java, PL/SQL, and various UDFs. While this is primarily a manner of finding out what a method does or what an object is, it also doubles as loading the file in a buffer for extending vim’s builtin autocomplete (covered below).
In order to enable ctags you’ll need to add a line to your vimrc which looks something like this:
set tag <directory>/tags
You may also need to change the tag file if you change trees or projects. That looks like “:set tag <new_directory>/tags”
Vim comes built in with autocomplete, and it’s accessible via ^p and ^P. By default, it searches files which are currently open in a buffer (including the current file) – so it builds nicely on ctags and long lived multi file vim sessions. The built in autocomplete is the kind of textual autocomplete that helps avoid typos and makes typing longer variable names much faster. It’s not the kind of intellisense given by the YouCompleteMe plugin below. However, it does mean that autocomplete is very fast and works everywhere from source editing to writing documentation – so it’s still a huge help.
Vundle [github.com] is a vim plugin manager, and I think it’s one of the cleanest ways to install and use plugins in vim. Once you’ve added a bundle to your vimrc, :BundleInstall will make it active in your current (and all future) vim sessions. If a plugin causes you grief or doesn’t work out for you, it’s pretty easy to deactivate it by commenting it yout in your ~/.vimrc.
Buffer Explorer [vim.org] is a plugin that gives you a view into the files and buffers that you have open. In a lot of ways it works just like having a bunch of tabs to files all over the project in a generic IDE. Again, this builds nicely on top of ctags and autocomplete to provide a framework to easily move between already open files. Another thing that this gives you is the ability to easy copy and paste things between buffers.
For the most part, this plugin is manipulated via the following commands:
- :e <some file name> # opens the file in a new buffer
- :e . # opens an explorer for the current directory in a new buffer
- \be # opens up the explorer for the buffers currently available. Navigate with standard vim navigation (hjkl, arrows, or mouse). Enter selects the currently selected buffer.
I also highly recommend putting the following line in your ~/.vimrc to allow you to change between files without having saved them first.
This plugin [vim.org] takes columns of numbers or dates and increments them. The idea is that you ^v and select a column of text and increment every value in it. This is primarily useful for setting up test data, and plays into why I like the Align plugin below. A typical interaction with this plugin is as follows:
- See a column of numbers or dates
- ^v and highlight the column of numbers
- :II (to increment the numbers by an implicit 1 – :II2 would have incremented by 2s!)
This plugin [vim.org] is a taste plugin. I like being able to visually look down a line of things (such as in a JSON dictionary) and see the keys on one side of a line and values on the other. This script helps automate pretty much that entire process. My typical interactions with this plugin are via key bindings which are available in my ~/.vimrc. However, you can align a block of equal signs by highlighting at and :Align =
I’ve copied them here for reference:
map ,as :Align! =W \<AS\><CR> map ,a= :Align! =W =+<CR> map ,a: :Align! =W [:]\+<CR>
This plugin [github.com] requires a little bit of setup and may not work on certain esoteric operating systems, however if you’re running on a modern operating system like OSX, Linux, or Windows it will give you great intellisense style autocomplete for pretty much every language possible. The autocomplete works automatically as you type and access packages or member variables. It should also be available through the standard ^p/^P channel as well. If you end up using ^p/^P, it will often open a small buffer at the top of the screen with the function prototype, which can sometimes be quite helpful. You’ll probably want to use :on[ly] to get rid of the extra window at the top when you’re done with it.
Every once in a while there’ll be a language (Python, for example) which has great autocomplete if you could only setup the PYTHONPATH for where your’e currently working. This can be done before launching vim or via “let $PYTHONPATH=<directory>:$PYTHONPATH”.
As a note, Java/Groovy/Scala autocomplete will likely require eclim [eclim.org], and isn’t covered here.
Syntastic [github.com] is a plugin that helps you with syntax errors. If you goof and write some bad syntax, it’ll tell you right on the screen what and where your error was. I highly recommend turning off the syntactic python checkers if the company style guide violates pep8, because they try to run pyflakes and pep8. Still, it’s pretty fantastic for seeing malformed syntax in whatever language you’re trying to work in – and in whatever style.
I’ve sometimes found it painful to visually look at code that’s not indented enough (say, two spaces). However, as with all things, VIM has plugins that will come to the rescue. The vim-indent-guides [github.com] plugin is enabled and disabled with \ig. It colors the backgrounds in vertical slices and allows you to really easily see where things are going.
This is a useful snippet for helping follow the usual Python style guides (goes in your .vimrc):
set nocompatible " No, don't use vi mode set number " Line numbers on the screen set backspace=2 " (bs) allow backspacing over everything in insert mode set hidden " Bufexlorer stops whining about the current file being unsaved set history=50 " (hi) keep 50 lines of command line history set hlsearch " Highlight my current search term set incsearch " Start searching while I type set mouse=a " Use the mouse on the terminal set pastetoggle=<F12> " Toggle !paste with a key press - mostly so that autoindent doesn't interfere set ruler " (ru) show the cursor position all the time set showcmd " set showmatch " Do paren matching as I move over them set laststatus=2 " I always want a status line set statusline=[%n]\ %F\ %(\ %M%R%H)%)\ \@(%l\,%c%V)\ %P set textwidth=0 " Don't word wrap automatically set wrap " But do continue long lines on the next line set vb t_vb= " No bells set viminfo='20,\"50,% " (vi) read/write a .viminfo file, don't store more than 50 lines of registers set autoindent " Indent is for the win set ts=8 " Tabs and I don't get along, so make them obviously huge set softtabstop=4 " use soft tabs are 4 spaces set shiftwidth=4 " use soft tabs are 4 spaces set expandtab " use soft tabs are 4 spaces set scrolloff=5 " Syntax highlighting reset set autoread " Reload files that have changed autocmd BufEnter,BufRead,BufNewFile * lcd %:p:h " Always chdir to the current directory with a file. Helps with relative paths autocmd BufEnter,BufRead,BufNewFile * syntax sync fromstart " Syntax highlight from the beginning of a file (helps with long string blocks) autocmd BufEnter,BufRead,BufNewFile * set softtabstop=4 shiftwidth=4 expandtab " Setup 4 space soft tabs no matter what autocmd BufEnter,BufRead,BufNewFile *.scala set filetype=scala " Set filetype scala for all scala files " Kills wailing tritespace map ,wt :perldo s/\s+$//g<CR>:perldo s/\t/ /g<CR> " Make Eclim and YouCompleteMe play nice let g:EclimCompletionMethod = 'omnifunc' " Find and display overly long lines highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%121v.\+/ " Modify python filetype autoindent to be less annoying. let g:pyindent_open_paren = '&sw' let g:pyindent_nested_paren = '&sw' let g:pyindent_continue = '&sw' let python_space_error_highlight = 1
I’m more than happy to pair with anyone to show how all of this works together in person (or perhaps via a youtube video or projector). Feel free to ask me any questions about how to make your vim workflow better. :)