highsciguy
highsciguy

Reputation: 2647

change vim status line while executing command

I would like to change the vim status line which is shown while vim is busy executing an external command. I have a vim script which pipes lines of text from vim into some external program using "!". After execution vim waits for the output of the command to replace the lines with it. While it is waiting I would like to show the status of the external command in the statusline. Is this possible?

Upvotes: 2

Views: 1906

Answers (1)

Prince Goulash
Prince Goulash

Reputation: 15735

I would solve this using a temporary global variable. Here is a simple function that returns the value of a variable g:temp_var if it exists, otherwise it returns an empty string:

function! TemporaryStatus()
    if exists("g:temp_var")
        return g:temp_var
    else
        return ""
    endif
endfunction

You can set the statusline to use the return value of this function like this:

set statusline=%!TemporaryStatus()

Then, when you call the slow external command, just set the temporary variable beforehand (using the message you want to be displayed), and unlet it afterwards. For example:

...
let g:temp_var = "I am calling a slow external command"
redraw!
call SlowExternalCommand()
unlet g:temp_var
redraw!
...

Note that the redraw! commands are necessary to make sure Vim updates the display (and therefore uses the updated value of g:temp_var in the statusline).

EDIT

Of course it would be much simpler to display the message by echoing it and clearing the display afterwards, like this:

...
redraw!
echo "I am calling a slow external command"
call SlowExternalCommand()
redraw!
...

This way the message will be displayed on the Vim command line, rather than in the statusline. You don't need any of the functions defined above.

Upvotes: 1

Related Questions