Reputation: 122
I have a Macbook 10.7. I always maximize the terminal window so if I wanna check the battery status i have to move the mouse to the top or quit the maximum vim window. So I would like to show the battery in the vim statusbar. Here is what I did: Create a file and make it executable.
vi /bin/battery
#!/bin/bash
ioreg -l|grep -i capacity | tr '\n' ' | '|awk '{printf("%.1f%%", $10/$5 * 100)}'
Now if I type "battery" in terminal it will output something like 87.6%. My question is how am I supposed to show this thing in VIM status bar?
OK! Thanks for Romainl and Ingo's help. after reading and testing i think i get a 'perfect' solution. at least i am satified. :P
Here is the detail.
sudo vi /bin/battery
#!/bin/bash
/usr/sbin/ioreg -l|grep -i capacity|tr '\n' ' | '|awk '{printf("%d%%",$10/$5*100)}'>~/.battery
crontab -e
#check battery every 4 mins.
*/4 * * * * /bin/battery
the battery script outputs to a file. and it runs every 4 mins(controlled by crontab). the rest part is about the same as Ingo's answer.
:let g:battery = '???'
:autocmd CursorHold * let g:battery = system('cat ~/.battery')
:set statusline+=%{g:battery}
Upvotes: 2
Views: 1613
Reputation: 31070
It's probably better to set a cronjob and put the battery level in a file then read that file from vim.
Upvotes: 1
Reputation: 172658
The statusline will be updated very often (on every move!), that's too often for invoking an external script.
Have a look at :help autocommand-events
, and use :autocmd
s to update a Vim variable, then just include that variable in the statusline. Suitable events may be: CursorHold
, FocusGained
, BufRead
:let g:battery = '???'
:autocmd CursorHold * let g:battery = system('/bin/battery')
:set statusline+=%{g:battery}
(And/or define a mapping to update the variable manually.)
Upvotes: 2
Reputation: 196751
Your script doesn't output anything on my MacPro but I guess that's somewhat normal (no battery).
Anyway, this should work:
:set statusline+=%{system('/bin/battery')}
Be aware that the statusline is updated very often: /bin/battery
will probably be called a dozen times per second. It doesn't sound good for your battery!
Did you consider setting up a notification system (with Growl, for example) instead?
Or simply a custom mapping:
:nnoremap <F11> :exec('echo(sytem("date"))')<CR>
Upvotes: 1