VIM: working with "light-projects"

While VIM certainly isn't an IDE, there are a lot of plugins in existence to make it more IDE-like. Although I have been working a lot inside IDE's the past few years, I don't really need all those fancy IDE features (and the bloat that comes with it). That's one of the many things I like so much about Vim, I can just add the features I need to the editor. So while there are certainly at least a few plugins to work with projects inside Vim, I came up with my own system for working with "projects" (call them "light-projects" if you wish). One small note: For the moment this method suits my needs, but since I'm only using Vim for about two months as my main editor, this might change obviously.

Basically, my solutions is based on an autocommand for every 'project' and the command-t plugin. Oh, and sessions as well. The autocommand detects when a file is read (or created) inside the project-directory. In this case, it will source the correct 'vimrc-project-file' and setup everything specific for the project. So as an example, suppose we have a project in ~/Projects/foobar/. In my .vimrc I add the following line (to be more correct, for most of the projects I would add the line in my host-specific vimrc-file, as I mostly use one specific computer to work on a specific project):

autocmd BufRead,BufNewFile ~/Projects/foobar/* source ~/.vim/projects/foobar.vim

This autocommand simply sources the foobar.vim file everytime a file in the foobar directory is read, or created. Since the foobar.vim project-file is just a regular vim-script file, you can do anything you want in it. I mostly use it to set some editor properties and add some additional mappings, such as:

" I use autochdir by default, but for this project I prefer
" to always be in the project-root. So we first disable the
" autochdir option
setlocal noautochdir 

" and then we cd to the project-root
cd ~/Projects/foobar/

" in this project i like to use ant, so set it up as the make program inside vim
setlocal makeprg=ant\ -find\ build.xml

I have some other options in my project-vimrc file which are more specific to exploring and navigating code, but I will explain these in a later post I plan to write.

So that's it for automatically configuring vim with settings specific to your project when editing one of the files of that project. One other important aspect of working with projects is to be able to easily open any file of your project. Most IDE's have fancy file-explorers, and a lot of vim-users seem to love the NERDtree plugin. But a few years ago, when I found the OpenIt plugin for jEdit, I discovered that I can type faster than search my way through a tree-structure of directories and files. Resharper adds a similar feature to VS.NET (did I already mention that Visual Studio is just crap without Resharper?). Just press a shortcut, start typing what you need, and let the program do the searching.

For vim I found the command-t plugin. After installing the plugin you can launch command-t with <leader>t (unless you have changed your leader key it will be a backslash, so the correct mapping will be \t). Command-T will index your current directory and it's children. The fact that it indexes the current working directory is one of the reasons why I `cd` to the root of a project in my project-vimrc, so while working on a project Command-T will always index my entire project. The first time you launch it, command-t might take a few seconds before the indexing finishes (depending on the amount of files in the directory-tree), but the results are cached, so next time you launch Command-T it shows up almost instantly. Command-T will now show you a list of all the files in your project. Now you can just start typing parts of the filename (or even of the path to the file) and Command-T will filter out the matching files. If the correct file is selected, simply press enter and Command-T will open the file. I really find this a lot easier than hunting down the file myself.

One last feature of Vim I use for projects are sessions. If your Vim is compiled with session-support (it probably is), you can let Vim write a file which contains a list of all the buffers you have opened (and mappings etc which you have defined while editing). When you want to continue working on your project, you can simply instruct Vim to open the session from the session-file and Vim will restore all the opened buffers, mappings etc.

While this is already useful, I think it still requires to many manual actions. No problem, plugins to the rescue! The SessionMan plugin makes all of this a lot easier. The first time you start your project, simply save your session with :SessionSave. SessionMan will notice that you currently don't have a session and ask for a name, so you can enter 'foobar' (if that is the very original name of your project). When you start vim again to continue working on your project you can now simply use the command :SessionOpen foobar to open your foobar session. Also SessionMan will automatically save your session when quitting vim. Another useful command is :SessionList (which I have mapped to ,s). This shows a list of all your sessions and lets your select the session which you want to open.

So in short, when I want to continue working on a project:

  • I launch gvim
  • I type ,s to open the sessionlist
  • I select the correct session
  • Vim restores all my opened buffers and windows
  • Thanks to the autocommand for the project, vim is correctly setup for the project
  • I can quickly open files with \t


There are no comments.

Comment Atom Feed