0

~/.vimrc:

set backspace=2
set nocompatible
set clipboard^=unnamed,unnamedplus

syntax on
set expandtab tabstop=2 shiftwidth=2
set smartindent
set hlsearch 
set incsearch 
set ignorecase
set number 
set t_Co=16

" Sample comment to test color
" These highlight lines are not loading/getting overriden somehow
hi Comment ctermfg=darkgreen
hi Type ctermfg=darkblue
hi Statement ctermfg=red
hi StorageClass ctermfg=darkblue
hi Statement ctermfg=magenta
hi Constant ctermfg=red
hi Function ctermfg=darkyellow

set cursorline
hi LineNr ctermfg=darkgray
hi CursorLine cterm=NONE
hi CursorLineNr cterm=NONE

So whenever I open vim, all the lines except for the lines from hi Comment to hi Function are loaded. Doing :scriptnames gives the O/P:

:scriptnames
  1: /usr/share/vim/vimrc
  2: ~/.vimrc
  3: /usr/share/vim/vim90/syntax/syntax.vim
  4: /usr/share/vim/vim90/syntax/synload.vim
  5: /usr/share/vim/vim90/syntax/syncolor.vim
  6: /usr/share/vim/vim90/colors/lists/default.vim
  7: /usr/share/vim/vim90/filetype.vim
  8: /usr/share/vim/vim90/scripts.vim
  9: /usr/share/vim/vim90/autoload/dist/script.vim
 10: /usr/share/vim/vim90/syntax/vim.vim
 11: /usr/share/vim/vim90/plugin/getscriptPlugin.vim
 12: /usr/share/vim/vim90/plugin/gzip.vim
 13: /usr/share/vim/vim90/plugin/logiPat.vim
 14: /usr/share/vim/vim90/plugin/manpager.vim
 15: /usr/share/vim/vim90/plugin/matchparen.vim
 16: /usr/share/vim/vim90/plugin/netrwPlugin.vim
 17: /usr/share/vim/vim90/plugin/rrhelper.vim
 18: /usr/share/vim/vim90/plugin/spellfile.vim
 19: /usr/share/vim/vim90/plugin/tarPlugin.vim
 20: /usr/share/vim/vim90/plugin/tohtml.vim
 21: /usr/share/vim/vim90/plugin/vimballPlugin.vim
 22: /usr/share/vim/vim90/plugin/zipPlugin.vim

I can only make those highlight lines load after typing :source $MYVIMRC again after everything is loaded. So I suspect these lines are getting overriden somehow? But I fail to see why, I don't have any third-party plugins or scripts, it's just the .vimrc. The same .vimrc works fine for WSL, linux etc.

I'm using MacOS Ventura 13.3, using the stock vim that came with this OS:

VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Mar  4 2023 14:02:05)
macOS version - arm64
Included patches: 1-1313
Compiled by [email protected]
Normal version without GUI.  Features included (+) or not (-):
+acl               +file_in_path      +mouse_urxvt       -tag_any_white
-arabic            +find_in_path      +mouse_xterm       -tcl
+autocmd           +float             +multi_byte        +termguicolors
+autochdir         +folding           +multi_lang        +terminal
-autoservername    -footer            -mzscheme          +terminfo
-balloon_eval      +fork()            +netbeans_intg     +termresponse
-balloon_eval_term -gettext           +num64             +textobjects
-browse            -hangul_input      +packages          +textprop
++builtin_terms    +iconv             +path_extra        +timers
+byte_offset       +insert_expand     -perl              +title
+channel           +ipv6              +persistent_undo   -toolbar
+cindent           +job               +popupwin          +user_commands
-clientserver      +jumplist          +postscript        -vartabs
+clipboard         -keymap            +printer           +vertsplit
+cmdline_compl     +lambda            -profile           +vim9script
+cmdline_hist      -langmap           -python            +viminfo
+cmdline_info      +libcall           -python3           +virtualedit
+comments          +linebreak         +quickfix          +visual
+conceal           +lispindent        +reltime           +visualextra
+cryptv            +listcmds          -rightleft         +vreplace
+cscope            +localmap          -ruby              +wildignore
+cursorbind        -lua               +scrollbind        +wildmenu
+cursorshape       +menu              +signs             +windows
+dialog_con        +mksession         +smartindent       +writebackup
+diff              +modify_fname      -sodium            -X11
+digraphs          +mouse             -sound             -xfontset
-dnd               -mouseshape        +spell             -xim
-ebcdic            +mouse_dec         +startuptime       -xpm
-emacs_tags        -mouse_gpm         +statusline        -xsmp
+eval              -mouse_jsbterm     -sun_workshop      -xterm_clipboard
+ex_extra          +mouse_netterm     +syntax            -xterm_save
+extra_search      +mouse_sgr         +tag_binary        
-farsi             -mouse_sysmouse    -tag_old_static    
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -DMACOS_X_UNIX  -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1      
Linking: gcc   -L/usr/local/lib -o vim        -lm -lncurses  -liconv -framework Cocoa

