Technology
 

Preview current HTML in browser on Mac OS X

From Vim Tips Wiki

(Redirected from VimTip684)

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 684 Previous Next created March 24, 2004 · complexity basic · author Robert Roberts · version 6.0


There are a few tips on previewing current HTML documents in a Windows browser, but none I could find for Mac OS X. By studying the others, though, I stumbled on a mapping that works. The <CR> at the end anticipates the "Hit ENTER or type command to continue" message.

:map <Leader>p :!open -a Safari %<CR><CR>

[edit] Comments

To expand on this:

general -- "open x" will open "x" with the default application ... "open -a applicationName x" will open "x" with application "applicationName"

general -- "<D-aKey>" creates a shortcut using the Command (Apple) key

improved(?) -- "noremap <silent> <D-p>f :exe ':silent !open -a /Applications/Path/To/Firefox.app %'<CR>"

(prevents remapping, runs quietly, shortcut is now [Command+p,f] for "preview, firefox" ... helpful if you preview in multiple browsers)

improved(?) -- "noremap <silent> <D-p>p :exe ':silent !open %'<CR>"

(same, but creates a generic "preview" using default applications ... helpful if you only preview in that browser, or can use it on other file types)

other (open directory in Finder) -- "noremap <silent> <D-p>d :exe ':silent !open -a finder %:p:h'<CR>"

(%:p:h completely expands the file path and removes the file name so only directory is left)

other (apply same to netrw browser, ie, :Ex command) -- add to .vimrc/.gvimrc: "let g:netrw_browsex_viewer = 'open'" (this is not necessary starting with 7.2, netrw now uses "open" as the default for "x").

(when using netrw browser, hit "x" while cursor is on file name to open that file with the default application ... if the cursor is over a directory, it will open in Finder)