Home

Awesome

<!-- markdownlint-disable MD013 MD033 MD041 --> <div align="center"> <p> <img src="assets/toggle-switch.png" align="center" alt="Toggle Logo" width="400" /> </p> <p> An extensible Neovim plugin for quickly toggling options. </p> </div>

Toggle is a modern, extensible Neovim Lua plugin for toggling options ala unimpaired.vim. The main use-case is to be able to quickly change something about your editing session. Consider the following scenarios:

If any of the above happen to you, then Toggle is worth a try!

Example session

tty Which Key integration

⚑️ Requirements

πŸ“¦ Installation

Install the plugin with your preferred package manager, such as Lazy:

{
  [1] = "gregorias/toggle.nvim",
  version = "2.0",
  config = true,
}

πŸš€ Usage

Toggle uses yo, because y looks like a toggle turned upside down.

πŸ› οΈ Configuration

Setup

The default configuration setup looks like so:

require"toggle".setup{
  keymaps = {
    toggle_option_prefix = "yo",
    previous_option_prefix = "[o",
    next_option_prefix = "]o",
    status_dashboard = "yos"
  },
  -- The interface for registering keymaps.
  keymap_registry = require("toggle.keymap").keymap_registry(),
  -- See the default options section below.
  options_by_keymap = …,
  --- Whether to notify when a default option is set.
  notify_on_set_default_option = true,
}

Default options

All default options use vim.notify for state changes.

OptionKeymapDescription
backgroundbdark-light switch
conceallevelcl0–3 slider with 0-sticky toggle
cursorline-on-off switch for cursorline
diffdon-off switch for diffthis/diffoff
diff allDoption for diffing all visible windows
listlon-off switch for list
numbernon-off switch for number
relativenumberron-off switch for relativenumber
wrapwon-off switch for wrap

Adding an option

You can define and add new options using the register function. For example, below is a snippet that registers an inlay hint buffer-local option with notifications:

-- This snippet happens in the context of an LSP attach event after checking
-- that the language server supports inlay hints.
local bufnr = … -- the current buffer
local toggle = require"toggle"
toggle.register(
  "i",
  -- Disables or enables inlay hints for the current buffer.
  toggle.option.NotifyOnSetOption(toggle.option.OnOffOption({
    name = "inlay hints",
    get_state = function()
      return vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr })
    end,
    set_state = function(new_value)
      vim.lsp.inlay_hint.enable(new_value, { bufnr = bufnr })
    end,
  })),
  { buffer = bufnr }
)

For more examples, see default-options.lua.

βœ… Comparison to Unimpaired

Unimpaired has more scope than Toggle, but it’s less extensible.

FeatureToggleUnimpaired
Which Key integrationβœ…βŒ
nvim-notify integrationβœ…βŒ
Extensible optionsβœ…βŒ
Configurable keybindingsβœ…βŒ
Slider (non-binary) option supportβœ…βŒ

πŸ™ Acknowledgments

The idea of quickly toggling options using yo, [o ]o came from unimpaired.vim.

The toggle SVG is a modified toggle icon from Arthur Shlain.

πŸ”— See also