nvim-autopairs/README.md

322 lines
8.3 KiB
Markdown
Raw Normal View History

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
2021-01-12 00:00:47 +03:00
Requires neovim 0.5+
2021-01-10 05:44:39 +03:00
2021-01-10 05:47:01 +03:00
### Setup
2021-01-10 05:44:39 +03:00
``` lua
2021-07-31 18:13:26 +03:00
require('nvim-autopairs').setup{}
2021-01-10 05:44:39 +03:00
```
2021-01-10 05:47:01 +03:00
## Default values
2021-01-10 05:44:39 +03:00
``` lua
local disable_filetype = { "TelescopePrompt" }
local disable_in_macro = false -- disable when recording or executing a macro
local disable_in_visualblock = false -- disable when insert after visual block mode
2021-04-20 14:05:45 +03:00
local ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]],"%s+", "")
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
2021-05-24 08:04:37 +03:00
local check_ts = false
local map_bs = true -- map the <BS> key
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
```
### 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>
<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.
</h3>
2021-08-12 14:43:50 +03:00
``` lua
-- If you want insert `(` after select function or method item
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
local cmp = require('cmp')
2021-10-29 14:00:01 +03:00
cmp.event:on( 'confirm_done', cmp_autopairs.on_confirm_done({ map_char = { tex = '' } }))
2021-11-04 02:53:04 +03:00
-- add a lisp filetype (wrap my-function), FYI: Hardcoded = { "clojure", "clojurescript", "fennel", "janet" }
cmp_autopairs.lisp[#cmp_autopairs.lisp+1] = "racket"
2021-11-04 02:53:04 +03:00
2021-08-12 14:43:50 +03:00
```
</details>
2021-05-25 15:12:08 +03:00
<details>
<summary><b>coq_nvim</b></summary>
``` lua
local remap = vim.api.nvim_set_keymap
local npairs = require('nvim-autopairs')
npairs.setup({ map_bs = false, map_cr = false })
2021-04-18 04:20:56 +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
return npairs.esc('<c-e>') .. npairs.autopairs_cr()
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
[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-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 %
: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
: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
: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
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("$$","$$")
: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")
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
``` text
Before Input After
------------------------------------
( |)) ( ( (|))
```
2021-06-02 04:50:02 +03:00
``` lua
require('nvim-autopairs').setup({
enable_check_bracket_line = false
})
```
### 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
``` lua
2021-04-23 03:29:52 +03:00
require('nvim-autopairs').disable()
require('nvim-autopairs').enable()
require('nvim-autopairs').remove_rule('(') -- remove rule (
2021-11-22 00:15:25 +03:00
require('nvim-autopairs').clear_rules() -- clear all rules
2021-04-28 02:26:38 +03:00
require('nvim-autopairs').get_rule('"') -- get rule " then modify it
```
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 = { '{', '[', '(', '"', "'" },
pattern = string.gsub([[ [%'%"%)%>%]%)%}%,] ]], '%s+', ''),
2021-09-10 23:38:57 +03:00
offset = 0, -- Offset from pattern match
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
### 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)