Skip to content

Instantly share code, notes, and snippets.

@elijahmanor
Created March 27, 2023 04:59
Show Gist options
  • Save elijahmanor/b279553c0132bfad7eae23e34ceb593b to your computer and use it in GitHub Desktop.
Save elijahmanor/b279553c0132bfad7eae23e34ceb593b to your computer and use it in GitHub Desktop.
Neovim Switcher
alias nvim-lazy="NVIM_APPNAME=LazyVim nvim"
alias nvim-kick="NVIM_APPNAME=kickstart nvim"
alias nvim-chad="NVIM_APPNAME=NvChad nvim"
alias nvim-astro="NVIM_APPNAME=AstroNvim nvim"
function nvims() {
items=("default" "kickstart" "LazyVim" "NvChad" "AstroNvim")
config=$(printf "%s\n" "${items[@]}" | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0)
if [[ -z $config ]]; then
echo "Nothing selected"
return 0
elif [[ $config == "default" ]]; then
config=""
fi
NVIM_APPNAME=$config nvim $@
}
bindkey -s ^a "nvims\n"
@Armadillidiid
Copy link

Armadillidiid commented Mar 30, 2023

For anyone who uses bash

# Neovim config switcher
alias nvim-chad="NVIM_APPNAME=nvim-chad nvim"

nvims() {
  items=("default" "NvChad")
  config=$(printf "%s\n" "${items[@]}" | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0)
  if [[ -z $config ]]; then
    echo "Nothing selected"
    return 0
  elif [[ $config == "default" ]]; then
    config=""
  fi
  NVIM_APPNAME=$config nvim $@
}

bind -x '"\C-a": nvims'

@Yumshot
Copy link

Yumshot commented Mar 30, 2023

For anyone who uses bash

# Neovim config switcher
alias nvim-chad="NVIM_APPNAME=nvim-chad nvim"

nvims() {
  items=("default" "NvChad")
  config=$(printf "%s\n" "${items[@]}" | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0)
  if [[ -z $config ]]; then
    echo "Nothing selected"
    return 0
  elif [[ $config == "default" ]]; then
    config=""
  fi
  NVIM_APPNAME=$config nvim $@
}

bind -x '"\C-a": nvims'

I believe I posted a bash one already, was something wrong with it?

@doctorfree
Copy link

doctorfree commented Mar 31, 2023

I'm not sure why we need a bash version since the zsh version works fine in bash. The only change I can detect between the zsh version and the two bash versions above is the use of the keyword function in the zsh version. But bash supports the function keyword.

However, bindkey does need to be replaced with bind for bash users. This can be accomplished while maintaining only a single version of this nvims gist by testing for the SHELL version variable:

if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then
   bindkey -s ^a "nvims\n"
elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then
   bind -x '"\C-a": nvims'
else
   bindkey -s ^a "nvims\n"
fi

@Yumshot
Copy link

Yumshot commented Mar 31, 2023

I'm not sure why we need a bash version since the zsh version works fine in bash. The only change I can detect between the zsh version and the two bash versions above is the use of the keyword function in the zsh version. But bash supports the function keyword.

However, bindkey does need to be replaced with bind for bash users. This can be accomplished while maintaining only a single version of this nvims gist by testing for the SHELL version variable:

if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then
   bindkey -s ^a "nvims\n"
elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then
   bind -x '"\C-a": nvims'
else
   bindkey -s ^a "nvims\n"
fi

Thats fair, Im a fish shell user myself, so I just figured if it was different in fish, it would probably differ in bash.

@doctorfree
Copy link

The other thing I would change is the use of ctrl-a as the key binding since that is already bound to move to the beginning of the line. I changed it to ctrl-n in my adaptation.

@doctorfree
Copy link

I've adapted this gist for use in Lazyman. In that project, the lazyman command installs and initializes Neovim configurations. For each new Neovim configuration it installs it also adds that config directory to the file .nvimdirs. I modified the gist to look for that file and if it exists use it to setup the aliases and nvims function. If it does not find that file then it sets up the aliases and nvims function for the supported Lazyman Neovim configurations if they are found.

