VIMRC you’re doing it wrong!
Every few weeks I get the urge to revisit my vimrc file to do some basic house keeping. Normally I will review the plugins i’m using and clean up some of the settings i’m no longer using. The vimrc file seems to quickly become a mess of random settings, most times when I do some house keeping I don’t remember what some of the settings are. Is this setting related to a plugin? I dunno! Why did I remap this key? Who knows! I decided it might be easier to break the config into logical sections so that every setting has a home. Originally I broke the file into sections using comments. This worked well, I needed to make sure I was diligent with placing settings in the correct section based on the header. I still found sometimes settings where sneaking into areas where they shouldn’t be. Finally this past weekend I thought to myself, why not break the config into separate files. Then I will be less likely to put settings into the wrong area of the vimrc. I decided to break the config up into the following files.
This file contains the bare minimum settings that, I feel, should be used for any VIM installation!
I use vundle to manager the installation of my vim plugins. This file contains all of the plugins I depend on. I also added a comment describing what each plugin does. There have been many times I have reviewed my vimrc only to find a plugin which I don’t remember why I have it installed.
From time to time I find it useful to add utility functions to vim to make working with text easier. For example I have a simple function which pretty prints JSON data. I will place every custom function into the functions file.
Most VIM users I know have taken some time to make their editor pretty. This includes selecting a theme they feel works best for them as well as customizing things like the numbering of lines and how VIM uses the system bell. I keep these settings in my theme file.
Seems like no matter how hard I try almost every dot file I have ever made has a misc section. This is an area where I have a random collection of setting which don’t seem to fit into any major category. The settings file is where I keep these settings.
The leader key is critical for any heavy VIM user. The leader key provides a safe namespace for any custom key combinations you want to add to VIM. The leader file is where I customize my leader key settings.
No matter what editor you use there will always be come default behavior which will drive you crazy! Normally I try not to override default behavior but if its annoying or interrupts my workflow its gone. The key mappings file is where I override default key mappings which just seem to get in the way.
languages.vim One of the best parts about vim is the ability to override behavior based on the filetype. I have different settings depending on the file type being edited. The languages file is where I keep all of the customizations I do per file type.
Each plugin I have installed might have the ability to customize it’s bahavior. I have created a .vim file for each plugin and put them into their own sub directory. The plugin settings directory has all of my plugin specific customizations.
Pulling it all together.
Now that I have a home for each portion of my configuration mapped out into separate files. I need a way to pull everything together from the main vimrc file. Thats where this handy little bit of vimscript comes in.
let s:vim_home = '~/.vim/' let config_list = [ \ 'base.vim', \ 'plugins.vim', \ 'functions.vim', \ 'theme.vim', \ 'settings.vim', \ 'leader.vim', \ 'keymappings.vim', \ 'languages.vim', \ 'plugin_settings/*.vim' \] for files in config_list for f in split(glob(s:vim_home.files), '\n') exec 'source '.f endfor endfor
This code basically just loops over all of the files in my configs list. Then I run glob all of the strings, split the results and source every file. In effect its including every file into my main vimrc.
What does all this organization buy me? Now I have the ability to experiment with my configurations and keep the changes to one particular file. For example, I will experiment with a new plugin from time to time. During the evaluation period I could include the settings and the bundle statement for the plugin in a “temp_plugin.vim” file and add it to the config_list. If the plugin proves to be useful then I can later integrate it into my standard configuration. I also have the ability to swap entire portions of my config based on the environment! For example I could swap my functions.vim file for a customized version used at work. I could also add custom config if I am pairing with someone to make them feel more at home in my environment.
I’m sure as time goes on this organization will change and evolve. If you are interested in keeping up with changes to any of my dot files. You can find them on github!