Vim Tips Wiki
(update list of launch tips (removed "review" and redundant "dup" as will be merging many of these soon))
m (Reverted edits by 103.250.149.204 (talk | block) to last version by Fritzophrenic)
Tag: rollback
(20 intermediate revisions by 8 users not shown)
Line 5: Line 5:
 
|created=2001
 
|created=2001
 
|complexity=basic
 
|complexity=basic
|author=Jamis Buck
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=4/12
 
|rating=4/12
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
I've found while writing HTML files that it can become cumbersome when I have to switch to a web browser, load my page, and move back to Vim regularly to preview what I've written. I've come up with the following tricks.
+
This tip shows how to use Vim to view an html file, or a web page, in a web browser. If wanted, a text-based web browser can be used to read the text from an html page into a scratch buffer in Vim.
   
  +
==Viewing text from an html file or a URL==
The first one requires that you have lynx (the text-based browser) installed on your computer (available from http://lynx.isc.org/release/). If your HTML page is primarily text, with few (if any) images, you can set up the following function and mapping:
 
  +
A text-based web browser such as [[wikipedia:Elinks|<code>elinks</code>]] can extract a formatted view of the text from an html file or a web page. The following shows how to use elinks to read that text into a scratch buffer. You might do that for a quick preview, or to copy text from the displayed html page.
 
<pre>
 
<pre>
function PreviewHTML_TextOnly()
+
function! ViewHtmlText(url)
 
if !empty(a:url)
let l:fname = expand("%:p" )
 
new
+
new
set buftype=nofile nonumber
+
setlocal buftype=nofile bufhidden=hide noswapfile
exe "%!lynx " . l:fname . " -dump -nolist -underscore -width " . winwidth(0)
+
execute 'r !elinks ' . a:url . ' -dump -dump-width ' . winwidth(0)
  +
1d
 
endif
 
endfunction
 
endfunction
  +
" Save and view text for current html file.
map <Leader>pt :call PreviewHTML_TextOnly()<CR>
 
  +
nnoremap <Leader>H :update<Bar>call ViewHtmlText(expand('%:p'))<CR>
 
" View text for visually selected url.
 
vnoremap <Leader>h y:call ViewHtmlText(@@)<CR>
 
" View text for URL from clipboard.
  +
" On Linux, use @* for current selection or @+ for text in clipboard.
 
nnoremap <Leader>h :call ViewHtmlText(@+)<CR>
 
</pre>
 
</pre>
   
  +
After sourcing the above, and assuming the default backslash Leader key, you can:
This will open a new window and display your formatted HTML document in that window. Note that bold-face, italics, links, etc. will be lost -- all you will see is the text -- but the "-underscore" parameter to Lynx causes any text that would have been bold, italicized, or underlined to be displayed like _this_.
 
  +
*Edit an html file, then type <code>\H</code> to save and preview the file.
  +
*Visually select the full path of a local html file or a URL, then type <code>\h</code> to preview the file or web page.
  +
*Copy the full path of a local html file or a URL in another application, then type <code>\h</code> to preview the file or web page in Vim.
  +
 
==From 256 [[Opening current Vim file in your Windows browser]]==
  +
*''TODO'': Finish cleaning following notes.
  +
On Windows, using <code><cWORD></code> to open the link at the cursor in a browser does not work when the URL is in an html tag surrounded by quotes. However, using <code><cfile></code> works for both situations: plain URL and URL between html tags.
   
The other trick requires that Vim be running on your current machine, and that you be running a GUI of some sort (X-Windows, Windows, etc.). You can cause Vim to invoke your favorite browser and have it display the file, like this:
 
 
<pre>
 
<pre>
  +
" On Windows, open URL under cursor.
function PreviewHTML_External()
 
  +
nnoremap <F8> :!start C:\progra~1\intern~1\iexplore.exe <cfile><CR>
exe "silent !mozilla -remote \"openurl(file://"; . expand( "%:p" ) . ")\""
 
  +
nnoremap <F8> :!start C:\progra~1\mozill~1\firefox.exe <cfile><CR>
endfunction
 
  +
" On Windows, open current file in Internet Explorer.
map <Leader>pp :call PreviewHTML_External()<CR>
 
  +
nnoremap <F5> :update<Bar>silent !start c:\progra~1\intern~1\iexplore.exe file://%:p<CR>
  +
" On Linux, open URL under cursor in Firefox.
  +
nnoremap <F8> :silent !firefox <cfile><CR>
 
</pre>
 
</pre>
   
  +
In the above, there is no space after <code>!</code> and the Vim command has the form <code>:!start program file</code> which causes Vim (on Windows) to run the specified program with the file as an argument. If the program is not in a PATH directory, the full path to the program must be specified. If the program path contains spaces, it is necessary to put the path in quotes (<code>"..."</code>).
If you don't use mozilla, you will need to modify the function to use your preferred browser.
 
   
  +
----
==See also==
 
  +
If using <cfile> or <cWORD> does not suit a particular requirement, it is possible to use a pattern to select what is needed. For example, the following takes the first match on the current line which starts with 'http' and continues with non-whitespace characters (it finishes at the first space or tab, or at the end of the line):
'''Preview current html file in a web browser'''
 
 
<pre>
*127 Preview HTML files quickly &nbsp;&nbsp; ''(this tip)''
 
  +
nnoremap <F8> :execute 'silent ! start '.matchstr(getline('.'), 'http\S*')<CR>
*[[VimTip256|256 Opening current Vim file in your Windows browser]]
 
 
</pre>
*[[VimTip587|587 Preview current file in Mozilla through localhost]]
 
*[[VimTip684|684 Preview current HTML in browser on Mac OS X]]
 
*[[VimTip1015|1015 Preview file on localhost]]
 
   
  +
----
  +
On Windows 7 this opens the current file in Chrome (fix the path by replacing "MyName" with what it should be on your system).
 
<pre>
  +
nnoremap <silent> <F5> :update<Bar>silent !start "C:\Users\MyName\AppData\Local\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>
 
</pre>
  +
Or it might be here:
 
<pre>
  +
nnoremap <silent> <F5> :update<Bar>silent !start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>
 
</pre>
  +
----
  +
On Linux, use <code>xdg-open</code> to open the file using its default application. The first of the following mappings saves the current file, then opens it; the second opens the file under the cursor:
 
<pre>
  +
nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
  +
nnoremap <F8> :silent !xdg-open <cfile> &<CR>
 
</pre>
  +
 
==From 587 [[Preview current file in Mozilla through localhost]]==
  +
When editing an HTML file on Windows, enter the following to save the current file and open it in your default browser:
 
<pre>
  +
:update|silent ! start %:p
 
</pre>
  +
  +
On Windows, the current file can be opened in its associated application using the <code>start</code> command of the <code>cmd.exe</code> shell. With this mapping, press F5 to save the current file and open it in its associated application:
  +
<pre>
  +
nnoremap <silent> <F5> :update<Bar>silent ! start %:p<CR>
  +
</pre>
  +
  +
The space after <code>!</code> is required (on Windows, a command of the form <code>!start xxx</code> causes Vim to run <code>xxx</code> asynchronously; Vim does ''not'' run <code>start</code>, whereas <code>! start xxx</code> invokes the shell <code>cmd.exe</code> to run its <code>start</code> internal command operating on <code>xxx</code>). In the unlikely case that the associated application runs in a console (command prompt window), it would be better to add the <code>/b</code> option so the application runs in the same console as <code>start</code>. That is, change the command to <code>start /b %:p</code>.
  +
  +
It is convenient to leave the browser window open: switch back to Vim to do any further editing required, save the file, then switch to the browser and tell it to reload the file with a key like Ctrl-R or F5.
  +
  +
A Linux version of the above mapping would use a program like <code>xdg-open</code>:
  +
<pre>
  +
nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
  +
nnoremap <F5> :silent update<Bar>silent !xdg-open %:p &<CR>
  +
</pre>
  +
 
==From 684 [[Preview current HTML in browser on Mac OS X]]==
  +
*''TODO'': Clean following.
  +
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.
  +
  +
<pre>
  +
nnoremap <F5> :!open -a Safari %<CR><CR>
  +
</pre>
  +
  +
;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(?) -- "nnoremap <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(?) -- "nnoremap <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) -- "nnoremap <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)
  +
  +
