2021-01-10 05:44:39 +03:00
|
|
|
## nvim-autopairs
|
|
|
|
|
2021-11-22 00:15:25 +03:00
|
|
|
A super powerful autopair plugin for Neovim that supports multiple characters.
|
2021-01-10 05:44:39 +03:00
|
|
|
|
2022-08-13 16:34:01 +03:00
|
|
|
Requires neovim 0.7
|
2021-01-10 05:44:39 +03:00
|
|
|
|
2022-06-08 09:06:14 +03:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
Install the plugin with your preferred package manager:
|
|
|
|
|
|
|
|
### [vim-plug](https://github.com/junegunn/vim-plug)
|
|
|
|
|
|
|
|
```vim
|
|
|
|
Plug 'windwp/nvim-autopairs'
|
|
|
|
|
|
|
|
lua << EOF
|
|
|
|
require("nvim-autopairs").setup {}
|
|
|
|
EOF
|
|
|
|
```
|
|
|
|
|
|
|
|
### [packer](https://github.com/wbthomason/packer.nvim)
|
|
|
|
|
|
|
|
```lua
|
|
|
|
use {
|
|
|
|
"windwp/nvim-autopairs",
|
2022-06-09 15:45:05 +03:00
|
|
|
config = function() require("nvim-autopairs").setup {} end
|
2022-06-08 09:06:14 +03:00
|
|
|
}
|
2021-01-10 05:44:39 +03:00
|
|
|
```
|
2021-01-10 05:47:01 +03:00
|
|
|
|
2021-01-12 23:50:27 +03:00
|
|
|
## Default values
|
2021-01-10 05:44:39 +03:00
|
|
|
|
|
|
|
``` lua
|
|
|
|
local disable_filetype = { "TelescopePrompt" }
|
2021-10-14 08:10:33 +03:00
|
|
|
local disable_in_macro = false -- disable when recording or executing a macro
|
2021-12-02 11:40:08 +03:00
|
|
|
local disable_in_visualblock = false -- disable when insert after visual block mode
|
2022-10-20 16:10:01 +03:00
|
|
|
local disable_in_replace_mode = true
|
2022-11-24 02:24:11 +03:00
|
|
|
local ignored_next_char = [=[[%w%%%'%[%"%.%`%$]]=]
|
2021-04-23 02:46:48 +03:00
|
|
|
local enable_moveright = true
|
2021-05-24 08:04:37 +03:00
|
|
|
local enable_afterquote = true -- add bracket pairs after quote
|
2021-06-02 04:50:02 +03:00
|
|
|
local enable_check_bracket_line = true --- check bracket in same line
|
2022-03-25 04:32:47 +03:00
|
|
|
local enable_bracket_in_quote = true --
|
2022-08-01 12:52:22 +03:00
|
|
|
local enable_abbr = false -- trigger abbreviation
|
2022-06-15 01:28:10 +03:00
|
|
|
local break_undo = true -- switch for basic rule break undo sequence
|
2021-05-24 08:04:37 +03:00
|
|
|
local check_ts = false
|
2022-03-24 11:43:05 +03:00
|
|
|
local map_cr = true
|
2021-09-15 15:59:41 +03:00
|
|
|
local map_bs = true -- map the <BS> key
|
2021-12-17 13:48:41 +03:00
|
|
|
local map_c_h = false -- Map the <C-h> key to delete a pair
|
2021-11-22 00:15:25 +03:00
|
|
|
local map_c_w = false -- map <c-w> to delete a pair if possible
|
2021-01-10 05:44:39 +03:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2021-01-12 23:50:27 +03:00
|
|
|
### Override default values
|
2021-01-10 05:44:39 +03:00
|
|
|
|
|
|
|
``` lua
|
|
|
|
require('nvim-autopairs').setup({
|
|
|
|
disable_filetype = { "TelescopePrompt" , "vim" },
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2021-05-25 15:12:08 +03:00
|
|
|
#### Mapping `<CR>`
|
2021-06-11 16:26:29 +03:00
|
|
|
```
|
|
|
|
Before Input After
|
|
|
|
------------------------------------
|
|
|
|
{|} <CR> {
|
|
|
|
|
|
|
|
|
}
|
|
|
|
------------------------------------
|
|
|
|
```
|
2021-05-25 15:12:08 +03:00
|
|
|
|
2021-08-12 14:43:50 +03:00
|
|
|
<details>
|
|
|
|
<summary><b>nvim-cmp</b></summary>
|
2021-10-28 07:59:55 +03:00
|
|
|
<h3>
|
|
|
|
You need to add mapping `CR` on nvim-cmp setup.
|
2021-11-22 00:15:25 +03:00
|
|
|
Check readme.md on nvim-cmp repo.
|
2021-10-28 07:59:55 +03:00
|
|
|
</h3>
|
2021-08-12 14:43:50 +03:00
|
|
|
|
|
|
|
``` lua
|
2021-10-28 07:59:55 +03:00
|
|
|
-- If you want insert `(` after select function or method item
|
|
|
|
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
|
|
|
|
local cmp = require('cmp')
|
2022-06-18 04:58:25 +03:00
|
|
|
cmp.event:on(
|
|
|
|
'confirm_done',
|
|
|
|
cmp_autopairs.on_confirm_done()
|
|
|
|
)
|
|
|
|
```
|
2021-11-04 02:53:04 +03:00
|
|
|
|
2022-06-18 04:58:25 +03:00
|
|
|
You can customize the kind of completion to add `(` or any character.
|
2021-11-04 02:53:04 +03:00
|
|
|
|
2022-06-18 04:58:25 +03:00
|
|
|
```lua
|
|
|
|
local handlers = require('nvim-autopairs.completion.handlers')
|
|
|
|
|
|
|
|
cmp.event:on(
|
|
|
|
'confirm_done',
|
|
|
|
cmp_autopairs.on_confirm_done({
|
|
|
|
filetypes = {
|
|
|
|
-- "*" is a alias to all filetypes
|
|
|
|
["*"] = {
|
|
|
|
["("] = {
|
|
|
|
kind = {
|
|
|
|
cmp.lsp.CompletionItemKind.Function,
|
|
|
|
cmp.lsp.CompletionItemKind.Method,
|
|
|
|
},
|
|
|
|
handler = handlers["*"]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
lua = {
|
|
|
|
["("] = {
|
|
|
|
kind = {
|
|
|
|
cmp.lsp.CompletionItemKind.Function,
|
|
|
|
cmp.lsp.CompletionItemKind.Method
|
|
|
|
},
|
|
|
|
---@param char string
|
|
|
|
---@param item item completion
|
|
|
|
---@param bufnr buffer number
|
|
|
|
handler = function(char, item, bufnr)
|
|
|
|
-- Your handler function. Inpect with print(vim.inspect{char, item, bufnr})
|
|
|
|
end
|
|
|
|
}
|
|
|
|
},
|
|
|
|
-- Disable for tex
|
|
|
|
tex = false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2021-08-12 14:43:50 +03:00
|
|
|
```
|
2022-06-18 04:58:25 +03:00
|
|
|
|
|
|
|
Don't use `nil` to disable a filetype. If a filetype is `nil` then `*` is used as fallback.
|
|
|
|
|
2021-08-12 14:43:50 +03:00
|
|
|
</details>
|
2021-05-25 15:12:08 +03:00
|
|
|
<details>
|
2021-08-28 03:02:47 +03:00
|
|
|
<summary><b>coq_nvim</b></summary>
|
|
|
|
|
|
|
|
``` lua
|
|
|
|
local remap = vim.api.nvim_set_keymap
|
|
|
|
local npairs = require('nvim-autopairs')
|
|
|
|
|
2021-10-28 07:59:55 +03:00
|
|
|
npairs.setup({ map_bs = false, map_cr = false })
|
2021-04-18 04:20:56 +03:00
|
|
|
|
2021-08-28 03:02:47 +03:00
|
|
|
vim.g.coq_settings = { keymap = { recommended = false } }
|
|
|
|
|
|
|
|
-- these mappings are coq recommended mappings unrelated to nvim-autopairs
|
|
|
|
remap('i', '<esc>', [[pumvisible() ? "<c-e><esc>" : "<esc>"]], { expr = true, noremap = true })
|
|
|
|
remap('i', '<c-c>', [[pumvisible() ? "<c-e><c-c>" : "<c-c>"]], { expr = true, noremap = true })
|
|
|
|
remap('i', '<tab>', [[pumvisible() ? "<c-n>" : "<tab>"]], { expr = true, noremap = true })
|
|
|
|
remap('i', '<s-tab>', [[pumvisible() ? "<c-p>" : "<bs>"]], { expr = true, noremap = true })
|
|
|
|
|
|
|
|
-- skip it, if you use another global object
|
|
|
|
_G.MUtils= {}
|
|
|
|
|
|
|
|
MUtils.CR = function()
|
|
|
|
if vim.fn.pumvisible() ~= 0 then
|
|
|
|
if vim.fn.complete_info({ 'selected' }).selected ~= -1 then
|
|
|
|
return npairs.esc('<c-y>')
|
|
|
|
else
|
2021-09-22 10:16:03 +03:00
|
|
|
return npairs.esc('<c-e>') .. npairs.autopairs_cr()
|
2021-08-28 03:02:47 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
return npairs.autopairs_cr()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
remap('i', '<cr>', 'v:lua.MUtils.CR()', { expr = true, noremap = true })
|
|
|
|
|
|
|
|
MUtils.BS = function()
|
|
|
|
if vim.fn.pumvisible() ~= 0 and vim.fn.complete_info({ 'mode' }).mode == 'eval' then
|
|
|
|
return npairs.esc('<c-e>') .. npairs.autopairs_bs()
|
|
|
|
else
|
|
|
|
return npairs.autopairs_bs()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
remap('i', '<bs>', 'v:lua.MUtils.BS()', { expr = true, noremap = true })
|
|
|
|
```
|
|
|
|
</details>
|
2021-05-25 15:12:08 +03:00
|
|
|
<details>
|
|
|
|
<summary><b>without completion plugin</b></summary>
|
|
|
|
|
|
|
|
```lua
|
2021-10-20 04:37:03 +03:00
|
|
|
-- add option map_cr
|
|
|
|
npairs.setup({ map_cr = true })
|
2021-01-31 06:37:32 +03:00
|
|
|
```
|
2021-05-25 15:12:08 +03:00
|
|
|
</details>
|
2021-06-19 14:19:21 +03:00
|
|
|
|
2021-10-17 04:34:37 +03:00
|
|
|
[another completion plugin](https://github.com/windwp/nvim-autopairs/wiki/Completion-plugin)
|
|
|
|
|
2021-11-22 00:15:25 +03:00
|
|
|
If you have a problem with indent after you press ` <CR> `
|
|
|
|
please check the settings of treesitter indent or install a plugin that has indent support for your filetype.
|
2021-06-19 14:19:21 +03:00
|
|
|
|
2021-04-19 04:21:08 +03:00
|
|
|
### Rule
|
2021-01-31 06:37:32 +03:00
|
|
|
|
2021-11-22 00:15:25 +03:00
|
|
|
nvim-autopairs uses rules with conditions to check pairs.
|
2021-01-24 15:55:31 +03:00
|
|
|
|
2021-04-19 04:21:08 +03:00
|
|
|
``` lua
|
|
|
|
local Rule = require('nvim-autopairs.rule')
|
|
|
|
local npairs = require('nvim-autopairs')
|
|
|
|
|
2021-07-11 03:37:24 +03:00
|
|
|
npairs.add_rule(Rule("$$","$$","tex"))
|
2021-04-19 04:21:08 +03:00
|
|
|
|
2021-11-22 00:15:25 +03:00
|
|
|
-- you can use some built-in conditions
|
2021-04-19 04:21:08 +03:00
|
|
|
|
|
|
|
local cond = require('nvim-autopairs.conds')
|
|
|
|
print(vim.inspect(cond))
|
|
|
|
|
|
|
|
npairs.add_rules({
|
|
|
|
Rule("$", "$",{"tex", "latex"})
|
|
|
|
-- don't add a pair if the next character is %
|
2021-12-08 15:59:52 +03:00
|
|
|
:with_pair(cond.not_after_regex("%%"))
|
2021-04-19 04:21:08 +03:00
|
|
|
-- don't add a pair if the previous character is xxx
|
2021-12-08 15:59:52 +03:00
|
|
|
:with_pair(cond.not_before_regex("xxx", 3))
|
2021-04-19 04:21:08 +03:00
|
|
|
-- don't move right when repeat character
|
|
|
|
:with_move(cond.none())
|
|
|
|
-- don't delete if the next character is xx
|
2021-12-08 15:59:52 +03:00
|
|
|
:with_del(cond.not_after_regex("xx"))
|
2021-11-22 00:15:25 +03:00
|
|
|
-- disable adding a newline when you press <cr>
|
2021-04-19 04:21:08 +03:00
|
|
|
:with_cr(cond.none())
|
|
|
|
},
|
2021-11-22 00:15:25 +03:00
|
|
|
-- disable for .vim files, but it work for another filetypes
|
2021-10-01 16:37:15 +03:00
|
|
|
Rule("a","a","-vim")
|
2021-04-19 04:21:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
npairs.add_rules({
|
|
|
|
Rule("$$","$$","tex")
|
2021-05-19 17:43:56 +03:00
|
|
|
:with_pair(function(opts)
|
|
|
|
print(vim.inspect(opts))
|
2021-04-19 04:21:08 +03:00
|
|
|
if opts.line=="aa $$" then
|
|
|
|
-- don't add pair on that line
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
-- you can use regex
|
2021-11-22 00:15:25 +03:00
|
|
|
-- press u1234 => u1234number
|
2021-04-19 04:21:08 +03:00
|
|
|
npairs.add_rules({
|
2021-04-21 10:30:25 +03:00
|
|
|
Rule("u%d%d%d%d$", "number", "lua")
|
2021-04-19 04:21:08 +03:00
|
|
|
:use_regex(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2021-04-20 06:41:22 +03:00
|
|
|
|
2021-11-22 00:15:25 +03:00
|
|
|
-- press x1234 => x12341234
|
2021-04-19 04:21:08 +03:00
|
|
|
npairs.add_rules({
|
2021-04-21 10:30:25 +03:00
|
|
|
Rule("x%d%d%d%d$", "number", "lua")
|
2021-04-19 04:21:08 +03:00
|
|
|
:use_regex(true)
|
|
|
|
:replace_endpair(function(opts)
|
|
|
|
-- print(vim.inspect(opts))
|
|
|
|
return opts.prev_char:sub(#opts.prev_char - 3,#opts.prev_char)
|
|
|
|
end)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2021-04-20 06:41:22 +03:00
|
|
|
-- you can do anything with regex +special key
|
2021-11-22 00:15:25 +03:00
|
|
|
-- example press tab to uppercase text:
|
2021-04-20 06:41:22 +03:00
|
|
|
-- press b1234s<tab> => B1234S1234S
|
|
|
|
|
2021-04-21 07:39:36 +03:00
|
|
|
npairs.add_rules({
|
|
|
|
Rule("b%d%d%d%d%w$", "", "vim")
|
|
|
|
:use_regex(true,"<tab>")
|
|
|
|
:replace_endpair(function(opts)
|
|
|
|
return
|
|
|
|
opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char)
|
|
|
|
.."<esc>viwU"
|
|
|
|
end)
|
|
|
|
})
|
2021-08-06 18:29:27 +03:00
|
|
|
|
|
|
|
-- you can exclude filetypes
|
|
|
|
npairs.add_rule(
|
|
|
|
Rule("$$","$$")
|
2021-08-07 06:43:27 +03:00
|
|
|
:with_pair(cond.not_filetypes({"lua"}))
|
2021-08-06 18:29:27 +03:00
|
|
|
)
|
2021-04-19 04:21:08 +03:00
|
|
|
--- check ./lua/nvim-autopairs/rules/basic.lua
|
|
|
|
|
|
|
|
```
|
2021-07-14 16:40:07 +03:00
|
|
|
[Rules API](https://github.com/windwp/nvim-autopairs/wiki/Rules-API)
|
2021-04-19 04:21:08 +03:00
|
|
|
|
2021-04-21 07:39:36 +03:00
|
|
|
### Treesitter
|
2021-11-22 00:15:25 +03:00
|
|
|
You can use treesitter to check for a pair.
|
2021-04-21 07:39:36 +03:00
|
|
|
|
|
|
|
```lua
|
|
|
|
local npairs = require("nvim-autopairs")
|
2022-05-04 08:11:26 +03:00
|
|
|
local Rule = require('nvim-autopairs.rule')
|
2021-04-21 07:39:36 +03:00
|
|
|
|
|
|
|
npairs.setup({
|
|
|
|
check_ts = true,
|
|
|
|
ts_config = {
|
2021-09-16 04:58:35 +03:00
|
|
|
lua = {'string'},-- it will not add a pair on that treesitter node
|
2021-04-21 07:53:34 +03:00
|
|
|
javascript = {'template_string'},
|
2021-04-21 07:39:36 +03:00
|
|
|
java = false,-- don't check treesitter on java
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
local ts_conds = require('nvim-autopairs.ts-conds')
|
|
|
|
|
|
|
|
|
2021-11-22 00:15:25 +03:00
|
|
|
-- press % => %% only while inside a comment or string
|
2021-04-21 07:39:36 +03:00
|
|
|
npairs.add_rules({
|
|
|
|
Rule("%", "%", "lua")
|
|
|
|
:with_pair(ts_conds.is_ts_node({'string','comment'})),
|
|
|
|
Rule("$", "$", "lua")
|
|
|
|
:with_pair(ts_conds.is_not_ts_node({'function'}))
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2021-09-16 04:58:35 +03:00
|
|
|
### Don't add pairs if it already has a close pair in the same line
|
|
|
|
if **next character** is a close pair and it doesn't have an open pair in same line, then it will not add a close pair
|
2021-01-24 15:55:31 +03:00
|
|
|
|
|
|
|
``` text
|
|
|
|
Before Input After
|
|
|
|
------------------------------------
|
|
|
|
( |)) ( ( (|))
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2021-06-02 04:50:02 +03:00
|
|
|
``` lua
|
|
|
|
require('nvim-autopairs').setup({
|
|
|
|
enable_check_bracket_line = false
|
|
|
|
})
|
|
|
|
```
|
2021-01-27 18:02:17 +03:00
|
|
|
|
|
|
|
### Don't add pairs if the next char is alphanumeric
|
|
|
|
|
|
|
|
You can customize how nvim-autopairs will behave if it encounters a specific
|
|
|
|
character
|
|
|
|
``` lua
|
|
|
|
require('nvim-autopairs').setup({
|
|
|
|
ignored_next_char = "[%w%.]" -- will ignore alphanumeric and `.` symbol
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
``` text
|
|
|
|
Before Input After
|
|
|
|
------------------------------------
|
|
|
|
|foobar ( (|foobar
|
|
|
|
|.foobar ( (|.foobar
|
|
|
|
```
|
|
|
|
|
2021-04-29 14:24:41 +03:00
|
|
|
### Plugin Integration
|
2021-04-19 06:36:48 +03:00
|
|
|
``` lua
|
2021-04-23 03:29:52 +03:00
|
|
|
require('nvim-autopairs').disable()
|
|
|
|
require('nvim-autopairs').enable()
|
2021-10-14 14:13:51 +03:00
|
|
|
require('nvim-autopairs').remove_rule('(') -- remove rule (
|
2021-11-22 00:15:25 +03:00
|
|
|
require('nvim-autopairs').clear_rules() -- clear all rules
|
2022-09-17 07:54:27 +03:00
|
|
|
-- get rule " then modify it. It can return a list of rule or just a rule
|
2022-09-17 07:46:48 +03:00
|
|
|
require('nvim-autopairs').get_rule('"')
|
|
|
|
```
|
2021-04-28 02:26:38 +03:00
|
|
|
|
2022-09-17 07:46:48 +03:00
|
|
|
* Sample
|
|
|
|
```lua
|
|
|
|
-- remove add single quote on filetype scheme or lisp
|
|
|
|
require("nvim-autopairs").get_rule("'")[1].not_filetypes = { "scheme", "lisp" }
|
|
|
|
require("nvim-autopairs").get_rule("'")[1]:with_pair(cond.not_after_text("["}))
|
2021-04-19 06:36:48 +03:00
|
|
|
```
|
2022-09-17 07:46:48 +03:00
|
|
|
|
2021-06-25 06:01:48 +03:00
|
|
|
### FastWrap
|
|
|
|
|
|
|
|
``` text
|
|
|
|
Before Input After
|
|
|
|
--------------------------------------------------
|
|
|
|
(|foobar <M-e> then press $ (|foobar)
|
|
|
|
(|)(foobar) <M-e> then press q (|(foobar))
|
|
|
|
```
|
|
|
|
|
|
|
|
```lua
|
2021-09-16 04:58:35 +03:00
|
|
|
-- put this to setup function and press <a-e> to use fast_wrap
|
2021-06-25 06:01:48 +03:00
|
|
|
npairs.setup({
|
|
|
|
fast_wrap = {},
|
|
|
|
})
|
|
|
|
|
|
|
|
-- change default fast_wrap
|
|
|
|
npairs.setup({
|
|
|
|
fast_wrap = {
|
|
|
|
map = '<M-e>',
|
|
|
|
chars = { '{', '[', '(', '"', "'" },
|
2022-05-25 16:04:34 +03:00
|
|
|
pattern = [=[[%'%"%)%>%]%)%}%,]]=],
|
2021-06-25 06:01:48 +03:00
|
|
|
end_key = '$',
|
|
|
|
keys = 'qwertyuiopzxcvbnmasdfghjkl',
|
2021-06-25 14:21:39 +03:00
|
|
|
check_comma = true,
|
2021-10-17 15:55:59 +03:00
|
|
|
highlight = 'Search',
|
|
|
|
highlight_grey='Comment'
|
2021-06-25 06:01:48 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
```
|
2021-06-02 04:50:02 +03:00
|
|
|
|
2021-05-24 08:04:37 +03:00
|
|
|
### autotag html and tsx
|
2021-04-29 14:24:41 +03:00
|
|
|
|
|
|
|
[autotag](https://github.com/windwp/nvim-ts-autotag)
|
2021-04-20 14:38:56 +03:00
|
|
|
|
2021-04-19 06:36:48 +03:00
|
|
|
### Endwise
|
|
|
|
|
2021-07-14 16:40:07 +03:00
|
|
|
[endwise](https://github.com/windwp/nvim-autopairs/wiki/Endwise)
|
2021-01-31 06:37:32 +03:00
|
|
|
|
2021-05-21 03:12:14 +03:00
|
|
|
### Custom rules
|
|
|
|
[rules](https://github.com/windwp/nvim-autopairs/wiki/Custom-rules)
|
2022-12-07 05:11:05 +03:00
|
|
|
|
|
|
|
## Sponsors
|
|
|
|
|
|
|
|
Thanks to everyone who sponsors my projects and makes continued development maintenance possible!
|
|
|
|
<!-- patreon --><a href="https://github.com/t4t5"><img src="https://github.com/t4t5.png" width="60px" alt="" /></a><!-- patreon-->
|