From MacVim is there a command to open the currently edited HTML file in a web browser?
Asked
Active
Viewed 4,981 times
2 Answers
16
To do it just once, you can
:!open %
which will call the shell command open with the path to the current file as argument. I don't use Mac myself, but open seems appropriate to me. If it isn't, replace with whatever program you wish the file be opened with.
Of course you can bind a key, if you'll need it frequently:
:map <silent> <F5> :!open %<CR>
And you may want to
:set nowarn
to suppress warnings about unsaved file changes.
See:
:help :!:help cmdline-special:help 'warn'
Note that you can get arbitrarily sophisticated with Vim scripting. For example, this function lets you view the current unsaved changes by use of an intermediate file:
function! BrowserPreview()
if &modified
let tmpfile = tempname()
execute "silent write " . tmpfile
call system("firefox " . shellescape(tmpfile))
if delete(tmpfile) != 0
echoerr "could not remove " . tmpfile
endif
else
call system("firefox " . shellescape(expand("%:p")))
endif
endfunction
map <silent> <F5> :call BrowserPreview()<CR>
(Replace both occurrences of firefox with open if that worked earlier.)
peth
- 10,400