Run multiple neovim with various config in 3 steps (macOS & Linux)

We can use environmental variables to run neovim with different config by following these three steps (with sub steps).

  1. create a config directory
  2. create related directories
  3. create a script to start neovim with the config

Create a config directory

Create a config directory. I use ~/.config/nivm-profiles/ directory as base. For lazyvim, create a directory called lazyvim under the base directory. Use the ~/.config/nvim-profiles/lazyvim/nvim as nvim config directory.

Create related directories

Neovim uses data, cache and state directories under ~/.local/share/nvim, ~/.cache/nvim and ~/.local/state/nvim respectively. For this approach, I use ~/.local/share/nvim-profiles/ directory as base for data, ~/.cache/nvim-profiles/ directory as base for cache and ~/.local/state/nvim-profiles/ for state.

For lazyvim, create ~/.local/share/nvim-profiles/lazyvim/nvim, ~/.cache/nvim-profiles/lazyvim/nvim and ~/.local/state/nvim-profiles/lazyvim/nvim directories.

Create a script to start neovim with the config

Create a script file with the following content

#!/bin/sh

NVIM_PROFILE_NAME="lazyvim" # replace lazyvim with your config folder name
NVIM_INIT_FILE="init.lua"
NVIM_COMMAND="nvim"

XDG_CONFIG_HOME="$HOME/.config/nvim-profiles/$NVIM_PROFILE_NAME" \                                                 
  XDG_DATA_HOME="$HOME/.local/share/nvim-profiles/$NVIM_PROFILE_NAME" \                                            
  XDG_CACHE_HOME="$HOME/.cache/nvim-profiles/$NVIM_PROFILE_NAME" \ 
  XDG_STATE_HOME="$HOME/.local/state/nvim-profiles/$NVIM_PROFILE_NAME" \                                                
  ${NVIM_COMMAND} -u "$HOME/.config/nvim-profiles/$NVIM_PROFILE_NAME/nvim/$NVIM_INIT_FILE" \                       
  "$@"       

Make it executable and put it in a folder which is in the PATH environmental variable.

Use the script to start the neovim with that config.

The above script is for lazyvim and I put the lazyvim script under ~/.local/bin (which is in PATH environmental variable).

Repeat these 3 steps for every neovim config you want to run.

Pros

  • data and cache are not shared between neovim config
  • easy to backup various neovim config

Cons

  • does not work for config that uses hard coded paths (not using neovim stdpath method)
  • this approach requires disk spaces
  • tedious to setup (you could write a script to manage these steps)
  • you have to remember what to remove when you no longer want to use specific neovim config

Alternatives

  • use NVIM_APPNAME environmental variable
  • use symbolic link to swap the nvim config directory (like nv-ide installation)
  • use cheovim (Neovim configuration switcher written in Lua. Inspired by chemacs)
  • use this approach with all the 4 directories under a folder (this will make removing easier)

Leave a comment