Skip to content

Instantly share code, notes, and snippets.

@svallory
Last active August 24, 2024 02:22
Show Gist options
  • Save svallory/3b5ba661b791c8e3f97094945da07e8b to your computer and use it in GitHub Desktop.
Save svallory/3b5ba661b791c8e3f97094945da07e8b to your computer and use it in GitHub Desktop.
Executable Markdown - Fusing Code and Docs

nvims

Important

This is an executable Markdown file, an experiment of merging documentation and code together. A tool for running the code is under development. The focus of this document is to figure a syntax that does not get in the way of the code.

Neovim Flavours Manager

Original version from elijahmanor's gist

Easily manage and switch between multiple Neovim configurations using a fuzzy finder.

How It Works

The script uses NVIM_APPNAME to switch between configs which are stored in ~/.config/nvim-flavours/.

You can add new configs by placing them in the configs directory and the script will automatically include them in the selection list.

Usage

  • nvims [FILE, ...FILES]: Select and launch a Neovim config
  • nvims --help: Show usage and current settings

Shortcuts

  • Ctrl+N: Quick access to nvims

Config

See ~/.config/nvim-flavours/

The script

#!/bin/bash

The nvims command will check for NeoVim flavors in the $NVIM_FLAVOURS_DIR directory, which defaults to $HOME/.config/nvim-flavours if not set.

# Set NVIM_FLAVOURS_DIR only if it's not already defined
if [ -z "${NVIM_FLAVOURS_DIR}" ]; then
    export NVIM_FLAVOURS_DIR="$HOME/.config/nvim-flavours"
fi

We cache the list of flavors in the NVIM_FLAVOURS array, so if you add a new flavor, you'll need to restart the shell.

export NVIM_FLAVOURS=("default" $(ls $NVIM_FLAVOURS_DIR))

These are the current values of those variables:

echo "NVIM_FLAVOURS_DIR: $NVIM_FLAVOURS_DIR"
echo "NVIM_FLAVOURS: ${NVIM_FLAVOURS[@]}"
__nvims_config_dir() {
    if [[ -z $1 || $1 == "default" ]]; then
        echo ""
    else
        echo "${NVIM_FLAVOURS_DIR}/$1"
    fi
}

Now hre's the real function!

function nvims() {

First let's check if the user has requested to see this help message.

    if ! @help "$*"; then

Then we use fzf to show a nice fuzzy finder with the list of flavors for you to pick from.

        config=$(printf "%s\n" "${NVIM_FLAVOURS[@]}" | fzf --prompt=" Neovim Config " --height=~50% --layout=reverse --border --exit-0)

If you change your mind and don't select anything, that's fine, we'll just exit.

        if [[ -z $config ]]; then
            echo "Nothing selected"
            return 0
        fi

If you picked one, we set the $NVIM_APPNAME env variable to the selected flavor and launch  Neovim 🚀!

        NVIM_APPNAME=$(__nvims_config_dir $config) nvim "$@"
    fi
}

Finally, we bind the nvims command to Ctrl+N in the shell.

bindkey -s '^n' "nvims\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment