Skip to content

Instantly share code, notes, and snippets.

@romainl
Last active March 8, 2025 21:23

Revisions

  1. romainl revised this gist Jun 26, 2021. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ Your first reflex is probably to put those lines somewhere in your `vimrc`:
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f

    But it quickly appears that those lines have no effect if you source a colorscheme later in your `vimrc` so you move them below:
    But it quickly appears that those lines have no effect if you source a colorscheme later in your `vimrc` so you move them below that `colorscheme` command:

    colorscheme foobar

    @@ -26,11 +26,11 @@ But it quickly appears that those lines have no effect if you source a colorsche

    which gives the desired outcome. Everything is fine. In principle.

    But you like to try new colorschemes, or you prefer to have different colorschemes for different filetypes or time of the day and your overrides don't carry over when you change your colorscheme.
    But you like to try new colorschemes, or you prefer to have different colorschemes for different filetypes or time of the day, and your overrides don't carry over when you change your colorscheme.

    This is because colorschemes usually reset all highlighting, including your own, when they are sourced.

    The solution is to override the desired highlights in an autocommand that's executed whenever a colorscheme is sourced:
    The solution is to override the desired highlights in an autocommand that's executed whenever any colorscheme is sourced:

    augroup MyColors
    autocmd!
    @@ -42,7 +42,7 @@ The solution is to override the desired highlights in an autocommand that's exec

    colorscheme foobar

    Note that the autocmd must be added *before* any colorscheme is sourced or it will have no effect.
    Note that the autocommand will have no effect on previously sourced colorschemes so it must be added *before* any colorscheme is sourced.

    Of course, we can go one step further and put all our highlights in a neat little function for maximum readability/managability/maintainability:

    @@ -78,6 +78,12 @@ This won't work if you have an autocommand like the following in your `vimrc` be
    autocmd BufWritePost $MYVIMRC source $MYVIMRC
    [...]

    By default, autocommands don't trigger other autocommands. In this classic example, an autocommand is set to `:source $MYVIMRC` automatically when you write it. The problem, here, is that you are very likely to have a line like this one somewhere in your `vimrc`:

    colorscheme whatever

    which, when it is executed as part of the re-sourcing of your vimrc, won't trigger the `ColorScheme` autocommand because of that "no nesting" rule.

    The solution is to add the `nested` keyword to that autocommand to allow other autocommands to be triggered by that event:

    [...]
  2. romainl revised this gist May 21, 2021. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,7 @@ Your first reflex is probably to put those lines somewhere in your `vimrc`:
    But it quickly appears that those lines have no effect if you source a colorscheme later in your `vimrc` so you move them below:

    colorscheme foobar

    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    @@ -38,6 +39,7 @@ The solution is to override the desired highlights in an autocommand that's exec
    \ | highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ | highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    augroup END

    colorscheme foobar

    Note that the autocmd must be added *before* any colorscheme is sourced or it will have no effect.
    @@ -55,6 +57,7 @@ Of course, we can go one step further and put all our highlights in a neat littl
    autocmd!
    autocmd ColorScheme * call MyHighlights()
    augroup END

    colorscheme foobar

    ## But I only want that override for a single colorscheme
    @@ -63,6 +66,10 @@ The autocommands above use the glob `*` in order to override *any* colorscheme.

    autocommand ColorScheme foobar call MyHighlight()

    or, if you want to target several colorschemes:

    autocommand ColorScheme foobar,barbaz call MyHighlight()

    ## But this doesn't work when I "auto-source" my `vimrc`

    This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:
    @@ -77,6 +84,10 @@ The solution is to add the `nested` keyword to that autocommand to allow other a
    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
    [...]

    ## Reference

    For all things concerning autocommands, see the aptly named `:help autocommands`. For all things concerning syntax highlighting, see `:help syntax`.

    ---

    [My Vim-related gists](https://gist.github.com/romainl/4b9f139d2a8694612b924322de1025ce).
  3. romainl revised this gist May 21, 2021. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # The right way to override any highlighting if you don't want to edit the colorscheme file directly

    ## Generalities first

    Suppose you have weird taste and you *absolutely* want:

    * your visual selection to always have a green background and black foreground,
    @@ -55,11 +57,15 @@ Of course, we can go one step further and put all our highlights in a neat littl
    augroup END
    colorscheme foobar

    NOTE: The autocommands above use the glob `*` in order to override *any* colorscheme. If, say… you really like the colorscheme `foobar` but you can't stand its `Visual` highlight, you will want your autocommand to target that colorscheme specifically. To this end, you can use the name of that colorscheme instead of `*`:
    ## But I only want that override for a single colorscheme

    The autocommands above use the glob `*` in order to override *any* colorscheme. If, say… you really like the colorscheme `foobar` but you can't stand its `Visual` highlight, you will want your autocommand to target that colorscheme specifically. To this end, you can use the name of that colorscheme instead of `*`:

    autocommand ColorScheme foobar call MyHighlight()

    NOTE: This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:
    ## But this doesn't work when I "auto-source" my `vimrc`

    This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:

    [...]
    autocmd BufWritePost $MYVIMRC source $MYVIMRC
  4. romainl revised this gist Apr 12, 2020. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # The right way to override any highlighting if you don't want to edit the colorscheme file directly

    Suppose you have weird taste and you *absolutely* want:

    * your visual selection to always have a green background and black foreground,
    @@ -68,3 +70,7 @@ The solution is to add the `nested` keyword to that autocommand to allow other a
    [...]
    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
    [...]

    ---

    [My Vim-related gists](https://gist.github.com/romainl/4b9f139d2a8694612b924322de1025ce).
  5. romainl revised this gist Mar 16, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -53,6 +53,10 @@ Of course, we can go one step further and put all our highlights in a neat littl
    augroup END
    colorscheme foobar

    NOTE: The autocommands above use the glob `*` in order to override *any* colorscheme. If, say… you really like the colorscheme `foobar` but you can't stand its `Visual` highlight, you will want your autocommand to target that colorscheme specifically. To this end, you can use the name of that colorscheme instead of `*`:

    autocommand ColorScheme foobar call MyHighlight()

    NOTE: This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:

    [...]
  6. romainl revised this gist Jan 14, 2018. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -27,18 +27,19 @@ This is because colorschemes usually reset all highlighting, including your own,

    The solution is to override the desired highlights in an autocommand that's executed whenever a colorscheme is sourced:

    colorscheme foobar
    augroup MyColors
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    \ | highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ | highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ | highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    augroup END
    colorscheme foobar

    And of course, we can go one step further and put all our highlights in a neat little function for maximum readability/managability/maintainability:
    Note that the autocmd must be added *before* any colorscheme is sourced or it will have no effect.

    Of course, we can go one step further and put all our highlights in a neat little function for maximum readability/managability/maintainability:

    colorscheme foobar
    function! MyHighlights() abort
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    @@ -50,6 +51,7 @@ And of course, we can go one step further and put all our highlights in a neat l
    autocmd!
    autocmd ColorScheme * call MyHighlights()
    augroup END
    colorscheme foobar

    NOTE: This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:

  7. romainl revised this gist Jan 9, 2018. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,7 @@ This is because colorschemes usually reset all highlighting, including your own,

    The solution is to override the desired highlights in an autocommand that's executed whenever a colorscheme is sourced:

    colorscheme foobar
    augroup MyColors
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    @@ -37,6 +38,7 @@ The solution is to override the desired highlights in an autocommand that's exec

    And of course, we can go one step further and put all our highlights in a neat little function for maximum readability/managability/maintainability:

    colorscheme foobar
    function! MyHighlights() abort
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    @@ -51,8 +53,12 @@ And of course, we can go one step further and put all our highlights in a neat l

    NOTE: This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:

    [...]
    autocmd BufWritePost $MYVIMRC source $MYVIMRC
    [...]

    The solution is to add the `nested` keyword to that autocommand to allow other autocommands to be triggered by that event:

    [...]
    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
    [...]
  8. romainl revised this gist Nov 17, 2017. No changes.
  9. romainl revised this gist Nov 5, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -30,9 +30,9 @@ The solution is to override the desired highlights in an autocommand that's exec
    augroup MyColors
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ | highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ | highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ | highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    augroup END

    And of course, we can go one step further and put all our highlights in a neat little function for maximum readability/managability/maintainability:
  10. romainl revised this gist Nov 5, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -30,9 +30,9 @@ The solution is to override the desired highlights in an autocommand that's exec
    augroup MyColors
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    augroup END

    And of course, we can go one step further and put all our highlights in a neat little function for maximum readability/managability/maintainability:
  11. romainl revised this gist Nov 5, 2017. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -28,25 +28,25 @@ This is because colorschemes usually reset all highlighting, including your own,
    The solution is to override the desired highlights in an autocommand that's executed whenever a colorscheme is sourced:

    augroup MyColors
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    augroup END

    And of course, we can go one step further and put all our highlights in a neat little function for maximum readability/managability/maintainability:

    function! MyHighlights() abort
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    endfunction

    augroup MyColors
    autocmd!
    autocmd ColorScheme * call MyHighlights()
    autocmd!
    autocmd ColorScheme * call MyHighlights()
    augroup END

    NOTE: This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:
  12. romainl revised this gist Nov 5, 2017. 2 changed files with 58 additions and 67 deletions.
    58 changes: 58 additions & 0 deletions colorscheme-override.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    Suppose you have weird taste and you *absolutely* want:

    * your visual selection to always have a green background and black foreground,
    * your active statusline to always have a white background and red foreground,
    * your very own deep blue background.

    Your first reflex is probably to put those lines somewhere in your `vimrc`:

    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f

    But it quickly appears that those lines have no effect if you source a colorscheme later in your `vimrc` so you move them below:

    colorscheme foobar
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f

    which gives the desired outcome. Everything is fine. In principle.

    But you like to try new colorschemes, or you prefer to have different colorschemes for different filetypes or time of the day and your overrides don't carry over when you change your colorscheme.

    This is because colorschemes usually reset all highlighting, including your own, when they are sourced.

    The solution is to override the desired highlights in an autocommand that's executed whenever a colorscheme is sourced:

    augroup MyColors
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    augroup END

    And of course, we can go one step further and put all our highlights in a neat little function for maximum readability/managability/maintainability:

    function! MyHighlights() abort
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    endfunction

    augroup MyColors
    autocmd!
    autocmd ColorScheme * call MyHighlights()
    augroup END

    NOTE: This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:

    autocmd BufWritePost $MYVIMRC source $MYVIMRC

    The solution is to add the `nested` keyword to that autocommand to allow other autocommands to be triggered by that event:

    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
    67 changes: 0 additions & 67 deletions colorscheme-override.vim
    Original file line number Diff line number Diff line change
    @@ -1,67 +0,0 @@
    " Suppose you have weird taste and you *absolutely* want:
    " * your visual selection to always have a green background and
    " black foreground,
    " * your active statusline to always have a white background and
    " red foreground,
    " * your very own deep blue background.
    "
    " Your first reflex is probably to put those lines somewhere in your 'vimrc':

    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f

    " But it quickly appears that those lines have no effect if you source
    " a colorscheme later in your 'vimrc' so you move them below:

    colorscheme foobar
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f

    " which gives the desired outcome. Everything is fine. In principle.
    "
    " But you like to try new colorschemes, or you prefer to have different
    " colorschemes for different filetypes or time of the day and your overrides
    " don't carry over when you change your colorscheme.
    "
    " This is because colorschemes usually reset all highlighting, including your
    " own, when they are sourced.
    "
    " The solution is to override the desired highlights in an autocommand that's
    " executed whenever a colorscheme is sourced:

    augroup MyColors
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    augroup END

    " And of course, we can go one step further and put all our highlights in
    " a neat little function for maximum readability/managability/maintainability:

    function! MyHighlights() abort
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    endfunction

    augroup MyColors
    autocmd!
    autocmd ColorScheme * call MyHighlights()
    augroup END

    " NOTE: This won't work if you have an autocommand like the following in your
    " 'vimrc' because autocommands don't nest by default:

    autocmd BufWritePost $MYVIMRC source $MYVIMRC

    " The solution is to add the 'nested' keyword to that autocommand to allow
    " other autocommands to be triggered by that event:

    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
  13. romainl revised this gist Aug 15, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion colorscheme-override.vim
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@ augroup END

    autocmd BufWritePost $MYVIMRC source $MYVIMRC

    " The solution is to add the `nested` keyword to that autocommand to allow
    " The solution is to add the 'nested' keyword to that autocommand to allow
    " other autocommands to be triggered by that event:

    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
  14. romainl revised this gist Aug 15, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions colorscheme-override.vim
    Original file line number Diff line number Diff line change
    @@ -57,11 +57,11 @@ augroup MyColors
    augroup END

    " NOTE: This won't work if you have an autocommand like the following in your
    `vimrc` because autocommands don't nest by default:
    " 'vimrc' because autocommands don't nest by default:

    autocmd BufWritePost $MYVIMRC source $MYVIMRC

    " The solution is to add the `nested` keyword to that autocommand to allow
    other autocommands to be triggered by that event:
    " other autocommands to be triggered by that event:

    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
  15. romainl revised this gist Aug 15, 2017. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions colorscheme-override.vim
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ augroup MyColors
    augroup END

    " And of course, we can go one step further and put all our highlights in
    " a function for maximum clarity/readability/managability/maintainability:
    " a neat little function for maximum readability/managability/maintainability:

    function! MyHighlights() abort
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    @@ -56,11 +56,12 @@ augroup MyColors
    autocmd ColorScheme * call MyHighlights()
    augroup END

    NOTE: This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:
    " NOTE: This won't work if you have an autocommand like the following in your
    `vimrc` because autocommands don't nest by default:

    autocmd BufWritePost $MYVIMRC source $MYVIMRC
    autocmd BufWritePost $MYVIMRC source $MYVIMRC

    The solution is to add the `nested` keyword to that autocommand to allow other autocommands to be triggered by that event:
    " The solution is to add the `nested` keyword to that autocommand to allow
    other autocommands to be triggered by that event:

    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
                                     ^^^^^^
    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
  16. romainl revised this gist Aug 15, 2017. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion colorscheme-override.vim
    Original file line number Diff line number Diff line change
    @@ -54,4 +54,13 @@ endfunction
    augroup MyColors
    autocmd!
    autocmd ColorScheme * call MyHighlights()
    augroup END
    augroup END

    NOTE: This won't work if you have an autocommand like the following in your `vimrc` because autocommands don't nest by default:

    autocmd BufWritePost $MYVIMRC source $MYVIMRC

    The solution is to add the `nested` keyword to that autocommand to allow other autocommands to be triggered by that event:

    autocmd BufWritePost $MYVIMRC nested source $MYVIMRC
                                     ^^^^^^
  17. romainl revised this gist Jul 1, 2017. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions colorscheme-override.vim
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,19 @@ augroup MyColors
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    augroup END

    " And of course, we can go one step further and put all our highlights in
    " a function for maximum clarity/readability/managability/maintainability:

    function! MyHighlights() abort
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    endfunction

    augroup MyColors
    autocmd!
    autocmd ColorScheme * call MyHighlights()
    augroup END
  18. romainl revised this gist Jun 5, 2017. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions colorscheme-override.vim
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    " Suppose you have weird taste and you *absolutely* want: * your visual
    " selection to always have a green background and black foreground, * your
    " active statusline to always have a white background and red foreground, * your
    " very own deep blue background.
    " Suppose you have weird taste and you *absolutely* want:
    " * your visual selection to always have a green background and
    " black foreground,
    " * your active statusline to always have a white background and
    " red foreground,
    " * your very own deep blue background.
    "
    " Your first reflex is probably to put those lines somewhere in your 'vimrc':

  19. romainl revised this gist Jun 5, 2017. 1 changed file with 32 additions and 6 deletions.
    38 changes: 32 additions & 6 deletions colorscheme-override.vim
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,40 @@
    " Suppose you want:
    " * your visual selection to always have a green background and black foreground,
    " * your active statusline to always have a white background and red foreground.
    " Suppose you have weird taste and you *absolutely* want: * your visual
    " selection to always have a green background and black foreground, * your
    " active statusline to always have a white background and red foreground, * your
    " very own deep blue background.
    "
    " What you have to do is override those highlight groups in an autocommand
    " that's executed whenever a colorscheme is sourced.
    " Your first reflex is probably to put those lines somewhere in your 'vimrc':

    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f

    " But it quickly appears that those lines have no effect if you source
    " a colorscheme later in your 'vimrc' so you move them below:

    colorscheme foobar
    highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f

    " which gives the desired outcome. Everything is fine. In principle.
    "
    " But you like to try new colorschemes, or you prefer to have different
    " colorschemes for different filetypes or time of the day and your overrides
    " don't carry over when you change your colorscheme.
    "
    " This is because colorschemes usually reset all highlighting, including your
    " own, when they are sourced.
    "
    " This can be done by adding this snippet in your 'vimrc':
    " The solution is to override the desired highlights in an autocommand that's
    " executed whenever a colorscheme is sourced:

    augroup MyColors
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    \ highlight Normal cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    \ highlight NonText cterm=NONE ctermbg=17 gui=NONE guibg=#00005f
    augroup END
  20. romainl created this gist Jun 4, 2017.
    14 changes: 14 additions & 0 deletions colorscheme-override.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    " Suppose you want:
    " * your visual selection to always have a green background and black foreground,
    " * your active statusline to always have a white background and red foreground.
    "
    " What you have to do is override those highlight groups in an autocommand
    " that's executed whenever a colorscheme is sourced.
    "
    " This can be done by adding this snippet in your 'vimrc':

    augroup MyColors
    autocmd!
    autocmd ColorScheme * highlight Visual cterm=NONE ctermbg=76 ctermfg=16 gui=NONE guibg=#5fd700 guifg=#000000
    \ highlight StatusLine cterm=NONE ctermbg=231 ctermfg=160 gui=NONE guibg=#ffffff guifg=#d70000
    augroup END