Vim 配色方案显示错误的颜色,仅在 .vimrc 写入后才能更正

Vim 配色方案显示错误的颜色,仅在 .vimrc 写入后才能更正

我已经将 Vim 设置为使用 Solarized 配色方案,但每次打开 Vim 时,它都会显示错误的颜色。此外,我的.vimrc文件中有一行告诉 VIM.vimrc每次保存时都要重新加载。当我打开.vimrc并只在其中输入一个字符时:w,颜色会自动更正。

.vimrc这是启用自动重新加载的 行:

au BufWritePost .vimrc so ~/.vimrc  

我的 Vim 是 7.4 版本,由我自己编译,在 Ubuntu 14.04 上运行,控制台也已更改为 Solarized。

作为参考,这是我的完整vimrc文件:

set nocompatible              " be iMproved, required
filetype off                  " required

set shell=/bin/bash

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" alternatively, pass a path where Vundle should install bundles
"let path = '~/some/path/here'
"call vundle#rc(path)

" let Vundle manage Vundle, required
Bundle 'gmarik/vundle'

" The following are examples of different formats supported.
" Keep bundle commands between here and filetype plugin indent on.
" scripts on GitHub repos
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
"Bundle 'rstacruz/sparkup', {'rtp': 'vim/'}
" scripts from http://vim-scripts.org/vim/scripts.html
"Bundle 'FuzzyFinder'
" scripts not on GitHub
"Bundle 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
"Bundle 'file:///home/gmarik/path/to/plugin'
" ...
"
Bundle 'L9'

Bundle 'scrooloose/nerdcommenter'
Bundle 'scrooloose/nerdtree'
"Bundle 'vim-scripts/Conque-Shell'
Bundle 'bling/vim-airline'

Bundle 'tpope/vim-fugitive'

Bundle 'Lokaltog/vim-easymotion'
Bundle 'tpope/vim-surround'
Bundle 'ervandew/supertab'
Bundle 'kien/ctrlp.vim'
Bundle 'Raimondi/delimitMate'

"Bundle 'lepture/vim-css'
"Bundle 'groenewege/vim-less'
Bundle 'hail2u/vim-css3-syntax'
Bundle 'groenewege/vim-less'

Bundle 'Valloric/YouCompleteMe'
Bundle 'scrooloose/syntastic'

Bundle 'xolox/vim-session'
Bundle 'xolox/vim-misc'

Bundle 'altercation/vim-colors-solarized'

Bundle 'jelera/vim-javascript-syntax'
"Bundle 'pangloss/vim-javascript'
Bundle 'nathanaelkane/vim-indent-guides'
Bundle 'marijnh/tern_for_vim'
Bundle 'vim-scripts/JavaScript-Indent'
Bundle 'othree/javascript-libraries-syntax.vim'


filetype plugin indent on     " required


" ycm
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_confirm_extra_conf = 0

" solarized theme
syntax enable
set background=dark
let g:solarized_termcolors=16
color solarized

" session manager
:let g:session_autosave = 'no'

" fix end & home buttons
set term=xterm-256color

" airline
set laststatus=2
let g:airline_powerline_fonts = 1

" list files for open
set wildmenu

" reselect visual block after indent
vnoremap < <gv
vnoremap > >gv

" easy split nav
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" Y behav
map Y y$

"move on wrapeed lines
nnoremap j gj
nnoremap k gk

" clear search highlights
"noremap <silent><Leader>/ :nohls<CR>

" automatically reload vimrc when it's saved
au BufWritePost .vimrc so ~/.vimrc

" leader remap
let mapleader = ","

" jk as esc
inoremap jk <Esc>
inoremap kj <Esc>



" absolute line numbers in insert mode, relative otherwise for easy
" movement
"au InsertEnter * :set nu
"au InsertLeave * :set rnu

" show current file in nerd tree
map <silent> <C-s> :NERDTree<CR><C-w>p:NERDTreeFind<CR>

" show line numbers
set number

" search case insensitive
set ignorecase
set smartcase

" searhc incremental
set incsearch