Also, for Mac users, here is a way to save the current PHP file then preview it through localhost (''TODO'' see <code>%:p:s?pat?sub?</code> below for Vim procedure):
  +
<pre>
  +
nnoremap <F5> :update<Bar>!open -a Google\ Chrome `echo http://localhost/${PWD\#*/*/*/*/*/}/%`<CR>
  +
</pre>
  +
 
==From 1015 [[Preview file on localhost]]==
  +
Using this mapping will save the current file (if changed), then preview it on localhost. For example, if you are editing file <code>/var/www/html/one/two/my.html</code>, pressing F5 will use Firefox to open <code><nowiki>http://localhost/one/two/my.html</nowiki></code>.
  +
<pre>
  +
nnoremap <F5> :silent update<Bar>silent !firefox %:p:s?\(.\{-}/\)\{4}?http://localhost/?<CR>
  +
</pre>
  +
  +
The core command has the form <code>!firefox %:p:s?pat?sub?</code> which uses the shell to start Firefox to open the current file (<code>%</code>) with its name modified by forming the full path (<code>:p</code>) then substituting the pattern <code>pat</code> with <code>sub</code>.
  +
  +
The pattern is <code>\(.\{-}/\)\{4}</code> which finds exactly four occurrences of any character (<code>.</code>) repeated as few times as possible (<code>\{-}</code>) up to and including the next slash (<code>/</code>). If the file being edited is <code>my.html</code> in directory <code>/var/www/html/one/two</code>, the pattern matches <code>/var/www/html/</code> (the first occurrence of four slashes, with any characters between).
  +
  +
