JohnBeckett (talk | contribs) (→See also: Add more links (plan to start merging soon).) |
JohnBeckett (talk | contribs) (Some fixes, lynx is obsolete.) |
||
Line 5: | Line 5: | ||
|created=2001 |
|created=2001 |
||
|complexity=basic |
|complexity=basic |
||
− | |author= |
+ | |author= |
|version=6.0 |
|version=6.0 |
||
|rating=4/12 |
|rating=4/12 |
||
Line 14: | Line 14: | ||
==Viewing text from an html file or a URL== |
==Viewing text from an html file or a URL== |
||
− | A text-based web browser such as |
+ | A text-based web browser such as [[wikipedia:Elinks|<tt>elinks</tt>]] 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! ViewHtmlText(url) |
function! ViewHtmlText(url) |
||
Line 20: | Line 20: | ||
new |
new |
||
setlocal buftype=nofile bufhidden=hide noswapfile |
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) |
execute 'r !elinks ' . a:url . ' -dump -dump-width ' . winwidth(0) |
||
− | + | 1d |
|
endif |
endif |
||
endfunction |
endfunction |
||
Line 30: | Line 27: | ||
nnoremap <Leader>H :update<Bar>:call ViewHtmlText(expand('%:p'))<CR> |
nnoremap <Leader>H :update<Bar>:call ViewHtmlText(expand('%:p'))<CR> |
||
" View text for visually selected url. |
" View text for visually selected url. |
||
− | vnoremap <Leader>h |
+ | vnoremap <Leader>h y:call ViewHtmlText(@@)<CR> |
− | " View text for |
+ | " View text for URL from clipboard. |
− | " |
+ | " On Linux, use @* for current selection or @+ for text in clipboard. |
− | nnoremap <Leader>h :call ViewHtmlText(@ |
+ | nnoremap <Leader>h :call ViewHtmlText(@+)<CR> |
</pre> |
</pre> |
||
Line 40: | Line 37: | ||
*Visually select the full path of a local html file or a URL, then type <tt>\h</tt> to preview the file or web page. |
*Visually select the full path of a local html file or a URL, then type <tt>\h</tt> to preview the file or web page. |
||
*Copy the full path of a local html file or a URL in another application, then type <tt>\h</tt> to preview the file or web page in Vim. |
*Copy the full path of a local html file or a URL in another application, then type <tt>\h</tt> to preview the file or web page in Vim. |
||
− | |||
− | The above script assumes <tt>elinks</tt> has been installed. If you prefer <tt>lynx</tt>, uncomment the line that executes <tt>lynx</tt> (remove the leading quote), and comment out the line that executes <tt>elinks</tt>. |
||
==View an html file in a web browser== |
==View an html file in a web browser== |
||
− | :'' |
+ | :''Have removed obsolete code; will replace.'' |
− | 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> |
||
− | function PreviewHTML_External() |
||
− | exe "silent !mozilla -remote \"openurl(file://"; . expand( "%:p" ) . ")\"" |
||
− | endfunction |
||
− | map <Leader>pp :call PreviewHTML_External()<CR> |
||
− | </pre> |
||
− | |||
− | If you don't use mozilla, you will need to modify the function to use your preferred browser. |
||
==See also== |
==See also== |
||
Line 66: | Line 52: | ||
'''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]] |
||
⚫ | |||
*[[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 |
||
Line 90: | Line 77: | ||
'''Other''' |
'''Other''' |
||
*[[VimTip317|317 Vim key bindings for Firefox]] |
*[[VimTip317|317 Vim key bindings for Firefox]] |
||
⚫ | |||
*[[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 |
*[[VimTip305|305 Best Vim Tips]] has a launching section |
||
Line 101: | Line 87: | ||
*[[VimTip557|557 Opening several files in vim via ListFile]] |
*[[VimTip557|557 Opening several files in vim via ListFile]] |
||
*[[VimTip1356|1356 Open PDF files]] |
*[[VimTip1356|1356 Open PDF files]] |
||
− | |||
− | ==Related plugins== |
||
− | *{{script|id=1053|text=web browser plugin}} requires a <tt>+perl</tt> build of Vim |
||
==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) |
Revision as of 10:16, 18 April 2012
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.
View an html file in a web browser
- Have removed obsolete code; will replace.
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
- Preview current HTML in browser on Linux proposed new tip from 201110
Open web browser to display URL under cursor
- 306 Open a web-browser with the URL in the current line
- 555 Vim as bookmark manager
- 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
- 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
- 805 Use gvim as an external editor for Windows apps
- 1156 View Source from Internet Explorer in gvim
Other
- 317 Vim key bindings for Firefox
- 691 Use gf to open a file via its URL
- 305 Best Vim Tips has a launching section
- 1440 Launch files in new tabs under Windows
- 1552 Launch files in new tabs under Unix
- 1652 Launch remote batch jobs and view results in Vim
- 299 Open file under cursor
- 485 Open a window with the man page for the word under the cursor
- 506 Open Windows Help files on a specific topic
- 557 Opening several files in vim via ListFile
- 1356 Open PDF files
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)