You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vim中实现括号自动补全后按右括号跳转至括号外的方法

解决Vim右括号重复跳转问题

你需要给右括号、引号等添加映射,判断光标右侧是否已经存在对应的闭合符号,存在则直接跳转,不存在才插入符号。在现有配置基础上添加以下内容即可实现需求:

首先在vimrc中定义辅助函数:

function! ClosePair(char)
  if getline('.')[col('.') - 1] == a:char
    return "\<Right>"
  else
    return a:char
  endif
endfunction

然后添加右括号、引号的插入映射:

inoremap <silent> ) <c-r>=ClosePair(')')<cr>
inoremap <silent> ] <c-r>=ClosePair(']')<cr>
inoremap <silent> } <c-r>=ClosePair('}')<cr>
inoremap <silent> " <c-r>=ClosePair('"')<cr>
inoremap <silent> ' <c-r>=ClosePair("'")<cr>
inoremap <silent> ` <c-r>=ClosePair('`')<cr>

功能说明

  • ClosePair函数会检查光标下一个字符是否为目标闭合符号:
    • 若是,返回<Right>命令让光标直接跳到符号外侧
    • 若不是,返回对应符号执行插入操作
  • <silent>选项用于隐藏映射执行时的命令行反馈,保持界面简洁

配置完成后,当你在自动补全的括号中间按下右括号时,光标会直接跳转至外侧;如果光标右侧无对应闭合符号,会正常插入符号,不会出现重复情况。

内容的提问来源于stack exchange,提问作者xyz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 17:34:57