Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From MacVim is there a command to open the currently edited HTML file in a web browser?

share|improve this question
up vote 13 down vote accepted

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.)

share|improve this answer
    
Perfect, thanks! – Florent2 May 12 '11 at 14:59
    
Just be sure that your file names don't have spaces. Otherwise, vim tries to open every space-delimited word as its own page. It's pretty annoying, in my opinion. – dylnmc Oct 19 '15 at 13:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .