created 2001 · complexity basic · author Jamis Buck · 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 lynx or elinks can extract a formatted view of the text from an html file or a web page. The following shows how to use these programs 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 " Using lynx. " execute 'r !lynx ' . a:url . ' -dump -nolist -underscore -width ' . winwidth(0) " Using elinks. execute 'r !elinks ' . a:url . ' -dump -dump-width ' . winwidth(0) 0d 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 "ey:call ViewHtmlText(@e)<CR> " View text for url from Linux or Windows clipboard " (on Linux, @* is the current selection; use @+ 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.
The above script assumes elinks has been installed. If you prefer lynx, uncomment the line that executes lynx (remove the leading quote), and comment out the line that executes elinks.
View an html file in a web browser
- Following is obsolete; will expand.
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:
function PreviewHTML_External() exe "silent !mozilla -remote \"openurl(file://"; . expand( "%:p" ) . ")\"" endfunction map <Leader>pp :call PreviewHTML_External()<CR>
If you don't use mozilla, you will need to modify the function to use your preferred browser.
See also
Preview current html file in a web browser
- 127 Preview HTML files quickly (this tip)
- 256 Opening current Vim file in your Windows browser
- 587 Preview current file in Mozilla through localhost
- 684 Preview current HTML in browser on Mac OS X
- 1015 Preview file on localhost
Open web browser to display URL under cursor
- 306 Open a web-browser with the URL in the current line
- 732 Quick launch html and other Windows documents
- 1549 Execute external programs asynchronously under Windows launch browser or document in associated app
- 1656 Automatically refresh display of html on saving file uses MozRepl addon for Firefox
Display information for word under cursor in a web browser
- 394 Internet search for the current word obsolete? see below
- 598 PHP online help
- 896 Lookup the city and state of a given US Zip code using TCL
- 922 Launch lynx to get info for the current word
- 926 QT Help from Vim
- 933 Search the web for text selected in Vim
- 1200 View PHP documentation for current word
- 1354 Online documentation for word under cursor
- 1377 Context sensitive access to MSDN help
Configure so "view source" opens in Vim
- 118 Use gvim to view page source in Internet Explorer
- 134 View Source in IE6 using VIM
- 581 Using vim to view source and edit textarea in mozilla/firebird obsolete? see below
- 805 Use gvim as an external editor for Windows apps
- 1156 View Source from Internet Explorer in gvim
Other
TO DO
- 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. JohnBeckett 09:23, August 20, 2011 (UTC)
- Probably some of these tips should be merged or deleted.
- 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).
Related plugins
- web browser plugin requires a +perl build of Vim