A cleaner vimrc

Reading time ~8 minutes

The more customized your vim is, the more the vimrc grows, that’s a fact.

And the bigger the vimrc is, harder it is to find some configuration in there, especially if you don’t establish any patterns for the configuration (order of the configuration, using comments, etc). One clear example is an old version of my vimrc.

To prevent this, split up your vimrc into separate files.

How can this be accomplished?

Let’s use plugin management as an example:

I use Vundle.vim, and according to the docs, your vimrc must be set up in a specific order (Vundle config must be at the top):

" ~/.vim/vimrc
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Avoid a name conflict with L9
Plugin 'user/L9', {'name': 'newL9'}

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on" " " " " " " " " " " " " " "" " " " "

let mapleader = ","
set number
syntax on
colorscheme Tubster
set autoindent
"... more vim configuration

This might be fine if you only use a couple of plugins, but if you use a lot of plugins (like me), then your vimrc will obviously get bigger.

Splitting the vimrc into multiple files

In order to be able to split the vimrc, we have to find a way to read/source the splitted files, the runtime command is what we’re looking for.

How can I use it?

  • we create a folder called config inside the vim configuration folder (in my case, the folder lives in ~/.vim/)
  • inside the config folder, create a file and move all the Vundle configuration there, I named it VundleFile.vim
  • replace all the Vundle.vim config in the vimrc with runtime config/VundleFile.vim

The contents of config/VundleFile.vim should be:

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Avoid a name conflict with L9
Plugin 'user/L9', {'name': 'newL9'}

" All of your Plugins must be added before the following line
call vundle#end()            " required

And your vimrc should look like:

" ~/.vim/vimrc
set nocompatible              " be iMproved, required
filetype off                  " required

runtime config/VundleFile.vim

filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on" " " " " " " " " " " " " " "" " " " "

let mapleader = ","
set number
syntax on
colorscheme Tubster
set autoindent
"... more vim configuration

So now config/VundleFile.vim contains Vundle-specific config only.

Your vimrc is a lot cleaner thanks to this technique:

" ~/.vim/vimrc
set nocompatible              " be iMproved, required
filetype off                  " required

runtime config/VundleFile.vim

filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on" " " " " " " " " " " " " " "" " " " "

runtime config/custom-settings.vim

runtime config/custom-mappings.vim

runtime config/custom-functions.vim

runtime config/misc-plugin-settings.vim

If you know of a better way to do this, feel free to share it in the comments.

Getting Started with Ember.js

In the summer I started to get interested in learning a MVC Client-side framework, I originally wanted to learn Backbone.js, but a friend...… Continue reading

Github Raw

Published on October 04, 2013