==From 201110 [[Preview current HTML in browser on Linux]]==
  +
This mapping allows you to use a browser to preview the HTML file currently being edited.
  +
<pre>
  +
nnoremap <F5> :silent update<Bar>silent !firefox %:p &<CR>
  +
</pre>
  +
  +
If necessary, the current file is saved, then Firefox is used to open the file. Replace "firefox" with the name of another browser if wanted, such as "chromium-browser".
  +
 
==Open an HTML file in a browser==
  +
:''TODO: Put a link somewhere in this tip to the result of merging all the stuff on opening HTML files.''
  +
 
==See also==
 
'''Open web browser to display URL under cursor'''
 
'''Open web browser to display URL under cursor'''
 
*[[VimTip306|306 Open a web-browser with the URL in the current line]]
 
*[[VimTip306|306 Open a web-browser with the URL in the current line]]
 
*[[VimTip555|555 Vim as bookmark manager]]
 
*[[VimTip732|732 Quick launch html and other Windows documents]]
 
*[[VimTip732|732 Quick launch html and other Windows documents]]
 
*[[VimTip1549|1549 Execute external programs asynchronously under Windows]] launch browser or document in associated app
 
*[[VimTip1549|1549 Execute external programs asynchronously under Windows]] launch browser or document in associated app
  +
*[[VimTip1656|1656 Automatically refresh display of html on saving file]] uses MozRepl addon for Firefox
   
 
'''Display information for word under cursor in a web browser'''
 
'''Display information for word under cursor in a web browser'''
*[[VimTip394|394 Internet search for the current word]] obsolete? see below
+
*[[VimTip394|394 Internet search for the current word]]
 
*[[VimTip598|598 PHP online help]]
 
*[[VimTip598|598 PHP online help]]
 
*[[VimTip896|896 Lookup the city and state of a given US Zip code using TCL]]
 
*[[VimTip896|896 Lookup the city and state of a given US Zip code using TCL]]
Line 63: Line 176:
 
*[[VimTip118|118 Use gvim to view page source in Internet Explorer]]
 
*[[VimTip118|118 Use gvim to view page source in Internet Explorer]]
 
*[[VimTip134|134 View Source in IE6 using VIM]]
 
*[[VimTip134|134 View Source in IE6 using VIM]]
*[[VimTip581|581 Using vim to view source and edit textarea in mozilla/firebird]] obsolete? see below
+
*[[VimTip581|581 Using vim to view source and edit textarea in mozilla/firebird]]
 
*[[VimTip805|805 Use gvim as an external editor for Windows apps]]
 
*[[VimTip805|805 Use gvim as an external editor for Windows apps]]
 
*[[VimTip1156|1156 View Source from Internet Explorer in gvim]]
 
