Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear output of function call in VIM?

Tags:

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.

like image 711
RanRag Avatar asked Feb 20 '23 08:02

RanRag


1 Answers

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

like image 132
Ves Avatar answered Mar 03 '23 04:03

Ves