Auto Pairs for Neovim

Shaik Zahid
3 min readFeb 21, 2023

--

Auto-Pairs automatically completes the closed brace of any pair such as {} ,(), [] as this feature is not provided initially by Neovim. Auto-pairs solves this problem and it was developed by Trieu Le.

Requirements

  • Neovim ≥ 0.7

Installation

  • Install the plugin by adding the github reference to plugins.lua file.
return {

-- Alpha (Dashboard)
{
"goolord/alpha-nvim",
lazy = true,
},


-- Auto Pairs
-- Added This Plugin
{
"windwp/nvim-autopairs"
},

-- Bufferline
{
'akinsho/bufferline.nvim',
dependencies = {
'nvim-tree/nvim-web-devicons'
},
},

-- Colorscheme
{
'folke/tokyonight.nvim',
},

-- Hop (Better Navigation)
{
"phaazon/hop.nvim",
lazy = true,
},


-- Lualine
{
'nvim-lualine/lualine.nvim',
dependencies = {
'nvim-tree/nvim-web-devicons'
},
},


-- Language Support
{
'VonHeikemen/lsp-zero.nvim',
lazy = true,
branch = 'v1.x',
dependencies = {
-- LSP Support
{'neovim/nvim-lspconfig'}, -- Required
{'williamboman/mason.nvim'}, -- Optional
{'williamboman/mason-lspconfig.nvim'}, -- Optional

-- Autocompletion
{'hrsh7th/nvim-cmp'}, -- Required
{'hrsh7th/cmp-nvim-lsp'}, -- Required
{'hrsh7th/cmp-buffer'}, -- Optional
{'hrsh7th/cmp-path'}, -- Optional
{'saadparwaiz1/cmp_luasnip'}, -- Optional
{'hrsh7th/cmp-nvim-lua'}, -- Optional

-- Snippets
{'L3MON4D3/LuaSnip'}, -- Required
{'rafamadriz/friendly-snippets'}, -- Optional
}
},


-- Nvimtree (File Explorer)
{
'nvim-tree/nvim-tree.lua',
lazy = true,
dependencies = {
'nvim-tree/nvim-web-devicons',
},
},


-- Telescope (Fuzzy Finder)
{
'nvim-telescope/telescope.nvim',
lazy = true,
dependencies = {
{'nvim-lua/plenary.nvim'},
}
},


-- Treesitter
{
"nvim-treesitter/nvim-treesitter",
},


-- Toggle Term
{
'akinsho/toggleterm.nvim',
config = true
},

-- Which-key
{
'folke/which-key.nvim',
lazy = true,
},

}
  • Install the plugin using Shift + i by invoking Lazy.

Configuration

Add Plugins

  • Create a new file in Lua directory named autopairs-config.lua file.
  • Add this code to configure Auto-pairs.
-- autopairs-config.lua 

-- Setup autopairs with nvim-cmp.
local status_ok, npairs = pcall(require, "nvim-autopairs")
if not status_ok then
return
end


npairs.setup {
check_ts = true,
ts_config = {
lua = { "string", "source" },
javascript = { "string", "template_string" },
java = false,
},
disable_filetype = { "TelescopePrompt", "spectre_panel" },
fast_wrap = {
map = "<M-e>",
chars = { "{", "[", "(", '"', "'" },
pattern = string.gsub([[ [%'%"%)%>%]%)%}%,] ]], "%s+", ""),
offset = 0, -- Offset from pattern match
end_key = "$",
keys = "qwertyuiopzxcvbnmasdfghjkl",
check_comma = true,
highlight = "PmenuSel",
highlight_grey = "LineNr",
},
}


local cmp_autopairs = require "nvim-autopairs.completion.cmp"
local cmp_status_ok, cmp = pcall(require, "cmp")
if not cmp_status_ok then
return
end
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done { map_char = { tex = "" } })

Main Configuration

  • Source above file to the main configuration init.lua file
require "options"
require "keymaps"
require "lazy-config"
require "alpha-config"
require "autopairs-config" -- Added this line
require "bufferline-config"
require "hop-config"
require "nvim-tree-config"
require "lualine-config"
require "lsp-config"
require "telescope-config"
require "toggleterm-config"
require "treesitter-config"
require "whichkey"

Neovim From Scratch

  • If you want a complete installation and configuration of Neovim from Scratch, then you can head over to my NEOVIM SERIES.
  • This series is updated regularly, with updates and inclusion of newer plugins which improves the wholesome IDE experience of Neovim.

--

--