Since I am using this gist in my project I wanted to share it with you in case there is anything you find useful. I had to fiddle a bit to keep it working in both bash and zsh. In particular, I wanted the nvims function to skip the fuzzy selection if there is only one config in the array but zsh and bash handle arrays differently. I came up with a compatible way to do that. Enjoy and thanks!

# $HOME/.config/nvim-Lazyman/.lazymanrc
# This file should be sourced from the shell initialization file
# e.g. $HOME/.bashrc or $HOME/.zshrc
#
# To use Vim
command -v vim > /dev/null && alias vi='vim'
# To use NeoVim
command -v nvim > /dev/null && {
  alias vi='nvim'
  items=()
  [ -d ${HOME}/.config/nvim ] && {
    alias nvim-default="NVIM_APPNAME=nvim nvim"
    items+=("default")
  }
  # Add all previously installed Neovim configurations
  if [ -f ${HOME}/.config/nvim-Lazyman/.nvimdirs ]
  then
    cat ${HOME}/.config/nvim-Lazyman/.nvimdirs | while read nvimdir
    do
      [ -d ${HOME}/.config/${nvimdir} ] && {
        alias ${nvimdir}="NVIM_APPNAME=${nvimdir} nvim"
        entry=$(echo ${nvimdir} | sed -e "s/nvim-//")
        items+=("${entry}")
      }
    done
  else
    # Add any supported config we find
    [ -d ${HOME}/.config/nvim-Lazyman ] && {
      alias nvim-lazy="NVIM_APPNAME=nvim-Lazyman nvim"
      items+=("Lazyman")
    }
    [ -d ${HOME}/.config/nvim-LazyVim ] && {
      alias nvim-lazy="NVIM_APPNAME=nvim-LazyVim nvim"
      items+=("LazyVim")
    }
    [ -d ${HOME}/.config/nvim-Kickstart ] && {
      alias nvim-kick="NVIM_APPNAME=nvim-Kickstart nvim"
      items+=("Kickstart")
    }
    [ -d ${HOME}/.config/nvim-NvChad ] && {
      alias nvim-chad="NVIM_APPNAME=nvim-NvChad nvim"
      items+=("NvChad")
    }
    [ -d ${HOME}/.config/nvim-AstroNvim ] && {
      alias nvim-astro="NVIM_APPNAME=nvim-AstroNvim nvim"
      items+=("AstroNvim")
    }
    [ -d ${HOME}/.config/nvim-Allaman ] && {
      alias nvim-aman="NVIM_APPNAME=nvim-Allaman nvim"
      items+=("Allaman")
    }
    [ -d ${HOME}/.config/nvim-LunarVim ] && {
      alias nvim-lunar="NVIM_APPNAME=nvim-LunarVim nvim"
      items+=("LunarVim")
    }
    [ -d ${HOME}/.config/nvim-MultiVim ] && {
      alias nvim-multi="NVIM_APPNAME=nvim-MultiVim nvim"
      items+=("MultiVim")
    }
    [ -d ${HOME}/.config/nvim-SpaceVim ] && {
      alias nvim-space="NVIM_APPNAME=nvim-SpaceVim nvim"
      items+=("SpaceVim")
    }
  fi

  function nvims() {
    numitems=${#items[@]}
    if [ ${numitems} -eq 1 ]
    then
      config="${items[@]:0:1}"
    else
      config=$(printf "%s\n" "${items[@]}" | fzf --prompt=" Neovim Config  " --height=60% --layout=reverse --border --exit-0)
    fi
    if [[ -z $config ]]; then
      echo "Nothing selected"
      return 0
    elif [[ $config == "default" ]]; then
      config="nvim"
    else
      if [ -d ${HOME}/.config/nvim-${config} ]
      then
        config="nvim-${config}"
      else
        [ -d ${HOME}/.config/${config} ] || {
          echo "Cannot locate ${config} Neovim configuration directory"
          return 0
        }
      fi
    fi
    NVIM_APPNAME=$config nvim $@
  }
  if [ -n "$($SHELL -c 'echo $ZSH_VERSION')" ]; then
   bindkey -s ^n "nvims\n"
  elif [ -n "$($SHELL -c 'echo $BASH_VERSION')" ]; then
   bind -x '"\C-n": nvims'
  else
   bindkey -s ^n "nvims\n"
  fi
}

@doctorfree
Copy link

I made additional changes to my adaptation of this gist, fixing some bash/zsh compatibility issues, breaking the key binding out into a separate shell initialization file, and adding an alias for nvims which sources the shell initialization file and runs the function. This last bit, making nvims an alias, was motivated by me adding and removing Neovim configurations frequently. As a shell function, I found I needed to either logout and log back in or source the file every time configs were added or deleted. Using an alias which does the sourcing allows me to have a dynamically generated menu without having to logout or manually source anything.

So, disregard my previous comment and, if you want to see how I'm doing things with nvims examine https://github.com/doctorfree/nvim-lazyman/blob/main/.lazymanrc

Please excuse the verbose comments. I've got it sorted out to my liking now and won't comment further. Thanks for this gist, I really like it.

@pannet1
Copy link

pannet1 commented Apr 4, 2023

in xonsh shell, it seems to be the simplest of all. just setting up the alias did the magic. of course, i opted out from the fzf menu.

aliases['nvim-astro']="$NVIM_APPNAME='AstroVim'; nvim"

there is plugin called zoxide for xonsh that provides me the menu anyway. so not missing fzf menu.

@hopezh
Copy link

hopezh commented Apr 16, 2023

Here's a PowerShell suggestion. Unfortunately aliases are very limited so I opted for a function and it isn't possible to bind hotkeys AFAIK. Add this to $PROFILE

function nvim_lazy()
{
  $env:NVIM_APPNAME="nvim-lazy"
  nvim $args
}

function nvims()
{
  $items = "default", "nvim-lazy"
  $config = $items | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0

  if ([string]::IsNullOrEmpty($config))
  {
    Write-Output "Nothing selected"
    break
  }
 
  if ($config -eq "default")
  {
    $config = ""
  }

  $env:NVIM_APPNAME=$config
  nvim $args
}

--edit: formatting, description and args

Thanks for sharing. Allow me to take some time to digest your script because I'm new to PowerShell.

Here's my simple config:

# --- neovim config switcher -------------------------------------------
function nvimAstro {
    $Env:NVIM_APPNAME = 'AstroVim'
    nvim 
}

function nvimLazy {
    $Env:NVIM_APPNAME = 'LazyVim'
    nvim 
}

function nvimNvChad {
    $Env:NVIM_APPNAME = 'NvChad'
    nvim 
}

function nvimDefault {
    $Env:NVIM_APPNAME = 'nvim'
    nvim 
}

set-alias -name nvim-astr -value nvimAstro
set-alias -name nvim-lazy -value nvimLazy
set-alias -name nvim-chad -value nvimNvChad
set-alias -name nvim-defa -value nvimDefault

@stekks
Copy link

stekks commented Apr 17, 2023

This is not the place to discus Powershellm but your aliases are redundant because you can execute the function straight from the prompt. Also your functions don't provide an argument to nvim like 'nvim-lazy my_script.ps1'

@bitrxjs
Copy link

bitrxjs commented Apr 19, 2023

emoji at
config=$(printf "%s\n" "${items[@]}" | fzf --prompt
not encoded properly

@RayneScofield
Copy link

in terminal input nvims and enter
not a valid number: ~50
nothing selected
what's wrong?

@rdlu
Copy link

rdlu commented Apr 26, 2023

Just a correction for fish-shell binding:

bind \ca nvims

Fish already does the <Enter> for me, so the last line of @Yumshot line is not working for me.

(Fedora 38, Fish-shell 3.6.1 + tmux inside alacritty here)

@hopezh
Copy link

hopezh commented May 11, 2023

Here's a PowerShell suggestion. Unfortunately aliases are very limited so I opted for a function and it isn't possible to bind hotkeys AFAIK. Add this to $PROFILE

function nvim_lazy()
{
  $env:NVIM_APPNAME="nvim-lazy"
  nvim $args
}

function nvims()
{
  $items = "default", "nvim-lazy"
  $config = $items | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0

  if ([string]::IsNullOrEmpty($config))
  {
    Write-Output "Nothing selected"
    break
  }
 
  if ($config -eq "default")
  {
    $config = ""
  }

  $env:NVIM_APPNAME=$config
  nvim $args
}

--edit: formatting, description and args

Thanks for sharing.

May I ask how to select from the menu as generated from your script in PowerShell?

@stekks
Copy link

stekks commented May 15, 2023

Rep

Just to be sure: you have to have 'fzf' installed (I use 'scoop' for installing stuff in Powershell). Then you can select items by just typing some characters of the selection you want (fuzzy finder) or select with cursor keys.

@georgejean
Copy link

georgejean commented May 17, 2023

For anyone who uses bash

# Neovim config switcher
alias nvim-chad="NVIM_APPNAME=nvim-chad nvim"

nvims() {
  items=("default" "NvChad")
  config=$(printf "%s\n" "${items[@]}" | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0)
  if [[ -z $config ]]; then
    echo "Nothing selected"
    return 0
  elif [[ $config == "default" ]]; then
    config=""
  fi
  NVIM_APPNAME=$config nvim $@
}

bind -x '"\C-a": nvims'

⚠ NVIM_APPNAME(s) in alias and items (configs other than default) must match.

@weedatom
Copy link

weedatom commented Jun 3, 2023

Here's a PowerShell suggestion. Unfortunately aliases are very limited so I opted for a function and it isn't possible to bind hotkeys AFAIK. Add this to $PROFILE

function nvim_lazy()
{
  $env:NVIM_APPNAME="nvim-lazy"
  nvim $args
}

function nvims()
{
  $items = "default", "nvim-lazy"
  $config = $items | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0

  if ([string]::IsNullOrEmpty($config))
  {
    Write-Output "Nothing selected"
    break
  }
 
  if ($config -eq "default")
  {
    $config = ""
  }

  $env:NVIM_APPNAME=$config
  nvim $args
}

--edit: formatting, description and args

How Does It Works?

@andyantrim
Copy link

So to make this a little easier (having to type nvims each time gets a bit old) I have the following:

alias vi="nvim"

alias nvim-lazy="NVIM_APPNAME=LazyVim nvim"
alias nvim-chad="NVIM_APPNAME=NvChad nvim"
alias nvim-astro="NVIM_APPNAME=AstroNvim nvim"

function nvims() {
  items=("default" "LazyVim" "NvChad" "AstroNvim")
  config=$(printf "%s\n" "${items[@]}" | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0)
  if [[ -z $config ]]; then
    echo "Nothing selected"
    return 0
  elif [[ $config == "default" ]]; then
    config=""
  else
    alias vi="NVIM_APPNAME=${config} nvim"
  fi
  NVIM_APPNAME=$config nvim $@
}

This has the same behavior, but rewrites the vi alias to take you to the last selected nvim distro. The idea here is nvims can then become a context switcher, that sets the nvim distro/version which can be opened in the future with vi command.

@doctorfree
Copy link

@andyantrim that's a pretty cool trick. I am attempting to incorporate this in the Lazyman definition of nvims. The problem I am trying to solve is continuing to allow the user to override the NVIM_APPNAME environment variable value while also enabling this persistent alias. That is, once the alias has been set, subsequent invocations of vi always set NVIM_APPNAME to whatever was previously selected. The user can no longer override that value other than rerunning nvims and selecting another config.

I'm experimenting with something like the following:

alias vi="NVIM_APPNAME=${NVIM_APPNAME:-${config}} nvim"

With this slightly revised alias, if the user has NVIM_APPNAME set then that is used, if not then the selected config is used in the alias. That is, what persists as an alias for vi is an NVIM_APPNAME setting either 1) from the environment, or 2) from the selection.

