1

If I open a random file (in this case, say .zshrc) in a blank vim with no rc file , but with vi compatibility forcing off

vim -N -u NORC ~/.zshrc

As expected vim will launch and display .zshrc without syntax highlighting.

attempt to enable syntax highlighting

I have a file containing a collection of highlight commands called darcula.vim in

~/.vim/colors/darcula.vim

contents of ~/.vim/colors/darcula.vim

"
" Darcula colorscheme for VIM.
"

set background=dark
highlight clear

if exists("syntax_on")
  syntax reset
endif

let g:colors_name = "darcula"

hi Cursor ctermfg=188 ctermbg=250 cterm=NONE guifg=#2b2b2b guibg=#a9b7c6 gui=NONE
hi Visual ctermfg=NONE ctermbg=24 cterm=NONE guifg=NONE guibg=#214283 gui=NONE
hi CursorLine ctermfg=NONE ctermbg=236 cterm=NONE guifg=NONE guibg=#323232 gui=NONE
hi CursorColumn ctermfg=NONE ctermbg=236 cterm=NONE guifg=NONE guibg=#313335 gui=NONE

[ ... plus many more highlight commands ...]

at vims command line I enter

:colorscheme darcula

then, to check if the highlight commands have been loaded, I run

:hi

and yes vim is showing all the hi commands from the above darcula.vim file have been loaded.
Then I run

:syntax enable

and nothing happens

from reading :h syntax-loading I thought the syntax enable command sets up filetype autocmd recognition, as verified with:

:filetype
filetype detection:ON  plugin:OFF  indent:OFF 

and also sets up autocommand which will run the vim function SynSet() on the autocommand event Syntax, from the file $VIMRUNTIME/syntax/synload.vim

[...]
au Syntax *     call s:SynSet()

fun! s:SynSet()
  " clear syntax for :set syntax=OFF  and any syntax name that doesn't exist
  syn clear
  if exists("b:current_syntax")
    unlet b:current_syntax
  endif

  let s = expand("<amatch>")
  if s == "ON"
    " :set syntax=ON
    if &filetype == ""
      echohl ErrorMsg
      echo "filetype unknown"
      echohl None
    endif
    let s = &filetype
  elseif s == "OFF"
    let s = ""
  endif

  if s != ""
    " Load the syntax file(s).  When there are several, separated by dots,
    " load each in sequence.
    for name in split(s, '\.')
      exe "runtime! syntax/" . name . ".vim syntax/" . name . "/*.vim"
    endfor
  endif
endfun

[...]

which looks like it will use the filetype and run the appropriate syntax file.

terminal

I'm running vim inside tmux and its not detecting my terminal properly:

:set t_Co?
  t_Co=8

so I set it to 256 color like it should be

:set t_Co=256

To me this looks like Ive covered everything, but I obviously havent, why isn't syntax highlighting happening here?

2 Answers 2

4

Adding t_Co=256 to your vimrc should never be necessarily, save for some highly unusual and archaic situations. It's typically a sign that something else is set up wrong.

By far the most common problem is a wrong TERM environment variable. You mentioned tmux, try using screen-256color, which should be the correct TERM for tmux. This is typically set in your shell's startup file (.zshrc, .zprofile, or .zshenv; different people use different files, and the "correct" one is a matter of religion).

You can try it first by just using:

$ export TERM=screen-256color
$ vim
:set t_Co?

You can also change the default for tmux in ~/.tmux.conf with set -g default-terminal "screen-256color". This is important for windows without shells (e.g. new-window vim). The default is screen.

3
  • thanks. I guess it must be better to set in terminal config? .tmux.conf? that way all shell instances should inherit the new $TERM Commented Feb 23, 2017 at 19:16
  • @the_velour_fog Well, you can do <Prefix>:new-window vim; you don't have to run a shell with tmux, so you should probably set it in both places. Commented Feb 23, 2017 at 19:21
  • that worked, thanks! I remember reading - I think in man tmux that tmux recommended not to set default-terminal - I think thats why I hadn't done it in the past. I'm guessing its better to set it as you have described as this will cause all the children processes to get the correct value Commented Feb 23, 2017 at 20:09
1

It turns out it was the tmux terminal detection - obviously beingis fundamental to everything else it seems the terminal state needs to be set right at launch, so launching with

vim -N -u NORC -c "set t_Co=256" ~/.zshrc

fixed the problem and the above steps did succeed in getting highlighting to work

3
  • You should accept your answer.
    – Davo
    Commented Feb 20, 2017 at 18:50
  • 1
    @Davo the stackexchange network wont allow you to accept your own answers for some period, like a day or so. Commented Feb 20, 2017 at 19:03
  • @carpetsmoker thanks. so the best place to do that would be my shell config file right? i.e. .zshrc or .tmux.conf Commented Feb 23, 2017 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.