[ACCEPTED]-How to modify existing highlight group in vim?-syntax-highlighting

Accepted answer
Score: 10

As "too much php" has said, there 22 is no direct way to say "create a value 21 that looks like that one and add bold". The 20 best way is to modify your colour scheme. If 19 you're not using a custom colour scheme, copy 18 one from the main vim installation directory 17 to your ~/.vim/colors directory and edit 16 it to suit you. Alternatively, search on 15 the vim scripts page and try some of the 14 many that are available.

Shameless plug: if 13 you want one that's easier to edit than 12 the standard format, try my "Bandit" colour scheme.

If you really want 11 to be able to add bold on the fly, you'll 10 need a fairly complex script, like the one 9 below. Note that this won't be saved for 8 your next session unless you call it automatically 7 after loading your colour scheme or by doing 6 something like:

:autocmd ColorScheme AddBoldToGroup my_highlight_group

The script in all it's enormity 5 is below. As far as I am aware, there is 4 no significantly quicker way of doing this! Obviously 3 you could save a few lines by writing less 2 verbose code, but the general idea of using 1 redir and silent hi repeatedly is the only way.

" Call this with something like
" :AddBoldToGroup perlRepeat
command! -complete=highlight -nargs=1 AddBoldToGroup call AddBoldToGroup(<f-args>)
function! AddBoldToGroup(group)
    " Redirect the output of the "hi" command into a variable
    " and find the highlighting
    redir => GroupDetails
    exe "silent hi " . a:group
    redir END

    " Resolve linked groups to find the root highlighting scheme
    while GroupDetails =~ "links to"
        let index = stridx(GroupDetails, "links to") + len("links to")
        let LinkedGroup =  strpart(GroupDetails, index + 1)
        redir => GroupDetails
        exe "silent hi " . LinkedGroup
        redir END

    " Extract the highlighting details (the bit after "xxx")
    let MatchGroups = matchlist(GroupDetails, '\<xxx\>\s\+\(.*\)')
    let ExistingHighlight = MatchGroups[1]

    " Check whether there's an existing gui= block
    let MatchGroups = matchlist(ExistingHighlight, '^\(.\{-}\) gui=\([^ ]\+\)\( .\{-}\)\?$')
    if MatchGroups != []
        " If there is, check whether "bold" is already in it
        let StartHighlight = MatchGroups[1]
        let GuiHighlight = MatchGroups[2]
        let EndHighlight = MatchGroups[3]
        if GuiHighlight =~ '.*bold.*'
            " Already done
        " Add "bold" to the gui block
        let GuiHighlight .= ',bold'
        let NewHighlight = StartHighlight . GuiHighlight . EndHighlight
        " If there's no GUI block, just add one with bold in it
        let NewHighlight = ExistingHighlight . " gui=bold"

    " Create the highlighting group
    exe "hi " . a:group . " " NewHighlight
Score: 2

Changing the attributes on a group which 8 is linked to another will disconnect the 7 link. AFAIK there is no easy way to copy 6 the colors from my_default_color into my_highlight_group. You will just have 5 to copy the color values by hand.

This shouldn't 4 be a big issue though, you should have all 3 your highlight groups defined in your colorscheme 2 file, so just put those two next to each 1 other:

hi my_default_color   guifg=#000088
hi my_highlight_group guifg=#000088 gui=bold
Score: 0

Assuming you only want this for one syntax 2 type, you should just make a new group name, my_bold_default_color, and 1 apply the bold attribute to that.

Score: 0

Years had passed and now the question has 6 a much more straightforward solution: hlget() and 5 hlset() functions.

It's still true that if you want 4 to modify a color scheme, copying it first 3 and modify the copied version would be a 2 better solution.

An example of adding bold 1 attribute to types' highlight:

" Get the highlight group, resolving links
let hl = hlget("Type", v:true)[0]
" Set GUI attributes
let hl.gui = hl->get("gui", {})->extend(#{ bold: v:true })
" Set the highlight group
call hlset([hl])

More Related questions