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

如何在(Neo)Vim中高亮光标所在的最近括号对?

实现光标在括号内容中时高亮包裹的括号

原生(Neo)Vim配置方案

可以通过自定义自动命令和匹配规则实现,无需依赖插件:

Vimscript 版本(适用于Vim/NeoVim)

" 定义更新括号高亮的函数
function! HighlightSurroundingParens()
    " 清除之前的高亮匹配
    call matchdelete(get(g:, 'surround_parens_match_id', -1))
    let l:pos = getpos('.')
    let l:synstack = synstack(l:pos[1], l:pos[2])
    let l:found = 0

    " 遍历语法栈,定位括号范围
    for l:synid in l:synstack
        let l:synname = synIDattr(l:synid, 'name')
        " 匹配括号相关语法组(适配多数编程语言)
        if l:synname =~# '^paren' || l:synname =~# '^delimiter'
            let l:match_pos = searchpair('(', '', ')', 'nW')
            if l:match_pos > 0
                " 高亮左括号
                let l:left_col = matchstrpos(getline(l:pos[1]), '(', 0)[1] + 1
                let g:surround_parens_match_id = matchadd('Search', '\%' . l:pos[1] . 'l\%' . l:left_col . 'c(')
                " 高亮右括号
                let l:right_col = matchstrpos(getline(l:match_pos), ')', 0)[1] + 1
                call matchadd('Search', '\%' . l:match_pos . 'l\%' . l:right_col . 'c)')
                let l:found = 1
                break
            endif
        endif
    endfor

    if !l:found
        let g:surround_parens_match_id = -1
    endif
endfunction

" 光标移动时自动更新高亮
autocmd CursorMoved,CursorMovedI * call HighlightSurroundingParens()

" 初始化匹配ID
let g:surround_parens_match_id = -1

Lua 版本(适用于NeoVim)

local surround_parens_match_id = -1

local function highlight_surrounding_parens()
    -- 清除旧的高亮匹配
    if surround_parens_match_id ~= -1 then
        vim.fn.matchdelete(surround_parens_match_id)
        surround_parens_match_id = -1
    end

    local pos = vim.api.nvim_win_get_cursor(0)
    local line_num = pos[1]
    local col_num = pos[2] + 1 -- Vim列索引为1-based

    -- 获取当前位置的语法栈
    local synstack = vim.fn.synstack(line_num, col_num)
    local found = false

    for _, synid in ipairs(synstack) do
        local synname = vim.fn.synIDattr(synid, 'name')
        if synname:match('^paren') or synname:match('^delimiter') then
            local match_line = vim.fn.searchpair('(', '', ')', 'nW')
            if match_line > 0 then
                -- 高亮左括号
                local left_col = vim.fn.matchstrpos(vim.fn.getline(line_num), '(', 0)[2] + 1
                surround_parens_match_id = vim.fn.matchadd('Search', string.format('\\%%%dl\\%%%dc(', line_num, left_col))
                -- 高亮右括号
                local right_col = vim.fn.matchstrpos(vim.fn.getline(match_line), ')', 0)[2] + 1
                vim.fn.matchadd('Search', string.format('\\%%%dl\\%%%dc)', match_line, right_col))
                found = true
                break
            end
        end
    end
end

-- 绑定光标移动事件
vim.api.nvim_create_autocmd({'CursorMoved', 'CursorMovedI'}, {
    pattern = '*',
    callback = highlight_surrounding_parens
})

如果想自定义高亮样式,可以新增一个高亮组替换默认的Search:

" Vimscript 自定义高亮
hi SurroundParens guifg=#ff4500 guibg=NONE gui=bold

然后把matchadd里的'Search'替换成'SurroundParens'即可。

插件方案(适配多语言更稳定)

如果觉得原生配置太繁琐,推荐用基于Tree-sitter的插件实现:

使用 rainbow-delimiters.nvim

这个插件能精准识别括号层级,默认会高亮光标所在层级的括号,完美符合需求:

  1. 安装插件(以Packer为例):
use 'hiphish/rainbow-delimiters.nvim'
  1. 配置:
require('rainbow-delimiters').setup({
    strategy = {
        [''] = require('rainbow-delimiters.strategy.local'),
    },
    highlight = {
        'RainbowDelimiterRed',
        'RainbowDelimiterYellow',
        'RainbowDelimiterBlue',
        'RainbowDelimiterOrange',
        'RainbowDelimiterGreen',
        'RainbowDelimiterViolet',
        'RainbowDelimiterCyan',
    },
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 14:40:49