如果我可以输入 ]
在 vim 中会很棒(或其他一些字符,可能是 <C-]>
)并让它自动插入正确关闭左括号的任何括号。例如。如果我的缓冲区中有这个:
object(function(x) { x+[1,2,3
然后我按
]]]
, 字符 ]})
将被插入。如何做到这一点?
最佳答案
这是您可能想要的草图。内置函数 searchpair
和 searchpairpos
对各种文本编辑任务都有很大帮助:)
" Return a corresponding paren to be sent to the buffer
function! CloseParen()
let parenpairs = {'(' : ')',
\ '[' : ']',
\ '{' : '}'}
let [m_lnum, m_col] = searchpairpos('[[({]', '', '[\])}]', 'nbW')
if (m_lnum != 0) && (m_col != 0)
let c = getline(m_lnum)[m_col - 1]
return parenpairs[c]
endif
return ''
endfun
要舒适地使用它,请制作
imap
其中:imap <C-e> <C-r>=CloseParen()<CR>
编辑 :过度转义搜索正则表达式
\
被包括在搜索中。现在少了一个问题。
关于Vim 魔术结束括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6080286/