Vim display in statusline after delay
Asked Answered
A

1

9

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?

Aryn answered 2/6, 2011 at 9:28 Comment(7)
Can you provide a little more information? Like which problem it is that you are trying to solve?Sailer
in my vimrc there is a shortcut which implements a functionality. i want to have the script report back that the functionality has been successfully implemented. but i dont want the report of the functionality having been successful, to stay on the statusline indefinitely.Aryn
@Hermann: Then when do you wish the statusline to revert to its default settings? I.e, which event? When you write a buffer, for instance?Sailer
i want it to revert to its default settings after say, 3 seconds. So its not really an event.Aryn
Bad luck. :timer is yet to be implemented (but is on the todo list). Would you like to pick an event?Sailer
what do you mean, pick an event?Aryn
Is there any particular reason you want to report the success of your operation in the status line, rather than, say, using an :echo command?Jevons
S
33

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

Scrivner answered 2/6, 2011 at 19:37 Comment(1)
you get a +1 just for re-enacting Star Wars in your vim statusline! But then of all the lines in the series, you picked Revenge of the Sith????Acidosis

© 2022 - 2024 — McMap. All rights reserved.