Search code examples
vimmarkdownfolding

How can I change the folding styles for Vim's built-in highlighing for Markdown?


The default styling for folded headers in Vim's built-in Markdown highlighting is a whitish background, which is very difficult to visually scan.

How can I change the background color of lines that are folded Markdown headers?

Thanks!


Solution

  • You can change folded highlight style by setting the Folded highlight group. In order to change background colour of a highlight group you need to change ctermbg (for Terminal Vim) or guibg (for Gvim).

    For example, to change folded background to white:

    :highlight Folded ctermbg=15
    

    You can use :hi instead of the full command name :highlight.

    You need to specify colour number for ctermbg and list of acceptable values can be found in :h highlight-ctermbg.

    For changing folded backgound colour in GUI Vim (GVim), you need to use guibg:

    :hi Folded guibg=white
    

    You can check list of acceptable values for guibg here: :h gui-colors

    Make it persistent

    Using :highlight (:hi in abbrev) command in the current session changes highlight settings for the current session only.

    It would be recommended to use autocommand to make it persistent.

    augroup MyGroup
      autocmd ColorScheme * hi Folded ctermbg=15 guibg=white
    augroup END
    
    Additional information

    You can put hi Folded ctermbg=15 guibg=white in your .vimrc to make background colour of folded lines white. However, with this, the :hi command is invoked when you open Vim or .vimrc is reloaded by :source command. So your highlight settings will be reset if you change the colorscheme in the session. With the autocmd why above, the autocmd is invoked if the ColorScheme event is fired (= colorscheme is changed).