Search code examples
vimdelaystatusline

Vim display in statusline after delay


Is it possible to have the statusline in Vim be updated so and so long after a given event?

And if so, how does one do that?


Solution

  • As others have already pointed out, using timed changes to the statusline is not possible at the moment. However, if you can pick a suitable event at which the status reverts to the default, then you might be in luck. For e.g., if your workflow is:

    1. Esc out to normal mode and call your shortcut
    2. See the outcome on the statusline and go back to insert mode to continue working

    then you could use the InsertEnter event in an autocommand to change the status to the default once you hit i in normal mode.

    Here's a small fun example that you can probably modify to your needs.

    The normal status line:

    enter image description here

    "statusline
    hi green term=bold cterm=bold ctermfg=107
    hi red term=bold cterm=bold ctermfg=167
    hi gray term=bold cterm=bold ctermfg=0
    hi lblue term=bold cterm=bold ctermfg=12
    
    function! Palpatine()
        let str='At last, the Jedi are no more' 
        return str
    endfunction
    
    function! Force()
        let str=',~`,~`,~`,~`,~`,~`,~`,~'
        return str
    endfunction
    
    function! DefaultStatus()
        let statusStr='%#red#Palpatine: %#gray#%{Palpatine()} %#lblue#%{Force()} %=%#green#Yoda'
        return statusStr
    endfunction
    
    set laststatus=2
    set statusline=%!DefaultStatus()
    

    Statuschange on function call:

    enter image description here

    function! Yoda()
        let str='Not if anything to say about it, I have'
        return str
    endfunction
    
    function! MyStatus()
        let statusStr='%#red#Palpatine %=%#lblue#%{Force()} %#gray#%{Yoda()} %#green#:Yoda'
        return statusStr
    endfunction
    
    function! MyFunc()
        set statusline=%!MyStatus()
    endfunction
    
    noremap <C-m> :call MyFunc()<CR>
    

    With the above definitions, every time I press Ctrlm, the statusline changes to the above.

    Now by setting an autocommand, we can revert it to the default whenever you enter insert mode.

    autocmd InsertEnter * set statusline=%!DefaultStatus()
    

    Back to insert:

    enter image description here