When I analyze log files in vim I often create ad-hoc syntax highlighting rules for better orientation. For example

:syntax match myerror match /NullPointerException/
:highlight myerror ctermbg=red

So far so good. But sometimes I create quite complex rules during a long editing session and I want to save all created ad-hoc rules so I can reuse them. I can print them:

myerror        xxx match /NullPointerException/
myerror        xxx ctermbg=4

But this output contains not only ad-hoc rules and it cannot be simply copied to .vimrc or to a new syntax file because of different format. The :makevimrc does not save syntax highlighting rules at all.

Any idea how to export ad-hoc coloring rules?

  • 1
    To my knowledge Vim does not provide a mechanism for this. I'd copy them from my :-command history by typing q: (q, colon) in normal mode and searching for them. – Heptite Apr 13 '17 at 20:06
  • @Heptite: Good hint! I realized that it is possible to edit the command history so I can do q: and then delete all non syntax commands :g!/^sy/d and copy all of them. And the to the same for hi. Thanks. – Zaboj Campula Apr 14 '17 at 11:54
  • Since you were able to come up with a full solution, you can answer your own question with full instructions and then mark it as accepted. – Heptite Apr 15 '17 at 22:00
  • I do not think it is a full solution. I would give +1 to answer like this but would not accept because I still hope there is more convenient way. If nobody comes with a better solution the I will perhaps write a function based on comments above, past it here and it could be an acceptable answer from my point of view. – Zaboj Campula Apr 16 '17 at 19:53

To my knowledge there is no native Vim mechanism to do this. I think it's assumed that if you are going to develop a syntax highlight rule set you will do it in a Vim script from the start.

I threw together a simple Vim script that will append all syntax and highlight commands from the :-command history into a named file:

function! GetSyntaxHistory()
    let l:lines=[]
    for l:i in range(1,histnr(':'))
        let l:line = histget(':', l:i)
        if l:line =~ '^\s*\(syn\|syntax\|hi\|highlight\)\s\+'
            let l:lines += [l:line]
    return l:lines

function! WriteSyntax(file)
    let l:lines = GetSyntaxHistory()
    call writefile(l:lines, a:file, 'a')

command! -nargs=1 -complete=file ExportSyn call WriteSyntax(<f-args>)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.