" ctrl-p
let g:ctrlp_map = '<c-p>'
let g:ctrlp_cmd = 'CtrlPMRUFiles' " by default ctrlp opens in mru + files mode
set wildignore+=*/tmp/*,*.so,*.swp,*.zip     " ignore shitty files

" ctrlp on enter, open in a tab. on c-e, open in current.
let g:ctrlp_prompt_mappings = {
\ 'AcceptSelection("e")': ['<c-e>', '<2-LeftMouse>'],
\ 'AcceptSelection("t")': ['<cr>'],
\ 'PrtCurEnd()':          ['<c-q>'],
\ }

let g:ctrlp_custom_ignore = '\v[\/]\.(git|hg|svn)$'
"let g:ctrlp_custom_ignore = {
  "\ 'dir':  '\v[\/]\.(git|hg|svn)$',
  "\ 'file': '\v\.(exe|so|dll)$',
  "\ 'link': 'some_bad_symbolic_links',
  "\ }

" nerd tree auto open on start
autocmd vimenter * if !argc() | NERDTree | endif

" nerd tree toggle with c-n
map <C-n> :NERDTreeToggle<CR>

" nerd tree auto close when opening a file
let NERDTreeQuitOnOpen = 1

" map Q to repeat last macro
nnoremap Q @@

" fold on syntax
set foldmethod=indent
set foldcolumn=0
set foldlevel=1

" set mouse
set mouse=a
" tab = 4 spaces
set tabstop=4
set expandtab
set shiftwidth=4

" css complete fix
"fu! InsertTabWrapper(direction)
  "let char_before = col('.') - 1
  "if !char_before || getline('.')[char_before - 1] !~ '\k'
"return "\<tab>"
  "elseif "backward" == a:direction
"return "\<c-p>"
  "else
"return "\<c-n>"
  "endif
"endfu

"inoremap <tab> <c-r>=InsertTabWrapper("forward")<cr>
"inoremap <s-tab> <c-r>=InsertTabWrapper("backward")<cr>

" javascript fold
au FileType javascript call JavaScriptFold()

" javascript libs
let g:used_javascript_libs = 'jquery,angularjs,angulaui'

" less compilation
nnoremap <Leader>m :w <BAR> !lessc % > %:t:r.css<CR><space>

" jump to last tab by leader tl
let g:lasttab = 1
nmap <leader>l :exe "tabn ".g:lasttab<CR>
au TabLeave * let g:lasttab = tabpagenr()

" map common typos
cabbrev W w
cabbrev Q q
cabbrev E e

" easier session
cabbrev S SaveSession
cabbrev O OpenSession

" nerd tree change current dir
let g:NERDTreeChDirMode = 2

" auto save the session on quit
let g:session_autosave = 'yes'

" auto save the session periodically
let g:session_autosave_periodic = 5


" fix backspace
set backspace=indent,eol,start  

事后想想,我猜第一张图片中的文字背景是正确的,但整个屏幕的背景是错误的。我仍然不知道为什么。

答案1

如果第二次执行后颜色正确color solarized,则可能是因为第二次执行命令时某些设置不同,即color solarized在 vimrc 中的命令之后进行的某些设置。对我来说最突出的是set term=xterm-256color。尝试将其放在以 开头的块之前syntax enable,看看是否有帮助。

我猜solarized.vimcolorscheme 文件会进行一些设置,这些设置取决于终端可用的颜色数量。如果启动 vim 时 $TERM 为“xterm”,则 vim 认为可用的颜色数量为 8。您可以通过以下方式启动 vim 来检查这一点

vim -N -u NONE

(这样你的 vimrc 就不会被获取)并执行

:set t_Co?

color solarized这是命令第一次执行时认为可用的颜色数量。执行该命令后,vimrc 将执行set term-xterm-256color,将可用颜色数量设置为 256。执行该命令不会对您的 colorscheme 产生任何影响。

但是,当你执行时:w,你的 vimrc 会再次被获取,这次当color solarized执行命令时,它将看到颜色数量为 256,并可能会相应地更改其调色板。

此外,继续像现在这样获取 vimrc 的源代码并不是一个好主意,因为这样你最终会得到自动命令的多个实例。例如,执行:w几次,然后执行:au TabLeave。你将看到 的实例列表let g:lasttab = tabpagenr(),每次获取 vimrc 时都会出现一个实例。每次发生 TabLeave 事件时,所有这些命令都会执行。

我不知道为什么每次写入缓冲区时您都想要获取 vimrc 文件,但我将把这个问题留给您自己解决。解决自动命令累积问题的一种方法是将它们全部放在 vimrc 中的一个组中,并在定义任何命令之前删除该组中的任何成员。让我们仅以您的两个自动命令为例。

augroup Vimrc
au! "Removes all autocommands in the Vimrc group.
au FileType javascript call JavaScriptFold() 
au TabLeave * let g:lasttab = tabpagenr()
augroup END

相关内容