Does this make sense?

@QMaxPowerQ
Copy link

in terminal input nvims and enter not a valid number: ~50 nothing selected what's wrong?

any news on this?
I do have the same error while just using nvims without any argument. Using WSL2 - bash

@saccarosium
Copy link

Here a POSIX shell script that can switch between configurations and remember the last selected one.

#!/bin/sh

config=""
history="${XDG_CACHE_HOME:-$HOME/.cache}/nvim-configuration"
items="default kickstart LazyVim NvChad AstroNvim"

trap 'echo "$config" >"$history"' EXIT

case $1 in
    -C | --config)
        if [ -z "$2" ]; then
            config=$(echo "$items" | tr ' ' '\n' | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0) || exit 0
            shift
        else
            config="$2"
            shift
            shift
        fi
        ;;
    *)
        config=$(cat "$history")
        ;;
esac

[ "$config" = "default" ] && config=""

NVIM_APPNAME="${config:-""}" nvim "$@"

Usage

NOTE All input extra input is passed to nvim

  • -C without extra input: prompt with fzf to select a configuration
  • -C with extra input: selects passed configuration
  • without any flag: use the last selected one or default

@ajmasia
Copy link

ajmasia commented Sep 14, 2023

I have created this script so that I can use it in a more customised way 😄

#!/usr/bin/env bash

