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.

base.vim

This file contains the bare minimum settings that, I feel, should be used for any VIM installation!

plugins.vim

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.

functions.vim

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.

theme.vim

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.

settings.vim

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.

leader.vim

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.

keymappings.vim

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.

I got some feedback from some people on the vim sub reddit. I  should be using ftplugin for language specific settings!

plugin_settings

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!

5 Comments on “VIMRC you’re doing it wrong!

  1. I do something similar, but instead of maintaining an array of the filenames, I just prepend numbers to the names to make sure they’re loaded in the right order (similar to the idea of Apache’s now-typical “sites-enabled” directory) and use the glob function to find them.

    Here’s the snippet: https://github.com/aaronbieber/dotvim/blob/master/vimrc#L92

    This was a nice write-up; more people should organize their large configs this way!

  2. Is it possible change the script that it loads all scripts from, say “~/.vim/rcfiles/*” without need to enumerate them explicitly?

    • Yes It would be, however you really shouldn’t do it, same reason for not doing it with shell scripts.
      If you know everything in a directory will be sourced or run, then someone else could put a file in there that could cause harm to your computer (or a practical joke).

      However if you’re confident about it, sure set it up.

  3. I do something similar, but instead of make a loop to load my configuration files I just put them in “~/.vim/plugin” directory, and vim load them form me.

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

%d bloggers like this: