8

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?

john-jones
  • 7,490
  • 18
  • 53
  • 86
  • 1
    Can you provide a little more information? Like which problem it is that you are trying to solve? – johnsyweb Jun 02 '11 at 09:31
  • 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. – john-jones Jun 02 '11 at 09:35
  • @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? – johnsyweb Jun 02 '11 at 10:00
  • i want it to revert to its default settings after say, 3 seconds. So its not really an event. – john-jones Jun 02 '11 at 10:09
  • 2
    Bad luck. `:timer` is yet to be implemented (but is on the [todo list](http://vimdoc.sourceforge.net/htmldoc/todo.html#todo.txt)). Would you like to pick an event? – johnsyweb Jun 02 '11 at 10:34
  • what do you mean, pick an event? – john-jones Jun 02 '11 at 13:02
  • 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? – Prince Goulash Jun 02 '11 at 14:02

1 Answers1

32

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

abcd
  • 41,765
  • 7
  • 81
  • 98
  • 11
    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_???? –  Jun 03 '11 at 02:10