vim - 如何在 vim 中更改当前缓冲区或 Pane 的背景颜色?

标签 vim

想象一下,我正在编码,并且打开了不同的拆分 Pane 。当我从一个缓冲区/ Pane 切换到另一个缓冲区/ Pane 时,我应该将哪些设置传递给 vimrc 以更改背景颜色?

我试过了:

autocmd BufEnter * highlight Normal ctermbg=black                                                                                                                                                                                                                              
autocmd BufLeave * highlight Normal ctermbg=white 

我想补充一点,我确定我启用了 256 种颜色

最佳答案

实际上,有一种方法可以获得这种效果。请参阅@blueyed 对此相关问题的回答:vim - dim inactive split panes .他提供了下面的脚本,当放置在我的 .vimrc 中时,它会使非事件窗口的背景变暗。实际上,它使它们的背景与为 colorcolumn 指定的颜色相同(指示所需文本宽度的垂直线)。

" Dim inactive windows using 'colorcolumn' setting
" This tends to slow down redrawing, but is very useful.
" Based on https://groups.google.com/d/msg/vim_use/IJU-Vk-QLJE/xz4hjPjCRBUJ
" XXX: this will only work with lines containing text (i.e. not '~')
" from 
if exists('+colorcolumn')
  function! s:DimInactiveWindows()
    for i in range(1, tabpagewinnr(tabpagenr(), '$'))
      let l:range = ""
      if i != winnr()
        if &wrap
         " HACK: when wrapping lines is enabled, we use the maximum number
         " of columns getting highlighted. This might get calculated by
         " looking for the longest visible line and using a multiple of
         " winwidth().
         let l:width=256 " max
        else
         let l:width=winwidth(i)
        endif
        let l:range = join(range(1, l:width), ',')
      endif
      call setwinvar(i, '&colorcolumn', l:range)
    endfor
  endfunction
  augroup DimInactiveWindows
    au!
    au WinEnter * call s:DimInactiveWindows()
    au WinEnter * set cursorline
    au WinLeave * set nocursorline
  augroup END
endif

关于vim - 如何在 vim 中更改当前缓冲区或 Pane 的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283410/

相关文章:

bash - 在 vim 命令行模式下 : how to kill the line from the current cursor position to the end

git - 如何保存更改并退出 Vim?

linux - 运行! less.sh 中的变量

regex - 在 Vim 中,如何交换 2 个不相邻的模式?

shell - VIM - shell 工作目录集成

Vim 有时不会在没有 "!"的情况下写入文件 (E13)

vim - 获取Vim脚本的<SNR>

vim - 显示 Vim 变量

regex - Vim 正则表达式跳过定界符

vim - JSLint 和 VIM 需要在 Windows 7 上手动提升 UAC