/autoclose.nvim

A minimalist Neovim plugin that auto pairs & closes brackets

Primary LanguageLuaMIT LicenseMIT

autoclose.nvim

Stargazers Issues Contributors

📃 Introduction

A minimalist Neovim plugin that auto pairs & closes brackets written in 100% Lua.

⚙️ Functions

Most functions work in both insert and command-line mode.

Auto-close

Auto-delete

(works in <BS> and <C-W>)

Auto-escape

Auto-indent

(works in <CR> and <S-CR>, only in insert mode)

⚡ Requirements

📦 Installation

  1. Install via your favorite package manager.
Plug 'm4xshen/autoclose.nvim'
use 'm4xshen/autoclose.nvim'
  1. Setup the plugin in your init.lua.
require("autoclose").setup()

🔧 Configuration

You can pass your config table into the setup() function.

Keys

The available options in keys:

  • close: If set to true, pressing the character will insert both the opening and closing characters, and place the cursor in between them.
  • escape: If set to true, pressing the character again will escape it instead of inserting a closing character.
  • pair: The table that represents the pair of opening and closing characters. This should be a two-item (list-like) table, with the opening character first and the closing character second.
  • disabled_filetypes: Table of filetypes where the specific key should not be autoclosed.
  • enabled_filetypes: Only autoclose the key under these filetypes. This option takes precedence over disabled_filetypes.
  • disable_command_mode: If set to true, the character will be disabled in command-line mode.

Example: Add a $$ pair.

require("autoclose").setup({
   keys = {
      ["$"] = { escape = true, close = true, pair = { "$", "$" }, disabled_filetypes = {} },
   },
})

You can also overwrite the default config.

Example: Remove the escape function of >.

require("autoclose").setup({
   keys = {
      [">"] = { escape = false, close = false, pair = { "<", ">" }, disabled_filetypes = {} },
   },
})

Options

The available options in options:

  • disabled_filetypes: The plugin will be disabled under the filetypes in this table.
    • type of the value: table of strings
    • default value: { "text" }

Example: Disable the plugin in text and markdown file.

require("autoclose").setup({
   options = {
      disabled_filetypes = { "text", "markdown" },
   },
})
  • disable_when_touch: Set this to true will disable the auto-close function when the cursor touches character that matches touch_regex.

    • type of the value: boolean
    • default value: false
  • touch_regex

    • type of the value: string
    • default value: "[%w(%[{]" (alphanumeric characters or ( or [ or {)

Example:

Your current file: ( ^ points to your cursor position)

word
^

You press ( and the file will become

(word
^

It doesn't autoclose for you because your cursor touches w.

  • pair_spaces: Pair the spaces when cursor is inside a pair of keys.
    • type of the value: boolean
    • default value: false

Example:

The | is your cursor in insert mode.

import {|}

after inserting a space:

import { | }
  • auto_indent: Enable auto-indent feature

    • type of the value: boolean
    • default value: true
  • disable_command_mode: Disable autoclose for command mode globally

    • type of the value: boolean
    • default value: false

Default config

local config = {
   keys = {
      ["("] = { escape = false, close = true, pair = { "(", ")" } },
      ["["] = { escape = false, close = true, pair = { "[", "]" } },
      ["{"] = { escape = false, close = true, pair = { "{", "}" } },

      [">"] = { escape = true, close = false, pair = { "<", ">" } },
      [")"] = { escape = true, close = false, pair = { "(", ")" } },
      ["]"] = { escape = true, close = false, pair = { "[", "]" } },
      ["}"] = { escape = true, close = false, pair = { "{", "}" } },

      ['"'] = { escape = true, close = true, pair = { '"', '"' } },
      ["'"] = { escape = true, close = true, pair = { "'", "'" } },
      ["`"] = { escape = true, close = true, pair = { "`", "`" } },
   },
   options = {
      disabled_filetypes = { "text" },
      disable_when_touch = false,
      touch_regex = "[%w(%[{]",
      pair_spaces = false,
      auto_indent = true,
      disable_command_mode = false,
   },
}

autoclose.nvim vs other plugins

Some plugins such as nvim-autopairs and ultimate-autopair.nvim provide a wider range of features such as fast wrap, treesitter pair checking, etc., but some users may not need all of them. If you just want the basic functionality of editing with pairs, you can use autoclose.nvim to achieve the same thing in a simpler and faster way.