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"
@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' --
}

Slightly more readable update:

function nvims() {
    NVIM_APPNAME="$(
        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)" \
    nvim
}

@asethcore
Copy link

can anyone give the snippet for nushell please

@psifertex
Copy link

For anyone who uses the Fish shell.

function nvim-chad
    env NVIM_APPNAME=nvim-chad nvim
end

function nvim-yum
    env NVIM_APPNAME=nvim-yum nvim
end

function nvims
    set items nvim-yum nvim-chad
    set config (printf "%s\n" $items | fzf --prompt=" Neovim Config  " --height=~50% --layout=reverse --border --exit-0)
    if [ -z $config ]
        echo "Nothing selected"
        return 0
    else if [ $config = "default" ]
        set config ""
    end
    env NVIM_APPNAME=$config nvim $argv
end

bind \ca 'nvims\n'```

You should consider changing this to pass $argv to nvim in each of those functions so you can pass arguments. Otherwise you can't even use those aliases to open files from the CLI but have to use a picker from within neovim.

@ashishmgofficial
Copy link

I just set the NVIM_APPNAME env variable to the directory name as per the commit. And so I just use the nvims function to set the desired config as environment variable. After this I can just use the regular nvim command to start the neovim in my desired config:

-- .zshrc

export DEFAULT_NVIM=<Your default conf directory within .config>
export NVIM_APPNAME=${DEFAULT_NVIM}

# Function to dynamically find and select Neovim configurations
function nvims() {
  local config_dirs
  config_dirs=$(find -L "${XDG_CONFIG_HOME:-$HOME/.config}" -mindepth 2 -maxdepth 2 -name init.lua -o -name init.vim | awk -F/ '{print $(NF-1)}')
  
  local config
  config=$(printf "%s\n" "default" $config_dirs | 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=${DEFAULT_NVIM:-""}
  fi
  
  export NVIM_APPNAME=$config
}

You can set the default to your most frequently used

@rafisics
Copy link

bind \ca 'nvims\n'
Pressing ctrl+A shows:

❯ nvims
: command not found

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