Toggle between two different vim configurations?

9

2

I've got a .vimrc and .vim folder I've been tweaking for a while, but on my machine at a new job, I've just installed ryanb's dotfiles (which contains config files for vim, git, and more). There are things I like in each config, so ultimately I want to combine them into a new personal config. Meanwhile, I'd like to be able to switch back and forth.

Is there an easy command for this in vim - "use the following .vimrc and .vim directory" - or would it be more straightforward to swap out the files themselves when I want to switch?

Nathan Long

Posted 2010-11-09T19:09:07.497

Reputation: 20 371

Answers

7

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.

garyjohn

Posted 2010-11-09T19:09:07.497

Reputation: 29 085

I'm guessing I can use this to keep everything in Dropbox :) – Michael Caron – 2011-05-11T15:03:24.523

1@Michael Caron - you can keep your .vimrc and .vim anywhere you like, as long as you have symlinks in your home folder to wherever you put them. Personally, I use this trick to keep all my configs in a .dotfiles folder, which I can sync to Github. – Nathan Long – 2011-10-07T20:15:20.520

3

-u tells vim to use an alternate vimrc file.

Ignacio Vazquez-Abrams

Posted 2010-11-09T19:09:07.497

Reputation: 100 516

I recently learned that vim -u "NONE" opens vanilla vim, with no configuration. That can be useful if you've got a complex configuration and want to know whether some behavior is native to Vim or not. (This also works for MacVim - I haven't tried Gvim on Linux.) – Nathan Long – 2011-10-07T20:13:45.967

1

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)

Hotschke

Posted 2010-11-09T19:09:07.497

Reputation: 255

Good solution, I suggest to add a alias for each config like alias vimbasic="ln -snf ~/.vim-configs/basic/ ~/.vim && vim", so you can change your config just typing the alias like vimbasic. – ton – 2019-05-10T17:19:11.700

1

Vim Plugin Manager Volt

https://github.com/vim-volt/volt

Installation on macOS: $ brew install volt

The feature “profile” saves a set of plugins, vimrc, and gvimrc.

You can switch combinations with one command. For example, you can switch from a web development setup to essential plugins + vimrc, or vanilla vim.

This is also helpful creating minimal configurations when raising questions on stackexchange network or issues on github for vim/vim plugins.

Hotschke

Posted 2010-11-09T19:09:07.497

Reputation: 255