# ANSI color codes
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
CLEAR='\033[0m'

DEFAULT_INSTANCE=nvim-default

show_help() {
	echo -e "Usage: ${YELLOW}nvim-starter ${PURPLE}[COMMAND]${CYAN} [NVIM_OPTIONS...]${CLEAR}"
	echo
	echo "Available commands: (no command start nvim natively)"
	echo -e "  ${PURPLE}sel, select${CLEAR}     Show a selector to choose the Neovim configuration"
	echo -e "  ${PURPLE}def, default${CLEAR}    Start the default instance of Neovim"
	echo -e "  ${PURPLE}help${CLEAR}            Display this help message"
}

select_config() {
	local items=("default" "nvim" "nvim-learning")
	local config=$(printf "%s\n" "${items[@]}" | fzf --prompt=" Neovim Config: " --height=~50% --layout=reverse --exit-0)

	if [[ -z $config ]]; then
		echo "${GREEN}Nothing selected${CLEAR}"
		exit 0
	elif [[ $config == "default" ]]; then
		config=$DEFAULT_INSTANCE
	fi

	NVIM_APPNAME=$config nvim "$@"
}

default_exec() {
	# Default behavior (no script commds)
	nvim "$@"
	exit 0
}

[[ $# -eq 0 ]] && default_exec

while [[ $# -gt 0 ]]; do
	key="$1"

	case $key in
	help)
		show_help
		exit 0
		;;
	sel | select)
		shift # remove the -s or --select
		select_config "$@"
		exit 0
		;;
	def | default)
		shift # remove the -d or --default
		NVIM_APPNAME=$DEFAULT_INSTANCE nvim "$@"
		exit 0
		;;
	*)
		default_exec
		;;
	esac
