m4xshen / autoclose.nvim

A minimalist Neovim plugin that auto pairs & closes brackets
MIT License
517 stars 14 forks source link
autoclose autopair autopairs lua neovim neovim-plugin nvim nvim-plugin

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.
    • vim-plug
      Plug '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:

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:

Example: Disable the plugin in text and markdown file.

require("autoclose").setup({
   options = {
      disabled_filetypes = { "text", "markdown" },
   },
})

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.

Example:

The | is your cursor in insert mode.

import {|}

after inserting a space:

import { | }

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.