I'm a relatively new neovim user. I use nvim-autopairs to always induce matching (
, [
, {
, '
, "
character pairs, and automatically placing my cursor inside them. As an example, in insert mode, if I type (
, I get ($)
, where $
represents my active cursor insert mode position.
I want to have a keybinding in insert mode to allow me to <Tab>
out of the ending pair, and remain in insert mode, ready to keep going. So I would like to type in insert mode, (x, y<Tab>
to obtain (x, y) $
, where again $
represents my cursor in insert mode ready to continue typing. Note the requirement of the extra space between )
and $
above.
I found this reddit thread which apparently does this keybinding, with a more advanced implementation in the same thread. I tried to implement the advanced implementation as follows in my init.lua
file:
-- tab out of `(['` etc in insert mode
vim.keymap.set('i', '<Tab>', getline('.')[col('.')-1] =~? '[]>)}]' || getline('.')[col('.')-1] =~? '[''"`]' && synIDattr(synID(line("."), col(".")+1, 1), "name") !~? 'string', {silent = true, expr = true, remap =false})
But I get a syntax error when restarting nvim. Could anyone please help me get this working and help ensure that the keymap is ported over to lua correctly? The latter is for my future configuration learning purposes.
<expr>
mapping and I think it's missing some code.? '<Right>' : '<Tab>'
part of the ternary)getline('.')[col('.')-1] =~? '[]>)}]' || getline('.')[col('.')-1] =~? '[''"]' && synIDattr(synID(line("."), col(".")+1, 1), "name") !~? 'string' ? '<Right>' : '<Tab>'
(I took the backtick out of the secondgetline
match for formatting; don't forget it if you want it). This is typical of an<expr>
mapping: check a condition and return different keys based on the result. It gives a single mapping dynamic behavior.