Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 10 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.