Vim : Anywhere With You

Last time I showed you how to manage your Vim plugins with Vundle. Now I want to give you some ideas how to make your configuration portable. Imagine that you are on another machine (some production server for example) and you want your own Vim. Another thing I want to share is my idea for readable Vundle plugin management. So lets start:

  1. The VundleFile. I don’t want to put my plugins in the .vimrc file, I want to keep it clean. So I created a file called VundleFile (or .VundleFile if you want to be hidden) and put it next to my .vimrc configuration. This file looks like that:

    Bundle 'gmarik/vundle'
    Bundle 'tpope/vim-fugitive'
    Bundle 'The-NERD-tree'

    Clean, isn’t it? But the problem is that this file should be sourced somewhere in the .vimrc configuration. So I modified my .vimrc configuration like this:

    au BufRead,BufNewFile VundleFile set filetype=vim
    if filereadable("VundleFile")
        so VundleFile
    endif

    Now if I have a file called “VundleFile” next to the current (.vimrc) file it is sourced and all the Bundles are taken into account. You can do the same thing for a .VundleFile. The command on the first line tells vim to treat the VundleFile as a vimscript source file (when editing it).

  2. Per-project configuration with Vim.
    Put these two lines in your .vimrc file:

    set exrc
    set secure

    Now if you have a .vimrc file in you current folder (for example your project folder) it will be automatically sourced and used. The secure setting will keep you from harm :). You can push this configuration to your git repository and have it anywhere (but you will have to update all the .vimrc files on all machines to have the two lines from this section… Later I will show you how to have fully portable configuration).

  3. Per-project configuration with Vim + per-project plugins.
    The idea is to not only to have a .vimrc configuration in your project folder but to have a .vim folder with all your plugins in it too. Modify the .vimrc configuration of your project to begin with this:

    set nocp
    
    let s:current = expand('%:p:h')
    while s:current != '/'
        if isdirectory(s:current . '/.vim')
            let s:parent = s:current . '/.vim'
            break
        endif
        let s:current = fnamemodify(s:current, ':h')
    endwhile
    
    let s:clean_path = $VIM . '/vimfiles,' . $VIMRUNTIME . ',' . $VIM . '/vimfiles/after'
    let &runtimepath = s:parent . ',' . s:clean_path . ',' . s:parent . '/after'filetype off
    let &runtimepath = &runtimepath . ',' . s:parent . '/bundle/vundle'
    call vundle#rc(s:parent . '/bundle')

    A lot of magic! The first line tells Vim to be Vim and not Vi. It is important; you will use Vundle. The while statement searches for a .vim folder from the current folder up to your root folder and if it finds such folder it saves it in the s:parrent variable (bad name, I know, I’ll fix it some sunny day…). The following two lines clean up the runtime path of vim to contain only the system paths and the found .vim folder. So if you don’t have dedicated .vim folder the one in your home will be used, isn’t this convenient?
    The last three lines setup Vundle to use your project’s .vim folder. So it is important to install Vundle there.
    Now all your plugins will be loaded and installed from this .vim folder. The plugins installed in the home folder won’t affect this configuration. In other words you have absolutely clean vim configuration for the project.

  4. Per-project configuration without depending on the .vimrc located in the home folder.
    This one is easy. Just start vim manually pointing to your project’s .vimrc.

    vim -u .vimrc
  5. Vim as interpreter for Vimscript.
    If you want to try some vimscript commands in something like interpreter. You should start vim like this:

    vim -nNE

    Here is your interpreter. If you want to use the current project configuration for the interpreter:

    vim -nNE -u .vimrc

    If you want to start the interpreter without configuration:

    vim -nNE -u NORC

This is for today, now you are Vim lover. Try this post using the structure in my github – https://github.com/meddle0x53/portable-vim.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s