3

If I set syntax highlighting rules manually (not in a file), is there a way to save them out to a file? Specifically, the goal is to source this file to restore the syntax highlighting rules later on.

3
  • 1
    You should improve the question to make it more clear what you want. Based on the question as it stands now, @romainl has given a good answer. However, as is clear from the (rather childish) discussion between you guys, what you ask is if there is a way to save the highlighting commands to a file for them to be sourced later on. This is really quite different. Commented Oct 19, 2015 at 7:01
  • 1
    FWIW, I updated the question. I also added a new answer that might be more helpful. Commented Oct 19, 2015 at 7:11
  • 1
    Could you provide an example of what you want to save to a file. If it's what I think a combination of using the .viminfo and yolenor's answer and adding to .vimrc might do what you want
    – Steve
    Commented May 1, 2018 at 23:26

3 Answers 3

4

No, there is no command that will save the syntax/highlight definitions to a file that can be parsed to restore the same definitions at a later stage. Or, to be more specific, I do not think that any such command exists. I also do not know of any plugins or addons that will do this.

However, the command history is stored in ~/.viminfo. Thus, if you want to copy manually typed highlight/syntax commands into a file for later sourcing, you may copy the relevant commands from the command history. Of course, this requires some manual parsing and tweaking, but I think this is the simplest solution to your problem.

For more details, see:
- :h viminfo
- :h 'viminfo'

12

You can use :redir for that.

Dump highlight definitions:

:redir > file
:hi
:redir END

Dump syntax definitions:

:redir > file
:syntax
:redir END

Notes:

  • you will probably need to press G and <CR> to get to the end of the listing if you don't want to go through everything.
  • neither method will produce a working syntax script or a working colorscheme, you will need to edit the result anyway.

See :help :redir

9
  • Not quite; that just shows what links to what. That's not something that can be loaded back in as a syntax file. Commented Oct 19, 2015 at 3:32
  • 1
    It is exactly what you asked for: a snapshot of every current hi, linked or not. Now, there is no magical command to turn that into a proper colorscheme so you will have to do a bit of editing. And it so happens that Vim is pretty good at editing text.
    – romainl
    Commented Oct 19, 2015 at 6:06
  • That is not what I asked. Being pedantic doesn't help anything; you know what I meant. Commented Oct 19, 2015 at 6:07
  • Revenge downvoting doesn't help either. Commented Oct 19, 2015 at 6:11
  • Yes, that's exactly what you asked. I used that very command to create a flat clone of Solarized and it worked perfectly. If you don't want to dump your hi whatever commands don't ask how to dump them and ask what you want.
    – romainl
    Commented Oct 19, 2015 at 6:12
4

This may be helpful for future readers:

This function returns an executable command which can restore the actual highlight attributes for one highlight group (not fully tested, but it handles linked groups):

function! GetHiCommand(group)
    redir => l:output
        silent! exec 'hi' a:group
    redir end

    let l:link = matchstr(l:output, 'links to \zs\w\+')

    if empty(l:link)
        let l:attrs = matchstr(l:output, '\<xxx \zs[^\n]*')
        return printf('hi %s %s', a:group, l:attrs)
    else
        return printf('hi! link %s %s', a:group, l:link)
    endif
endf

Example output:

:echo GetHiCommand('LineNr')
hi LineNr term=underline ctermfg=243 guifg=#7c6f64

:echo GetHiCommand('MoreMsg')
hi! link MoreMsg GruvboxYellowBold

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.