When I write html in MacVim I keep the same file open in a web browser so that I can examine the html output. One of the things I dislike about this method is that I constantly have to leave vim, go to the browser, and reload the html to see the updates. Is there a more elegant solution with vim that will dynamically update html ouput as it is typed in vim? I seem to recall that Textmate could do this? Thanks.
UPDATE: I found the video I was trying to remember. You can find it here:
http://files.fletcherpenney.net/TextMate-Preview.mov
Add this to your .vimrc:
" Refresh WebKit
nnoremap \r :Refresh<CR>
command! Refresh call RefreshWebKit()
function! RefreshWebKit()
silent !osascript -e 'tell application "WebKit"'
\ -e ' set p_url to URL of current tab of front window'
\ -e ' set URL of current tab of front window to p_url'
\ -e 'end tell'
endfunction
This will create a map, a command and a function. I could have joined them, but this way it it's more clear and I can reuse the function in other places.
The function calls a little AppleScript to set the URL of frontmost tab again, thus refreshing it. This will work under Safari and WebKit, but I can't guarantee that it works with other browsers. If not, google "refresh {browser} applescript" and use a different script.
The map just calls the command, which calls the function. So you can write your
file and use it to refresh the browser without leaving Vim. Equally, use
:Refresh
to do the same.
Note that you may want to change some things:
\r
, use whatever you feel comfortable with.A quick note: the extra -e
options passed to the program are just for the sake
of readability. Do it in the fashion you want.
Take a look also in auto-commands (check :h autocmd.txt). This will let you do it in a more automatized way like:
:autocmd BufWrite *.html Refresh
This will call :Refresh
every time you write a buffer for .html files. There
are also events for inserted text and so on.
I've seen a bunch of ways to do that:
or a simple meta tag:
<meta http-equiv="refresh" content="15" />
I like lo-tech solutions to hi-tech problems so I actually use the meta tag but sidyll's solution seems excellent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With