Toggle between two different vim configurations?

The -u option will allow you to specify a configuration file other than ~/.vimrc, but there is no option to specify an alternative to the ~/.vim directory. However, you can have Vim use a different directory by modifying the 'runtimepath' ('rtp') option in each of your configuration files. For example, you could use this command

let &rtp = substitute(&rtp, '\.vim\>', '.vim1', 'g')

in one of your configuration files to tell Vim to use the ~/.vim1 directory rather than the ~/.vim directory.


-u tells vim to use an alternate vimrc file.


Toggle Config by Replacing Directory Symlink

I have following setup:

❯ tree -L  2 ~/.vim-configs
~/.vim-configs
├── nathanl
│   ├── .git
│   ├── gvimrc
│   ├── pack
│   └── vimrc
├── ryanb
│   ├── autoload
│   ├── ...
│   └── vimrc
└── minimal
    ├── autoload
    ├── cache
    ├── doc
    ├── plugin
    └── vimrc

❯ ls -l ~/.vim
lrwxr-xr-x  1 hotschke  staff  42 Apr  3 16:35 .vim -> /Users/hotschke/.vim-configs/minimal

If you want to change to a different config, you simply have to execute

❯ ln -snf ~/.vim-configs/configX ~/.vim

Optional: shell function with tab complete for switching config

If you are ambitious, you could write a minimal shell function with autocompletion to make this superconvenient. For example if you use zsh, you could use

# Swim - Switch Vim Configs
compdef '_path_files -/ -W ~/.vim-configs' swim
function swim {
  if [ $# -eq 0 ]
  then
    zmodload zsh/stat
    active=$(stat +link "$HOME/.vim")
    echo "Swimming with ${${active}:t}"
    echo "$HOME/.vim -> $active"
    zmodload -u zsh/stat
  else
    echo "Swimming with $1"
    ln -snfv ~/.vim-configs/$1 ~/.vim
  fi
}

It already provides tab completion and works everywhere in your file system:

❯ pwd
<somewhere in your filesystem not necessarily $HOME or $HOME/.vim>
❯ swim <Tab>
-- directory --
minimal/  nathanl/  ryanb/
❯ swim minimal
Swimming with minimal
/Users/hotschke/.vim -> /Users/hotschke/.vim-configs/minimal
❯ swim
Swimming with minimal
/Users/hotschke/.vim -> /Users/hotschke/.vim-configs/minimal

(function name stolen from https://github.com/dawsbot/swim)

Tags:

Vim