*[[VimTip1156|1156 View Source from Internet Explorer in gvim]]
Line 69: Line 182:
 
'''Other'''
 
'''Other'''
 
*[[VimTip317|317 Vim key bindings for Firefox]]
 
*[[VimTip317|317 Vim key bindings for Firefox]]
*[[VimTip555|555 Vim as bookmark manager]]
 
 
*[[VimTip691|691 Use gf to open a file via its URL]]
 
*[[VimTip691|691 Use gf to open a file via its URL]]
  +
*[[VimTip305|305 Best Vim Tips]] has a launching section
 
  +
*[[VimTip1440|1440 Launch files in new tabs under Windows]]
{{Todo}}
 
  +
*[[VimTip1552|1552 Launch files in new tabs under Unix]]
*Above is a list of tips related to launching a web browser or document in its associated app. I plan to start merging these soon. [[User:JohnBeckett|JohnBeckett]] 09:23, August 20, 2011 (UTC)
 
  +
*[[VimTip1652|1652 Launch remote batch jobs and view results in Vim]]
*Probably some of these tips should be merged or deleted.
 
  +
*[[VimTip299|299 Open file under cursor]]
*The author of tips 394 and 581 (Xiangjiang Ma) has said that he believes they are obsolete (if true, they should be replaced with a redirect to some suitable tip).
 
  +
*[[VimTip485|485 Open a window with the man page for the word under the cursor]]
*Incorporate following material that was originally planned for a new tip by [[User:Paluh|Paluh]].
 
  +
*[[VimTip506|506 Open Windows Help files on a specific topic]]
 
  +
*[[VimTip557|557 Opening several files in vim via ListFile]]
==Alternative==
 
  +
*[[VimTip1356|1356 Open PDF files]]
There is often very handy to load snapshot from text web-browser into Vim buffer (when you want to preview html file which you are currently writing or open some website with documentation/manual/tutorial, so you can easily read it and copy some code snapshot from it to Vim). This function and mappings listed below allows you to do this very quickly (you can also use the very nice browser plugin {{script|id=1053}}, but it requires a <tt>+perl</tt> compilation of Vim).
 
 
Function is very simple and contains two variants, one using elinks and second using lynx, so you can choose which web browser you prefer:
 
<pre>
 
function ViewHTML(url)
 
if a:url == ""
 
return
 
endif
 
new
 
set buftype=nofile nonumber
 
"using lynx
 
"exe "%!lynx " . a:url . " -dump -nolist -underscore -width " . winwidth(0)
 
"usnig elinks
 
exe "%!elinks " . a:url . " -dump -dump-width " . winwidth(0)
 
endfunc
 
</pre>
 
 
Proposed mappings are:
 
<pre>
 
"Preview currently edited html file
 
nmap <Leader>H :call ViewHtml(@%)<CR>
 
"Open visually selected url
 
vmap <Leader>h "ey:call ViewHTML(@e)<CR>
 
"Open url from Linux or Windows clipboard
 
nmap <Leader>h :call ViewHTML(@*)<CR>
 
"Open url from Mac OS X clipboard
 
nmap <Leader>h :r!pbpaste<CR>"ey$:call ViewHTML(@e)<CR>
 
</pre>
 
   
 
==Comments==
 
==Comments==
  +
I have removed the "Related plugins" section as the only plugin listed is obsolete and relies on Perl to do what can be done in plain Vim. [[User:JohnBeckett|JohnBeckett]] 10:16, April 18, 2012 (UTC)
Better with dillo, a really fast an light browser... a graphical lynx in some sort.
 
See http://dillo.sourceforge.net or "apt-get install dillo" for debian users.
 
 
 
----
 
----
  +
I am working on merging a lot of tips related to opening HTML files in browsers, and documents in associated applications. I don't think anything more will be merged in to here. I will soon clean all this up and see how large the result is (some of it might then be moved elsewhere). [[User:JohnBeckett|JohnBeckett]] 11:21, April 26, 2012 (UTC)
You can also open visually selected url or clipboard content by modifying the first few line from
 
  +
----
<pre>
 
  +
