Vim Tips Wiki
Advertisement
Tip 127 Printable Monobook Previous Next

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

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

 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

Comments

Advertisement