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.
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:
You can use
:redir for that.
Dump highlight definitions:
:redir > file :hi :redir END
Dump syntax definitions:
:redir > file :syntax :redir END
- you will probably need to press
<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.
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
:echo GetHiCommand('LineNr') hi LineNr term=underline ctermfg=243 guifg=#7c6f64 :echo GetHiCommand('MoreMsg') hi! link MoreMsg GruvboxYellowBold