-1

I have cowfortune showing up at startup and I want to color it. How do I color the call append() line? I saw some things with signs but I didn't really get it to work.

enew
setlocal
    \ bufhidden=wipe
    " and some more options here...
for line in split(system('cowfortune -p'), '\n')
    " this is the line that I want to color!      
    call append('$', '        ' . l:line)
endfor
setlocal nomodifiable nomodified
0
1

One way to go with it is to use the vim syntax highlighting mechanisms to do that. The first thing to do is to add set filetype=cowfortune to your function:

function Cow()
    enew
    set filetype=cowfortune
    setlocal bufhidden=wipe
    for line in split(system('ls -l'), '\n')
        call append('$', '        ' . l:line)
    endfor
    setlocal nomodifiable nomodified
endfunction

This way the buffer you are creating with your message has a defined filetype cowfortune. Now you can create the file $HOME/.vim/after/syntax/cowfortune.vim which will contain the syntax instructions to be used in this buffer.

A very basic version of this file could be something like this:

" Guard to avoid loading the syntax file several times
if exists("b:current_syntax")
    finish
endif
let b:current_syntax = "potion"

" Match all the existing text
syntax match cowText ".*"
" And highlight it as an error
highlight link cowText Error

What it does is create a syntax group named cowText and say that everything in the file should be attributed to this group (.*), see :h :syn-match.

Then it creates a link to say that the group cowText should be shown like the group Error (see :h :hi-link)

With this code everything in your buffer will be highlighted in red (or however your colorscheme renders Error). Now these are some building block, you will need to change the match to apply your new group only to the message you want (I don't remember what cowfortune looks like but I think the message is displayed between | chars so maybe something like |\zs.*\ze|) and maybe you'll also want to create your own highlighting group.

To learn how to do all of that the help :h syntax is a very good place to start and learnvimscriptthehard way has some very good material too.

1
  • 1
    That worked nicely, thanks! I am very new to actually doing my own setup with vim, so all the :h suggestions were very useful. I set up different regions for the filetype with syn-region in the end to have different colored banners on Start. – carrasco Apr 13 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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