windwp / nvim-autopairs Goto Github PK
View Code? Open in Web Editor NEWautopairs for neovim written in lua
License: MIT License
autopairs for neovim written in lua
License: MIT License
With this configuration:
require('nvim-autopairs').setup({
pairs_map = {
["'"] = "'",
['"'] = '"',
['('] = ')',
['['] = ']',
['{'] = '}',
['`'] = '`',
-- python strings (working)
["b'"] = "'",
["f'"] = "'",
["r'"] = "'",
-- python strings (error)
['b"'] = '"',
['f"'] = '"',
['r"'] = '"',
}
})
I get error when typing f"
.
E116: Invalid arguments for function MPairs.autopairs("f"",""")
Btw, is there any way to configure extra autoclose rules based on filetype?
If a user has a typed word, and the completion menu is also opened, pressing a paired character , such as '(' or '{' will delete the user's text, including the character, and instead insert the closing character from the pair.
For example, if the user has typed Upload|
and the pum is visible, pressing (
would result in )
. Instead, it should have resulted in Upload(|)
I made simple mapping, so I could use ctrl+v in insert mode:
-- <C-r> - id doing paste from default copy register in insert mode
vim.api.nvim_set_keymap("i", "<C-v>", '<C-r>+', { noremap = true } ) --and disable 2MMB so no accidental paste..
vim.api.nvim_set_keymap("c", "<C-v>", '<C-r>+', { noremap = true } ) --and disable 2MMB so no accidental paste..
It worked ok some time ago but now this code:
config=function() require("nv-minimap")
becomes - after paste in insert mode with ctrl+v :
config=function) requirenv-minimap()()"""")
The 'funny' thing is pasting in inset mode with my mapping work if there is no space char before cursor position. So if I paste inside word then everything works ok. For now I switched to this version: b8272f5 and problem is not preset (was introduced in latter commits)
First of all, thanks for creating this nifty plugin.
I am trying to set up the pair ['``'] = "''"
. These is how LaTeX
understands double quotation. The pairing partially works, but instead of getting |'' as it should, I get
'|'. The cursor enters between the second single quotes. This may be due to the previous default pairing ["'"] = "'"
, but I would hope that autopairs is smarter than that.
On the same topic, for LaTeX purposes I would also like to setup paring like \(
is paired to \)
- this is math mode. I tried ['\\('] = '\\)'
, it is does not seem to work. The single space does not work either. Any ideas there?
I do not consider this to be extra feature but rather improvement on base feature.
Currently this configuration:
require('nvim-autopairs').setup({
pairs_map = {
['`'] = '`',
['```'] = '```',
}
})
Does not work as expected (expected behavior: `|
-> `|`
, ```|
-> ```|```
).
Hi there! I would love to be able to disable the insertion of matching pair when I'm in the middle of a word. For example:
require"some_text"|.foobar() -- | is the cursor, do not insert matching pair here
require"some_text" |.foobar() -- | is the cursor, do not insert matching pair here
require"some_text".foobar| -- | is the cursor, insert matching pair here
(|) -- | is the cursor, insert matching pair here, also applies to all matches like [] and {}
Basically, I don't want it to insert a matching pair if the right side of the cursor is not empty or it's not a symbol like what I've defined in the setup. ((){}[]
). Thanks in advance!
edit: this seems to work already, but it still inserts a pair if the next character is a .
What happens:
Before Input After
------------------------------------
{ {
| } }|
} }
------------------------------------
What I Want to Happen:
Before Input After
------------------------------------
{ {
| }
} }|
------------------------------------
Is there any way to achieve this?
If Treesitter is not installed, then the plugin gives an error:
Is it possible to turn this dependency into optional? I have a minimal configuration that allows me to use Neovim to edit text fields in browser (using Firenvim) and do not need Treesitter for it.
I am newbie to this plugin. So, without nvim-autopairs,it works like this.
int functionname() {
|
with , nvim-autopairs , i get this,
int functionname() {
|}
I wanted this.
int functionname() {
|
}
Please help me. I want closing brace to be on next line
Hi, good plugin. Thanks for your work.
Current
Before Input After
------------------------------------
( | ) ) ( )| )
I wish
Before Input After
------------------------------------
( | ) ) ( )|
How can I get this behavior?
Hello,
I'm having an issue that is quite annoying and I don't know how to solve it. This is what happens:
update(set|))
// input: (
update(set(|))
// input: )
update(set()|)
// input: backspace
update(set(|)
//input: backspace
update(set|
autopairs is constantly eating my closing brackets, and I don't know why, and I don't know how to configure it so it doesn't do that. Note that the count of the parens is not always supposed to make sense, in the example I included a closing parens that pairs with an opening parens earlier in a previous line (as maybe that's relevant with how the plugin counts).
What I'd want is that when I open a parens, it also inserts the closing parens, especially if the next parens in the line is already paired with an opening one.
Here's an example of a good behavior I'd like
update(set|))
// input: (
update(set(|)))
and
update(set()|))
// input: backspace
update(set|))
or
update(set()|))
// input: backspace
update(set(|))
// input: backspace
update(set|))
Let me know if that doesn't make sense, or if this maybe isn't the plugin to do this with
Did this with minimal config, only calling autopairs with:
require('nvim-autopairs').setup()
Before | Input | Expected | Actual |
---|---|---|---|
|foobar | ( | (|)foobar | (|foobar |
|.foobar | ( | (|).foobar | (|.foobar |
|+foobar | ( | (|)+foobar | (|+foobar |
Neovim version:
NVIM v0.5.0-dev+1367-g27c616d68 Build type: RelWithDebInfo LuaJIT 2.1.0-beta3
nvim-autopairs version:
b581620 (current)
I wonder if there is any plan to support angle brackets or <>
When pressing any of the keys ',`,"
within any other pairs, a single symbol is added rather than skipping the closing symbol
For example, starting at this position where the cursor is |
("|")
Expected behavior:
(""|)
Actual behavior:
(""|")
This is with default configuration settings
Hello, thanks for your plugin.
I'm trying to define a rule for a specific filetype but the sample code doesn't work
npairs.add_rules({
Rule("$", "$",{"tex", "latex"})
-- don't add a pair if the previous character is xxx
:with_pair(cond.not_before_regex_check("xxx", 3))
}
)
The only way I can get it to work is to leave off the filetype.
Here is my full configuration
require('nvim-autopairs').setup()
local Rule = require('nvim-autopairs.rule')
local remap = vim.api.nvim_set_keymap
local npairs = require('nvim-autopairs')
-- skip it, if you use another global object
_G.MUtils= {}
vim.g.completion_confirm_key = ""
MUtils.completion_confirm=function()
if vim.fn.pumvisible() ~= 0 then
if vim.fn.complete_info()["selected"] ~= -1 then
return vim.fn["compe#confirm"](npairs.esc("<cr>"))
else
return npairs.esc("<cr>")
end
else
return npairs.autopairs_cr()
end
end
remap('i' , '<CR>','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true})
-- you can use some builtin condition
local cond = require('nvim-autopairs.conds')
require('nvim-autopairs').clear_rules() -- <=== I clear the default rules on purpose. This is the only way my defined rule works
npairs.add_rules({
Rule("(", ")")
:with_pair(cond.not_before_regex_check("\\", 1)) -- <== I only want this rule to apply on latex files; adding {"tex","latex","plaintex"} to the argument list had no effect. It just made the rule not apply.
}
)
Reading the readme to me is not clear if support html as example.
Right now I use https://github.com/docunext/closetag.vim but I am not sure if this plugin can be an alternative (of course this one support also other things).
Maybe a language list of supported so I don't need to check all the rules available if are good for the one I use it?
Hello,
After I did a PackerUpdate today the pairs are inserted the other way around )( ][.
Can you reproduce?
Thank you in advance.
I just noticed that the pairs are appearing inside the telescope prompt even if they are disabled. I am using the plugin with default options.
before on after
(|) <CR> (
|
)
how can I get this behavior? With curly and square brackets I have this behavior. I don't have any completion enable.
Hello @windwp ,
Initially, thank you for maintaining so many beautiful plugins for neovim.
Is there a way to disable the jump mode with a flag that I am missing even though many of the community likes it?
Current behavior:
Before Input After
------------------------------------
((someword|) ) ((someword)|
The one I wish for:
Before Input After
------------------------------------
((someword |) ) ((someword)|)
Not sure if my config in at fault here but 43020b1 introduced weird behavior for me (with the latest config example applied)
my autopairs config looks like this right now:
local remap = vim.api.nvim_set_keymap
local npairs = require('nvim-autopairs')
local Rule = require('nvim-autopairs.rule')
npairs.setup({
ignored_next_char = "[%w%.]",
check_ts = true,
ts_config = {
lua = {'string'},-- it will not add pair on that treesitter node
javascript = {'string'}
}
})
require('nvim-treesitter.configs').setup {
autopairs = {enable = true}
}
local ts_conds = require('nvim-autopairs.ts-conds')
-- press % => %% is only inside comment or string
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'}))
})
require("nvim-autopairs.completion.compe").setup({
map_cr = true, -- map <CR> on insert mode
map_complete = true -- it will auto insert `(` after select function or method item
})
while compe's looks like this:
local remap = vim.api.nvim_set_keymap
vim.opt.completeopt = { "menuone" , "noselect" } -- "noinsert"
require('compe').setup {
enabled = true;
autocomplete = true;
debug = false;
min_length = 1;
preselect = 'enable';
throttle_time = 80;
source_timeout = 200;
incomplete_delay = 400;
max_abbr_width = 100;
max_kind_width = 100;
max_menu_width = 100;
documentation = true;
source = {
path = true;
buffer = true;
calc = true;
nvim_lsp = true;
nvim_lua = true;
vsnip = true;
};
}
local t = function(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
local check_back_space = function()
local col = vim.fn.col('.') - 1
if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
return true
else
return false
end
end
-- Use (s-)tab to:
--- move to prev/next item in completion menuone
--- jump to prev/next snippet's placeholder
_G.tab_complete = function()
if vim.fn.pumvisible() == 1 then
return t "<C-n>"
elseif vim.fn.call("vsnip#available", {1}) == 1 then
return t "<Plug>(vsnip-expand-or-jump)"
elseif check_back_space() then
return t "<Tab>"
else
return vim.fn['compe#complete']()
end
end
_G.s_tab_complete = function()
if vim.fn.pumvisible() == 1 then
return t "<C-p>"
elseif vim.fn.call("vsnip#jumpable", {-1}) == 1 then
return t "<Plug>(vsnip-jump-prev)"
else
-- If <S-Tab> is not working in your terminal, change it to <C-h>
return t "<S-Tab>"
end
end
-- remap('i' , '<CR>','compe#confirm("<CR>")', {expr = true , noremap = true})
remap('i' , '<Tab>','v:lua.tab_complete()', {expr = true})
remap('s' , '<Tab>','v:lua.tab_complete()', {expr = true})
remap('i' , '<S-Tab>','v:lua.s_tab_complete()', {expr = true})
remap('s' , '<S-Tab>','v:lua.s_tab_complete()', {expr = true})
Now, after confirming a functon in compe's popup I have this kind of result:
foo((|))
In other words two pairs of parentheses are being inserted.
Did I understand the change incorrectly?
I've read both documentation and some plugin code, but couldn't find the way to disable a default pair in certain situation.
For example, my use case is to disable adding second "
inside '.vim' files when it is intended to be a comment string (for example, when there is only white space to the left of the first "
). I even tried "more simple" approach to completely disable "
-"
pair in '.vim' files, but couldn't figure out a way to do that.
My first impression was to use this: npairs.add_rules({Rule('"', '', 'vim')})
. It didn't work, although it works if I try to add new pair (for example, with npairs.add_rules({Rule('^', '^', 'vim')})
).
When trying to use multi-character pairs, such as ``` in markdown, out of the box the rules don't work at all, but when calling :lua require'nvim-autopairs'.on_attach()
or simply :edit
, then the the rules start to work correctly. This, however, isn't ideal, and I'm not really sure how it can be solved.
Do you think it is suitable to add the following rules to basic.lua?
Rule(' ', ' '):with_pair(function (opts)
local pair = opts.line:sub(opts.col, opts.col+2)
return vim.tbl_contains({'()', '[]', '{}'}, pair)
end)
So we have the following
before | insert | after |
---|---|---|
(|) |
( |) |
( | ) |
{|} |
{ |} |
{ | } |
[|] |
[ |] |
[ | ] |
Hi there, I think it would be better if break line rule works for all of matching pairs. Currently it only works for {}
.
const obj = {
|
} // this works
const arr = [
|] // this doesn't
note: |
is cursor
Currently the wiki lacks the "Edit" button, so only the maintainer can edit it.
My apologies for the bad title, but unsure how to accurately name this situation. However, when you open a block, hit new line and attempt to open another where the closing for the first is directly after your cursor the closing pair isn't generated.
val x = {
|}
At the |
if I was to enter {
I'd expect the following:
val x = {
{|}}`
However, I'm instead left with the following:
val x = {
{|}`
Note that this doesn't happen if there is a space between the cursor and the closing pair.
val x = {
| }
The example above would correctly produce:
val x = {
{|} }
As a follow-up to #3, I believe it has only been fixed for "
and not for '
. For example, given then following:
api.nvim_set_var('test_thing|')
api.nvim_set_var('test_thing'|)
api.nvim_set_var('test_thing'|')
When wanting to insert I'm
, nvim-autopairs will expand single quote resulting with I''
. It's all the same for cases such: haven't
, etc.
Is there a way to enable break line rule for all filetypes without having to list the filetypes manually? I'd love to be able to set something like break_line_filetype = { "*" }
and it enables the rule for all filetypes. Thanks in advance!
Having this scenario:
`this|`
Expected: `this`|
Actual: `this`|`
I am using autopairs for the past one month when I moved by init.vim to init.lua. Its a great plugin. I am trying to move from Vvs-code to nvim
I am using it with nvim-compe for autocomplete. Is it possible to enable the autopairs automatically when I say select a function from my auto complete with the cursor in the middle of the brackets and not triggering of the auto brackets when say I have selected a module from my autocomplete.
Hey, nice plugin so far.
'b3nj5m1n/kommentary'
-- insert I
-- insert {
{}'b3nj5m1n/kommentary'
-- Thats bad, I wanted to set the bracket after I am done with configuring the plugin options
Can you make a toggle to deactivate the behavior?
Or how do you work with this plugin?
Being forced to delete the bracket is very unfortunate for usability.
Everything is in the title :)
When I am in insert mode, and I write something which has some pairs: (test)
And then press the .
command to repeat. It will only repeat what is inside the pair: test
without the parenthesis.
Is this the desired behaviour? Have I messed up something in my config? Thanks in advance.
Hi, I wonder if it is possible to make this plugin expand pairs only on <CR>
, similar to https://github.com/rstacruz/vim-closer?
A minium vimrc
call plug#begin('~/.vim/plugged')
Plug 'windwp/nvim-autopairs'
call plug#end()
lua <<EOF
-- require('nvim-autopairs').setup(). -- commented out because I want to load it when first time in insert mode
EOF
But if I run lua require('nvim-autopairs').setup()
manually after nvim loaded, the plugin does not work correctly, there are no errors, but if I type (
, it does not auto pair.
If I remove comments in this line: -- require('nvim-autopairs').setup()
it works fine.
It also can not manually loaded with
packadd nvim-autopairs
lua require('nvim-autopairs').setup()
There are no errors, simply does not do the job
With packer, it also has some trouble to load if I use lazy loading and specify either keys
or event
to lazyloading the plugin.
support ``` for markdown code block?
support """ for python block string?
The sample mapping with nvim-compe gives the error attempt to call field 'autopairs_cr' (a nil value)
when CR
is pressed and it types a zero instead.
This is the config (taken from the README)
local remap = vim.api.nvim_set_keymap
local npairs = require('nvim-autopairs')
-- skip it, if you use another global object
_G.MUtils= {}
vim.g.completion_confirm_key = ""
MUtils.completion_confirm=function()
if vim.fn.pumvisible() ~= 0 then
if vim.fn.complete_info()["selected"] ~= -1 then
return vim.fn["compe#confirm"](npairs.esc("<c-r>"))
else
return npairs.esc("<cr>")
end
else
return npairs.autopairs_cr()
end
end
remap('i' , '<CR>','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true})
Have can I achieve indent with autopairs same to webstorm's one?
thanks for this awesome plugin!
i am using nvim-compe for various reasons.
the provided example in the readme lacks of one "end" statement. correction:
MUtils.completion_confirm = function()
if vim.fn.pumvisible() ~= 0 then
if vim.fn.complete_info()["selected"] ~= -1 then
vim.fn["compe#confirm"]()
return npairs.esc("<c-y>")
else
vim.defer_fn(function()
vim.fn["compe#confirm"]()
end, 20)
return npairs.esc("<c-n>")
end
else
return npairs.check_break_line_char()
end
end
even though i fixed syntax error, the plugin does not work with compe and i am not able to fix that :S
i setup auto-pairs after i setup compe.
what i type {<enter>
what i get:
{
<cursor>
the closing bracket is missing :S
any help appreciated !
/edit
i am ... damn! after switching to compe i forgot the setup() function for autopairs when copy pasting...
it is working now as expected and i will open a PR for the syntax fix :)
Hey, thank you so much for the great work on this plugin. It's fast and works quite seamlessly. I just had one question: I seem to be getting a print message whenever i open up nvim saying "you need to install treesitter". Treesitter is of course already installed (i use packer.nvim
). Even after running :PackerSync
and reloading, I still get this message.
I checked the code and I see where the error is coming from and I am confused why this line is not returning ok
for me. Any ideas?
This is the file where i configure nvim-autopairs
:
local npairs = require('nvim-autopairs')
local Rule = require('nvim-autopairs.rule')
-- skip it, if you use another global object
_G.MUtils= {}
vim.g.completion_confirm_key = ""
MUtils.completion_confirm=function()
if vim.fn.pumvisible() ~= 0 then
if vim.fn.complete_info()["selected"] ~= -1 then
return vim.fn["compe#confirm"](npairs.esc("<cr>"))
else
return npairs.esc("<cr>")
end
else
return npairs.autopairs_cr()
end
end
require("nvim-autopairs.completion.compe").setup({
map_cr = true, -- map <CR> on insert mode
map_complete = true -- it will auto insert `(` after select function or method item
})
npairs.setup({
check_ts = true,
ts_config = {
lua = {'string'},-- it will not add pair on that treesitter node
javascript = {'template_string'},
java = false,-- don't check treesitter on java
}
})
It would be very nice if there was an option to let the carriage return space things out properly for stuff like functions, etc. For instance, pressing enter in
int main() {|}
would yield
int main() {
|
}
where |
is the location of the cursor. Thanks for your work on this plugin!
So this doesn't really have anything to do with autopairs really but I am using nvim-autopairs's esc function to try and write a mapping for tab and shift-tab with completion-nvim and vsnip and was hoping you could help me and/or are maybe using something similar for your personal neovim setup.
This is what I am using. Whe
MUtils.tab=function()
if vim.fn.pumvisible() ~= 0 then
return npairs.esc("<C-n>")
else
if vim.fn["vsnip#available"](1) ~= 0 then
return npairs.esc("<Plug>(vsnip-expand-or-jump)")
else
return npairs.esc("<Tab>")
end
end
end
MUtils.s_tab=function()
if vim.fn.pumvisible() ~= 0 then
return npairs.esc("<C-p>")
else
if vim.fn["vsnip#jumpable"](-1) ~= 0 then
return npairs.esc("<Plug>(vsnip-jump-prev)")
else
return npairs.esc("<C-h>")
end
end
end
Is there any reason to name the first 2 option as camelCase and the rest of them snake_case? I think it would be better if it has the same naming convention.
I'm getting extra character while using autopairs, after updating to 460cdfc .
Input :
{
Output:
{}[
I'm experiencing the bug with both my config and the minimal config:
Minimal config:
require('nvim-autopairs').setup()
My config:
local remap = vim.api.nvim_set_keymap
local npairs = require('nvim-autopairs')
local Rule = require('nvim-autopairs.rule')
-- skip it, if you use another global object
_G.MUtils= {}
vim.g.completion_confirm_key = ""
MUtils.completion_confirm=function()
if vim.fn.pumvisible() ~= 0 then
if vim.fn.complete_info()["selected"] ~= -1 then
return vim.fn["compe#confirm"](npairs.esc("<cr>"))
else
return npairs.esc("<cr>")
end
else
return npairs.autopairs_cr()
end
end
remap('i' , '<CR>','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true})
npairs.setup({
disable_filetype = { "TelescopePrompt" },
ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]],"%s+", ""),
enable_moveright = true,
enable_afterquote = true, -- add bracket pairs after quote
enable_check_bracket_line = true, --- check bracket in same line
check_ts = true,
ts_config = {
lua = {'string'},-- it will not add pair on that treesitter node
javascript = {'template_string'},
java = false,-- don't check treesitter on java
}
})
require('nvim-treesitter.configs').setup {
autopairs = {enable = true}
}
local ts_conds = require('nvim-autopairs.ts-conds')
-- press % => %% is only inside comment or string
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'}))
})
Using both configs I get [kind of] the same output. Why? I don't really know why, maybe you added some sort of verbosity?
Hope this gets solved soon. Thanks!
Edit: indeed latest update is broken(?), using this commit:
use {"windwp/nvim-autopairs", opt = true, commit = "b5816204bd2f92f1c64dff132fbd67a1530c1751"}
Works as expected
For some odd reason when using nvim-autopairs the icons from nvim-vim-devicons lose their color.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.