Is it because of the vim versions? I use vim-gtk for both WSL & linux. I just can't pinpoint what's causing those highlight lines to be overriden.

EDIT: "fixed" it by adding set background=dark in my .vimrc

5
  • Check the colors/lists/defaults file or :verbose highlight Comment
    – D. Ben Knoble
    Sep 5, 2023 at 15:43
  • @D.BenKnoble It shows: :verbose hi Comment Comment xxx term=bold ctermfg=14 guifg=#80a0ff Last set from /usr/share/vim/vim90/syntax/syncolor.vim line 34 The strange thing is, THIS problem is completely gone when I run vim inside tmux. How does that work?
    – ray
    Sep 6, 2023 at 6:56
  • @D.BenKnoble Nevermind, I just now fixed my problem by adding set background=dark in my .vimrc. I don't know why this works
    – ray
    Sep 6, 2023 at 7:00
  • 2
    That doesn't sound like a fix.
    – romainl
    Sep 6, 2023 at 7:11
  • @romainl, it isn't, but it works for now. Also I found your comment on a six-year-old reddit post mentioning this exact problem. I tried using syntax enable, my $TERM value is xterm-256color and all that but still to no avail.
    – ray
    Sep 6, 2023 at 7:23

1 Answer 1

1

The proper way to define your own colorscheme would be to actually define your colorscheme, for real, in its own file:

" in ~/.vim/colors/mycolors.vim
set background=dark

hi clear
let g:colors_name = 'mycolors'

hi Comment ctermfg=darkgreen
hi Type ctermfg=darkblue
hi StorageClass ctermfg=darkblue
hi Statement ctermfg=magenta
hi Constant ctermfg=red
hi Function ctermfg=darkyellow
hi LineNr ctermfg=darkgray
hi CursorLine cterm=NONE
hi CursorLineNr cterm=NONE

and then call it in your vimrc:

colorscheme mycolors

If you prefer to do it in your vimrc, then the next proper way to override the default colors is to use the :help ColorScheme autocommand:

function! MyColors()
    hi Comment ctermfg=darkgreen
    hi Type ctermfg=darkblue
    hi StorageClass ctermfg=darkblue
    hi Statement ctermfg=magenta
    hi Constant ctermfg=red
    hi Function ctermfg=darkyellow
    hi LineNr ctermfg=darkgray
    hi CursorLine cterm=NONE
    hi CursorLineNr cterm=NONE
endfunction
augroup mycolors
    autocmd!
    autocmd ColorScheme * call MyColors()
augroup END
color default

As for why what you are doing doesn't work reliably across systems…

  • Vim is generally a pretty stable piece of software but it still evolves over time. When comparing the behaviors of two Vims, make sure that a) you compare equivalent versions and b) that you take the environment into account.
  • As you can see from your :scriptnames dump, the various built-in scripts in charge of syntax highlighting, synload.vim and syncolor.vim, are sourced after your vimrc. syncolor.vim, in particular, is where the default "syntax-related" highlight groups get their visual attributes set, overriding the commands from your vimrc.
  • Note also that syncolor.vim only deals with syntax-related highlight groups so it shouldn't affect your LineNr, CursorLine, or CursorLineNr.
  • In other words, simply dropping hi commands in your vimrc is not a good way to override the defaults. More efforts are needed.

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.