done

@soomtong
Copy link

soomtong commented Oct 14, 2023

thanks a lot!

my configuration of zshrc for nvims here. made in macOS.

function nvims() {
  local nv_items=("Vanilla" "LazyVim" "NvChad")
  local nv_app=$(printf "%s\n" "${nv_items[@]}" | fzf --prompt=" Neovim Config 󰶻  " --height=~50% --layout=reverse --border --exit-0)

  if [[ -z $nv_app ]]; then
    echo "Nothing selected"
    return 0
  elif [[ $nv_app == "Vanilla" ]]; then
    nv_app=""
  else
    echo "Set $nvims_config to $nv_app"
  fi

  echo "$nv_app" > "$nvims_config"
  alias vi="NVIM_APPNAME=${nv_app} nvim"

  NVIM_APPNAME=$nv_app nvim $@
}

# nvims
if [[ -x /usr/local/bin/nvim || -x /opt/homebrew/bin/nvim ]]; then
  nvims_config="${XDG_CACHE_HOME:-$HOME/.cache}/nvims"
  nvims_app=$(cat "$nvims_config")
  #echo "Bind Neovim to $nvims_app"
  alias vi="NVIM_APPNAME=${nvims_app} nvim"
  export EDITOR="vi"
  bindkey -s "^v" "nvims\n"
fi

@ZediWards
Copy link

in terminal input nvims and enter not a valid number: ~50 nothing selected what's wrong?

any news on this? I do have the same error while just using nvims without any argument. Using WSL2 - bash

I had the same error.
Fix was to make height=50% NOT height=~50%
image

@hasecilu
Copy link

Due to some changes on Nerd Font you may want to update the glyphs to: " Neovim Config 󰄾 "

@wochap
Copy link

wochap commented Dec 5, 2023

For zsh, I refactored the function so you don't need to hardcode the options; it finds any folder in ~/.config with an init.lua file in it and considers it a nvim config folder. Additionally, it adds a preview using lsd to display the folder contents.

function nvims() {
  items=$(find $HOME/.config -maxdepth 2 -name "init.lua" -type f -execdir sh -c 'pwd | xargs basename' \;)
  selected=$(printf "%s\n" "${items[@]}" | FZF_DEFAULT_OPTS="${FZF_DEFAULT_OPTS-} --preview-window 'right:border-left:50%:<40(right:border-left:50%:hidden)' --preview 'lsd -l -A --tree --depth=1 --color=always --blocks=size,name ~/.config/{} | head -200'" fzf )
  if [[ -z $selected ]]; then
    return 0
  elif [[ $selected == "nvim" ]]; then
    selected=""
  fi
  NVIM_APPNAME=$selected nvim "$@"
}
alias nvs=nvims
Preview:

@hasecilu
Copy link

hasecilu commented Dec 5, 2023

For zsh, I refactored the function so you don't need to hardcode the options;

Nice, works well with eza too.

@Armadillidiid
Copy link

Armadillidiid commented Dec 5, 2023

@wochap I tried your script on my setup and it doesn't seem to detect symlinks. I store all my configs under ~/.dotfiles and generate respective symlinks using Stow. To work around it, I simply added an extra folder to search.

	items=$(
		find $HOME/.config -maxdepth 2 -name "init.lua" -type f -execdir sh -c 'pwd | xargs basename' \;
		find $HOME/.dotfiles -maxdepth 5 -name "init.lua" -type f -execdir sh -c 'pwd | xargs basename' \;
	)

@doctorfree
Copy link

Rather than adding an extra folder to search, the find command can be altered either with:

find $HOME/.config -maxdepth 2 -name "init.lua" "(" -type f -o -type l ")" -execdir sh -c 'pwd | xargs basename' \;

or

find -L $HOME/.config -maxdepth 2 -name "init.lua" -type f -execdir sh -c 'pwd | xargs basename' \;

Either of those will pickup both plain files and symbolic links.

I also ran into a problem on an Ubuntu 20.04 system with the older version of fzf in their repo. It did not understand the preview options:

invalid preview window layout: right:border-left:50%:<40(right:border-left:50%:hidden)

I simplified the preview options to get it to work on that system:

selected=$(printf "%s\n" "${items[@]}" | FZF_DEFAULT_OPTS="${FZF_DEFAULT_OPTS-} --preview-window=right:50% --preview 'lsd -l -A --tree --depth=1 --color=always --blocks=size,name ~/.config/{} | head -200'" fzf )

@mikeslattery
Copy link

mikeslattery commented Oct 22, 2024

My turn.

This uses the directory names of existing configurations. Someone else did the same, but my flavor is slightly shorter. Follows symlinks.

function nvims() {
    find -L "${XDG_CONFIG_HOME:-$HOME/.config}" -mindepth 2 -maxdepth 2 -name init.lua -o -name init.vim | \
        awk -F/ '{print $(NF-1)}' | \
        fzf --prompt 'Neovim config' --layout=reverse --border --exit-0 |\
        xargs -d$'\n' -n1 bash -c 'NVIM_APPNAME="$1" nvim' --
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment