How to Show Time and Battery Status on Vim 8 Statusline

Hi  People!

As a Vim lover and user, I decided to clean my vimrc a bit this week creating my own statusline and dropping the excellent vim-airline. As I frequently commute by train and I normally use Vim in fullscreen mode, I decided to tweak my statusline to show both the clock and the battery level.

I manily got inspired by Build your own Vim statusline by George Ornbo, but I added a new feature by Vim 8: timer_start(), which allows to call a function after a specified time interval.

Here’s the code to throw into your .vimrc:

" Mike's statusline
let g:battery_level = ''
function! SetBatteryLevel(timer_id)
let l:battery_level = system('acpi | grep -oP "(\d+)%" | tr -d "\n"')
if (battery_level != '')
let g:battery_level = l:battery_level
call timer_start(30000, 'SetBatteryLevel')
function! GitBranch()
return system("git rev-parse –abbrev-ref HEAD 2>/dev/null | tr -d '\n'")
function! StatuslineGit()
let l:branchname = GitBranch()
return strlen(l:branchname) > 0?' '.l:branchname.' ':''
set laststatus=2
set statusline=
set statusline+=%#PmenuSel#
set statusline+=%{StatuslineGit()}
set statusline+=%#LineNr#
set statusline+=\ %f
set statusline+=%m
set statusline+=%=
set statusline+=%{g:battery_level}\
set statusline+=%#CursorColumn#
set statusline+=\ %{strftime('%H:%M')}
set statusline+=\
call SetBatteryLevel(0)

view raw
hosted with ❤ by GitHub

And here’s the result:

Screenshot from 2018-02-16 16-13-28.png

Happy Hacking!

Image result for rss Subscribe to RSS Feed here

Image result for twitter Follow me on Twitter

Image result for github icon Check the code on GitHub