Vim in RAM

Jump to:

Recently I was thinking how to speed up my Vim. My Vim configuration has tons of plugins and currently my .vim directory has over 700 MB size. That is pretty much data. Every time when new Vim instance is started it has to load every thing to memory. Things get worse when I'm using my plugins because most of them are vimscript files so they are lazy loaded and interpreted at runtime.

Situation isn't dramatic, but I can see some glitches and lags when I move cursor very fast over whole file. My machine has 16 gigs of RAM and I thought I will use some of it.

So the plan is to move whole .vim directory to RAM and symlink the it to $HOME/.vim. And here is brief log how I did this.

Top

TMPFS

To create memory file system I've used tmpfs. I've added this to my /etc/fstab file.

tmpfs   $HOME/.in-memory/   tmpfs   rw,size=2G,nodev,nosuid     0 0

Of course change $HOME to your actual home path.

Some people add the noexec flag, but I'm using YouCompleteMe Vim plugin which is using some dynamic libraries which are created during plugin installation process and adding noexec flag makes that libraries unloadable.

Remember that files/directories stored in that location will be lost after reboot. How to partially protect from this?

Top

vimram script

Moving files from tmpfs directory to disk for persisting those files manually would be stubborn and problematic. So I've created a handy vimram script that can load your vim directory to RAM and create symlink to it from old location and obviously unload your vim directory from RAM to disk to persist your vim files across reboots.

This script is available here.

Remember to put in in your $PATH location :)

Top

Systemd service

So script is not enough to automate everything. It still needs to be run manually before first vim use and before reboot. This can be automated via systemd. Why systemd? Because I use Arch Linux which uses systemd. In other distros this can be done via upstart or init scripts.

Work here is to write simple service file that will use our script on login and before reboot.

This service file is available here.

Put it in $HOME/.config/systemd/user directory where all your user specific services are and run

sytemctl --user enable vimram.service

That's it. Your .vim directory will be moved to RAM and from RAM to disk automatically.

Top

Gotchas

Persisting .vim directory only works if you normally logout from your system. So in case when power gets down all your changes in vim will be lost. But you can easily recover from this situation. There is a $HOME/.vim.ondisk file with copy of your .vim directory created when vimram load was invoked. Simply copy this directory to your regular .vim directory.

Top

Effects

So what are the effects?

Vim runs much more flawlessly. Moving cursor along whole file no longer glitches and lags. NerdTree and TagBar plugins opens much faster and of course YouCompleteMe completion popups opens almost instantly.

There is a issue with vim opening. I've used vim --startuptime flag to measure this and it seems to be no difference if running in RAM or on disk. This is probably caused by .vimrc file which still lays on disk and needs to be parsed every time when new vim instance is created.

Top