RanRag
RanRag

Reputation: 49597

How to clear output of function call in VIM?

I know my question title is not explanatory enough so let me try to explain.

I created a vim function that displays my current battery state. My function is as follows:

function! BatteryStatus()

     let l:status = system("~/battery_status.sh")
     echo join(split(l:status))

endfunction

I have mapped the above function as nnoremap <F3> :call BatteryStatus()<cr>. Now, when I press F3 it displays my battery status as Discharging, 56%, 05:01:42 remaining which is my required output but my question is how do I make the above output disappear.

Currently what happens is after function call it continuously displays the output and I have to manually use :echo to clear the command window(:).

So, what necessary changes are to be made in my function so that I can achieve toggle like behaviour.

battery_status.sh

acpi | awk -F ": " '{print $2}'

PS: This is part of a learning exercise. So, please don't suggest alternative vim scripts.

Upvotes: 5

Views: 2426

Answers (4)

jdhao
jdhao

Reputation: 28516

You can just Ctrl+ L to clear the message on the status line.

Upvotes: 1

Eric Wong
Eric Wong

Reputation: 265

vim
function! pseudocl#render#clear()
  echon "\r\r"
  echon ''
endfunction

Upvotes: 2

Ves
Ves

Reputation: 1292

Simplistic straightforward way to toggling the output:

let s:battery_status_output_flag = "show"

function! BatteryStatus()

    if s:battery_status_output_flag == "show"
        let l:status = system("~/battery_status.sh")
        echo join(split(l:status))
        let s:battery_status_output_flag = "clear"
    else
        echo ""
        let s:battery_status_output_flag = "show"
    endif

endfunction

Note s: prefix, see :help script-variable

Upvotes: 3

kev
kev

Reputation: 161984

You can define a autocmd:

:au CursorHold * redraw!

Vim redraws itself 4 sec (set by updatetime option) after it's idle.

Upvotes: 2

Related Questions