In mappings, I am using F5 to open the current file and F8 to open the file under the cursor. Might rethink that after all merging complete. [[User:JohnBeckett|JohnBeckett]] 23:56, April 26, 2012 (UTC)
function PreviewHTML_TextOnly()
 
let l:fname = expand("%:p" )
 
</pre>
 
 
to
 
<pre>
 
function PreviewHTML_TextOnly(...)
 
let l:fname = a:1
 
</pre>
 
 
Then add the following maps to your .vimrc
 
<pre>
 
"Open visually selected url
 
vmap ,h "ey:call ViewHTML(@e)<CR>
 
"Open url from Mac OS X clipboard
 
nmap ,h :r!pbpaste<CR>"ey$:call ViewHTML(@e)<CR>
 
"Open url from Windows clipboard
 
nmap ,h :call ViewHTML(@*)<CR>
 
</pre>
 
 
 
----
 
----
  +
''TODO'': Probably need <code>shellescape()</code> or at least quotes in some of the mappings to handle embedded spaces. I think the Linux mappings also need <code>&</code> appended. [[User:JohnBeckett|JohnBeckett]] 05:01, April 27, 2012 (UTC)

Revision as of 16:45, 4 August 2014

Tip 127 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0


This tip shows how to use Vim to view an html file, or a web page, in a web browser. If wanted, a text-based web browser can be used to read the text from an html page into a scratch buffer in Vim.

Viewing text from an html file or a URL

A text-based web browser such as elinks can extract a formatted view of the text from an html file or a web page. The following shows how to use elinks to read that text into a scratch buffer. You might do that for a quick preview, or to copy text from the displayed html page.

function! ViewHtmlText(url)
  if !empty(a:url)
    new
    setlocal buftype=nofile bufhidden=hide noswapfile
    execute 'r !elinks ' . a:url . ' -dump -dump-width ' . winwidth(0)
    1d
  endif
endfunction
" Save and view text for current html file.
nnoremap <Leader>H :update<Bar>call ViewHtmlText(expand('%:p'))<CR>
" View text for visually selected url.
vnoremap <Leader>h y:call ViewHtmlText(@@)<CR>
" View text for URL from clipboard.
" On Linux, use @* for current selection or @+ for text in clipboard.
nnoremap <Leader>h :call ViewHtmlText(@+)<CR>

After sourcing the above, and assuming the default backslash Leader key, you can:

  • Edit an html file, then type \H to save and preview the file.
  • Visually select the full path of a local html file or a URL, then type \h to preview the file or web page.
  • Copy the full path of a local html file or a URL in another application, then type \h to preview the file or web page in Vim.

From 256 Opening current Vim file in your Windows browser

  • TODO: Finish cleaning following notes.

On Windows, using <cWORD> to open the link at the cursor in a browser does not work when the URL is in an html tag surrounded by quotes. However, using <cfile> works for both situations: plain URL and URL between html tags.

" On Windows, open URL under cursor.
nnoremap <F8> :!start C:\progra~1\intern~1\iexplore.exe <cfile><CR>
nnoremap <F8> :!start C:\progra~1\mozill~1\firefox.exe <cfile><CR>
" On Windows, open current file in Internet Explorer.
nnoremap <F5> :update<Bar>silent !start c:\progra~1\intern~1\iexplore.exe file://%:p<CR>
" On Linux, open URL under cursor in Firefox.
nnoremap <F8> :silent !firefox <cfile><CR>

In the above, there is no space after ! and the Vim command has the form :!start program file which causes Vim (on Windows) to run the specified program with the file as an argument. If the program is not in a PATH directory, the full path to the program must be specified. If the program path contains spaces, it is necessary to put the path in quotes ("...").


If using <cfile> or <cWORD> does not suit a particular requirement, it is possible to use a pattern to select what is needed. For example, the following takes the first match on the current line which starts with 'http' and continues with non-whitespace characters (it finishes at the first space or tab, or at the end of the line):

nnoremap <F8> :execute 'silent ! start '.matchstr(getline('.'), 'http\S*')<CR>

On Windows 7 this opens the current file in Chrome (fix the path by replacing "MyName" with what it should be on your system).

nnoremap <silent> <F5> :update<Bar>silent !start "C:\Users\MyName\AppData\Local\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>

Or it might be here:

