Vim Tips Wiki
(→‎See also: Add more links (plan to start merging soon).)
(Some fixes, lynx is obsolete.)
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 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 [[wikipedia:Lynx (web browser)|<tt>lynx</tt>]] or [[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 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.
+
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)
0d
+
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 "ey:call ViewHtmlText(@e)<CR>
+
vnoremap <Leader>h y:call ViewHtmlText(@@)<CR>
" View text for url from Linux or Windows clipboard
+
" View text for URL from clipboard.
" (on Linux, @* is the current selection; use @+ for text in clipboard).
+
" On Linux, use @* for current selection or @+ for text in clipboard.
nnoremap <Leader>h :call ViewHtmlText(@*)<CR>
+
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==
:''Following is obsolete; will expand.''
+
:''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]]
 
*[[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
Line 90: Line 77:
 
'''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
 
*[[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

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.

View an html file in a web browser

Have removed obsolete code; will replace.

See also

Preview current html file in a web browser

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)