Refresh active Chrome tab on bufWrite

I’ve been working with Tailwind CSS in Laravel Blade templates the last few days and it didn’t take long to tire of flipping between Vim and the browser to refresh the page I’m editing.

My first bright idea was throwing the page into an infinite refresh loop with


    <meta http-equiv="refresh" content="5; URL=/">

That got annoying quick so I turned to the web and found this post http://blog.zamith.pt/blog/2014/06/02/autoreload-with-vim. Thank you for sharing Luís.

I’m new to Vim and didn’t follow the FTPlugin approach Luís shared. I use an autocmd instead

augroup RefreshActiveChromeTab
    autocmd!
    autocmd BufWritePost *.blade.php :silent exec "!./reload-active-chrome-tab"
augroup END

The reload-active-chrome-tab script

#!/bin/bash
# All credit to http://blog.zamith.pt/blog/2014/06/02/autoreload-with-vim/

osascript -e 'tell application "Google Chrome" to tell the active tab of its first window to reload'

Save that to the directory you normally start Vim from or use the absolute path to it in the autocmd.

Leave a comment