1

I've been trying to set a custom statusline for help files. I tried:

set statusline=%!StatusLine()
function! StatusLine()
    if &ft == 'help'
        let b:status = "%#error#[HELP]%*"
    else
        let b:status = "my usual statusline"
    endif
    return b:status
endfunction

but when a help window is open, both statuslines become [HELP] when that window is in focus, and my usual statusline when the main file is open. I tried using setlocal statusline=%!StatusLine(), but then nothing gets set for the help file and the main file statusline still becomes [HELP].

What am I missing here?

2

From :h status-line:

This option can be local to the window, so that you can have a different status line in each window.

As ljden pointed out, you must use a local status line otherwise it is set for all buffers:

set statusline=%!StatusLine()
function! StatusLine()
    if &ft == 'help'
        let l:status = "%#error#[HELP]%*"
    else
        let l:status = "my usual statusline"
    endif
    let &l:statusline = l:status
endfunction

However it might be easier to just add this to ~/.vim/ftplugin/help.vim:

setlocal statusline=%#error#[HELP]%*
  • Only just got comment privilege otherwise would jave said before, I tried your first suggestion and it doesnt quite work as expected. When I add it to my vimrc and opened vim and then a help file, the statusline changed from my usual statusline to 0. Not sure why as I'm a little unfamiliar with this syntax and vimrc in general (still newish to it) but yeah didnt seem to work – ljden Sep 23 '17 at 1:35
  • The first method is kind of hacky, it should return a value but does not, hence the 0. This works on my machine though. The ftplugin method overrides any previously set statusline the same way an augroup + autocmd does. Finally another trick would be to return an expression to be evaluated in the context of each statusline: set statusline=%{MyStatusLine()}. There is interesting examples at the end of :h stl. – LEI Sep 23 '17 at 5:25
3

EDIT: SO I tested it out a little more this morning and had a bit of a dig. Your main issue was trying to do it all in the one function, I don't think this will work sorry. It also looks like you may have an issue with the filetype detection with the new buffer load. It appears if &ft == 'help' can be a little problematic, and it's probably better to use an autocmd, which should be wrapped in an augroup - from :help augroup-delete (can test out your new status line while you have a read!)

This prevents having the autocommands defined twice (e.g., after sourcing the .vimrc file again).

I've re-written the code so it follows OP's example more closely

" Default statusline
set statusline=%!StatusLine()
function! StatusLine()
    let b:status =  "my usual statusline"
    return b:status    
endfunction
" Override default statusline if help file
aug HelpStatusLine
    au!
    au FileType help setlocal statusline=%!LocalStatusLine()
aug END
function! LocalStatusLine()
    let b:status = "%#error#[HELP]%*"
    return b:status
endfunction

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.