Vim: Make chrome refresh anytime I :write Vim: Make chrome refresh anytime I :write google-chrome google-chrome

Vim: Make chrome refresh anytime I :write


I don't know how to reload a give chrome page from shell, however, I agree with Chiel92 that if you need to see your changes when file changes, you can do that from browser.

See LiveReload, works with Windows & Mac (not for me then) and supports Safari & Chrome.

LiveReload will check your main page as well all css and javscript that it depends from, if any of those changes, it will reload it.

They seems to have launched a second version, however official site of version 2 it's offline and doesn't seem version 2 it's on github either. (Version 1 it's it's on github)


a solution that might work for you is in your html coding include the line in the head tag

    <meta http-equiv="refresh" content="30" />

this will reload the page every 30 seconds directly taking from w3schools.comnow when you want to deploy it just remove that line


If you are working on mac, then a bit of apple script does the job.

putting this function in your vim configuration will let you automate the process of switching between windows and refreshing you described.

function! ReloadBrowser()    silent exe "!osascript -e 'tell app \"Firefox\" to activate\<cr>            \tell app \"System events\"\<cr> keystroke \"r\" using command down\<cr>            \end tell'"    silent exe "!osascript -e 'tell app \"Iterm2\" to activate'"endfunction

Calling that function will change system focus to Firefox, hit CMD-R to refresh the page, then change focus back to Iterm2.

Change 'Firefox' and 'Iterm2' to fit your workflow.

You can type the function into the vim command prompt like :call ReloadBrowser() or trigger the call with a mapping like this:

nnoremap <leader>rl call ReloadBrowser()<cr>

To trigger the call any time you write the file you could use an autocommand.

augroup AutoReload    au!    autocmd BufWritePost *.filetype call ReloadBrowser()augroup END

That could get a bit annoying though so if you really want that behavior I think it would be best to make it toggle-able like this:

let s:auto_reload = v:falsefunction! ToggleAutoReload()    if s:auto_reload        augroup AutoReload            au!            autocmd BufWritePost *.filetype call ReloadBrowser()        augroup END    else        augroup AutoReload            au!        augroup END    endif    let s:auto_reload = !s:auto_reloadendfunction

with that in place you can either manually trigger the reload with <leader>rl or use :call ToggleAutoReload() to enable automatic reloading when you save the file, and a second :call ToggleAutoReload() will disable it when you're done.