nnoremap <silent> <F5> :update<Bar>silent !start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "file://%:p"<CR>

On Linux, use xdg-open to open the file using its default application. The first of the following mappings saves the current file, then opens it; the second opens the file under the cursor:

nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
nnoremap <F8> :silent !xdg-open <cfile> &<CR>

From 587 Preview current file in Mozilla through localhost

When editing an HTML file on Windows, enter the following to save the current file and open it in your default browser:

:update|silent ! start %:p

On Windows, the current file can be opened in its associated application using the start command of the cmd.exe shell. With this mapping, press F5 to save the current file and open it in its associated application:

nnoremap <silent> <F5> :update<Bar>silent ! start %:p<CR>

The space after ! is required (on Windows, a command of the form !start xxx causes Vim to run xxx asynchronously; Vim does not run start, whereas ! start xxx invokes the shell cmd.exe to run its start internal command operating on xxx). In the unlikely case that the associated application runs in a console (command prompt window), it would be better to add the /b option so the application runs in the same console as start. That is, change the command to start /b %:p.

It is convenient to leave the browser window open: switch back to Vim to do any further editing required, save the file, then switch to the browser and tell it to reload the file with a key like Ctrl-R or F5.

A Linux version of the above mapping would use a program like xdg-open:

nnoremap <F5> :update<Bar>silent !xdg-open %:p &<CR>
nnoremap <F5> :silent update<Bar>silent !xdg-open %:p &<CR>

From 684 Preview current HTML in browser on Mac OS X

  • TODO: Clean following.

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.

nnoremap <F5> :!open -a Safari %<CR><CR>
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(?) -- "nnoremap <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(?) -- "nnoremap <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) -- "nnoremap <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)

Also, for Mac users, here is a way to save the current PHP file then preview it through localhost (TODO see %:p:s?pat?sub? below for Vim procedure):

nnoremap <F5> :update<Bar>!open -a Google\ Chrome `echo http://localhost/${PWD\#*/*/*/*/*/}/%`<CR>

From 1015 Preview file on localhost

Using this mapping will save the current file (if changed), then preview it on localhost. For example, if you are editing file /var/www/html/one/two/my.html, pressing F5 will use Firefox to open http://localhost/one/two/my.html.

nnoremap <F5> :silent update<Bar>silent !firefox %:p:s?\(.\{-}/\)\{4}?http://localhost/?<CR>

The core command has the form !firefox %:p:s?pat?sub? which uses the shell to start Firefox to open the current file (%) with its name modified by forming the full path (:p) then substituting the pattern pat with sub.

The pattern is \(.\{-}/\)\{4} which finds exactly four occurrences of any character (.) repeated as few times as possible (\{-}) up to and including the next slash (/). If the file being edited is my.html in directory /var/www/html/one/two, the pattern matches /var/www/html/ (the first occurrence of four slashes, with any characters between).

From 201110 Preview current HTML in browser on Linux

This mapping allows you to use a browser to preview the HTML file currently being edited.

nnoremap <F5> :silent update<Bar>silent !firefox %:p &<CR>

If necessary, the current file is saved, then Firefox is used to open the file. Replace "firefox" with the name of another browser if wanted, such as "chromium-browser".

Open an HTML file in a browser

TODO: Put a link somewhere in this tip to the result of merging all the stuff on opening HTML files.

See also

Open web browser to display URL under cursor

Display information for word under cursor in a web browser

Configure so "view source" opens in Vim

Other

Comments

I have removed the "Related plugins" section as the only plugin listed is obsolete and relies on Perl to do what can be done in plain Vim. JohnBeckett 10:16, April 18, 2012 (UTC)


I am working on merging a lot of tips related to opening HTML files in browsers, and documents in associated applications. I don't think anything more will be merged in to here. I will soon clean all this up and see how large the result is (some of it might then be moved elsewhere). JohnBeckett 11:21, April 26, 2012 (UTC)


In mappings, I am using F5 to open the current file and F8 to open the file under the cursor. Might rethink that after all merging complete. JohnBeckett 23:56, April 26, 2012 (UTC)


TODO: Probably need shellescape() or at least quotes in some of the mappings to handle embedded spaces. I think the Linux mappings also need & appended. JohnBeckett 05:01, April 27, 2012 (UTC)