0

I've set up my Vim to use the Solarized color scheme, but every time I open Vim, it shows the wrong colors. In addition, I have a line in my .vimrc file that tells VIM to reload .vimrc every time it's saved. When I open .vimrc and only enter a single :w in it, the color is automatically corrected.

This is the line in .vimrc that enables that autoreload:

au BufWritePost .vimrc so ~/.vimrc  

My Vim is version 7.4, compiled by myself, running on Ubuntu 14.04, inside a console whose has been changed to Solarized too.

For reference, this is my complete vimrc file:

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  

As an afterthought, I guess the background of texts in the first image are correct, but the background of the whole screen is wrong. I'm still wondering why.

2
  • 4
    If the colors are correct after executing color solarized the second time, it's probably because of some setting that was different the second time the command was executed, that is, some setting that was made after the color solarized command in your vimrc. The one that stands out to me is set term=xterm-256color. Try putting that before the block beginning with syntax enable and see if that helps. Also, it's not a good idea to keep sourcing your vimrc as you do because you will wind up with multiple instances of your autocommands. E.g., execute :w a few times, then au TabLeave.
    – garyjohn
    Apr 30, 2014 at 17:50
  • right! I would be pleased if could you wrote this as an answer. Also, could you mind elaborating more on both points? Does this mean that at 2nd time, it will not set terminal to xterm? and I can see that after au tableave I get multiple tabpagenr()s but does that mean it is executed multiple times? or what?
    – sazary
    Apr 30, 2014 at 21:12

1 Answer 1

4

If the colors are correct after executing color solarized the second time, it's probably because of some setting that was different the second time the command was executed, that is, some setting that was made after the color solarized command in your vimrc. The one that stands out to me is set term=xterm-256color. Try putting that before the block beginning with syntax enable and see if that helps.

My guess is that the solarized.vim colorscheme file makes some setting that depends on the number of colors available from the terminal. If $TERM is "xterm" when you start vim, then the number of colors vim thinks are available will be 8. You can check this by starting vim as

vim -N -u NONE

(so that your vimrc is not sourced) and executing

:set t_Co?

This is the number of colors that the color solarized command will think are available the first time it is executed. After that command is executed, your vimrc executes set term-xterm-256color which will set the number of available colors to 256. The execution of that command will not have any effect on your colorscheme.

When you execute :w though, your vimrc is sourced again and this time when the color solarized command is executed, it will see the number of colors as 256 and may change its palette accordingly.

Also, it's not a good idea to keep sourcing your vimrc as you do because you will wind up with multiple instances of your autocommands. As an example, execute :w a few times, then execute :au TabLeave. You will see a list of instances of let g:lasttab = tabpagenr(), one for each time your vimrc was sourced. All of those commands will be executed each time the TabLeave event occurs.

I don't know why you would want to source your vimrc file every time you write a buffer, but I'll leave that to you. One way around the problem of accumulating autocommands would be to put them all in a group in your vimrc and delete any members of that group before defining any. Let's take just two of your autocommands as an example.

augroup Vimrc
au! "Removes all autocommands in the Vimrc group.
au FileType javascript call JavaScriptFold() 
au TabLeave * let g:lasttab = tabpagenr()
augroup END
1
  • I used auto sourcing when I was configuring my vim, I was doing several changes and I wanted to see effects immediately. maybe it is time to remove it.
    – sazary
    May 1, 2014 at 7:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .