Neovim is a powerful text editor known for its extensibility and customization options. One of its key strengths lies in its ability to integrate essential features for efficient coding, such as automatic pairing of brackets, braces, quotes, and other symbols. To achieve this, many users turn to the popular Neovim plugin called nvim-autopairs
.
The nvim-autopairs
plugin significantly enhances coding speed and minimizes errors by automatically inserting closing pairs as you type. In this blog post, we will introduce you to the functionalities of nvim-autopairs
and guide you through the setup process for your Neovim editor.
Table of Contents
Prerequisites
Before proceeding with the installation and usage of nvim-cmp
in Neovim, please ensure you have the following packages installed on your system:
Packer
Packer serves as a plugin manager for Neovim and is necessary to install the nvim-cmp
plugin. If you haven’t installed Packer yet, you can consider installing Packer first and then proceed with the rest of the tutorial.
nvim-cmp
nvim-cmp
is a robust plugin for autocompletion while you type, supporting a wide range of programming languages. It significantly boosts your coding skills and productivity by providing automatic completion of basic tags and variables. For enhanced functionality, you can integrate nvim-cmp
with nvim-autopairs
to advance autocompletion for your code. We recommend installing nvim-cmp
before nvim-autopairs
. You can follow our detailed guides to set it up: How to Install and Use nvim-cmp Autocompletion.
Installing nvim-autopairs
Before proceeding with the installation of nvim-autopairs
in Neovim, let’s first familiarize ourselves with the project structure. It’s important to note that you can still follow this guide even if you have a different project structure, but exercise caution when editing the configuration files.
The project structure typically looks like this:
~/.config/
└── nvim
├── init.lua
└── lua
├── plugins.lua
├── autopairs.lua
├── .....
└── other configuration files
To install nvim-autopairs
, open the plugins.lua
file using the following command:
nvim ~/.config/nvim/lua/plugins.lua
If your plugins file has a different name or is located in a different directory, please open the file accordingly. After opening the plugins file, add the following two lines to install nvim-autopairs
:
-- Install nvim autopairs
use("windwp/nvim-autopairs")
Once you’ve added these lines to the plugins.lua
file, save it using the :w
command to install the plugins. Alternatively, you can use the following command to install the plugins:
:PackerInstall
After running this command, nvim-autopairs
should be successfully installed and ready to use in your Neovim editor.
Configuring nvim-autopairs
Now that you have successfully installed nvim-autopairs
, it’s time to configure it for automatic pairing of brackets, braces, quotes, and other symbols. To enable and set up nvim-autopairs
, you need to create or open the autopairs.lua
file. You can do this using the following command:
nvim ~/.config/nvim/lua/autopairs.lua
Once the file is open, add the following configuration lines:
-- Import nvim-autopairs safely
local autopairs_setup, autopairs = pcall(require, "nvim-autopairs")
if not autopairs_setup then
return
end
-- Configure autopairs
autopairs.setup({
check_ts = true, -- Enable treesitter
ts_config = {
lua = { "string" }, -- Don't add pairs in lua string treesitter nodes
javascript = { "template_string" }, -- Don't add pairs in JavaScript template_string treesitter nodes
java = false, -- Don't check treesitter on Java
},
})
-- Import nvim-autopairs completion functionality safely
local cmp_autopairs_setup, cmp_autopairs = pcall(require, "nvim-autopairs.completion.cmp")
if not cmp_autopairs_setup then
return
end
-- Import nvim-cmp plugin safely (completions plugin)
local cmp_setup, cmp = pcall(require, "cmp")
if not cmp_setup then
return
end
-- Make autopairs and completion work together
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
Once you’ve added these configuration lines, save the autopairs.lua
file.
However, for Neovim to actually use these configurations, you need to add the nvim-autopairs
setup to your init.lua
file so that it automatically loads the plugin when Neovim starts. To do this, open your init.lua
file with the following command:
nvim ~/.config/nvim/init.lua
Then add the following line to the bottom of the configuration file:
require "autopairs"
Save the init.lua
file and restart Neovim to enable nvim-autopairs
with the new configurations.
Disabling Autopairs for Specific Filetypes
While nvim-autopairs
is a very useful feature, there might be situations where you prefer not to use it. You have the flexibility to choose when to enable or disable autopairs
for specific filetypes. For instance, you may not want to use autopairs in the Telescope prompt when searching for files. To configure nvim-autopairs
to disable it for certain filetypes, you need to add the following extra lines to your autopairs configuration. If you don’t need this functionality, you can skip it.
Add the configuration as shown below:
-- Configure autopairs
autopairs.setup({
check_ts = true, -- Enable treesitter
ts_config = {
lua = { "string" }, -- Don't add pairs in lua string treesitter nodes
javascript = { "template_string" }, -- Don't add pairs in JavaScript template_string treesitter nodes
java = false, -- Don't check treesitter on Java
},
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",
},
})
After adding these lines to your configuration file, save the file and restart Neovim to apply the changes.
Conclusion
In this blog post, we explored how to set up and configure nvim-autopairs
to automatically close braces and symbols. With nvim-autopairs
automatically inserting closing pairs as you type, it streamlines your coding workflow, saves time, and reduces the chances of syntax errors.