Vim Tips Wiki
Line 73: Line 73:
 
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).
 
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 you prefer:
+
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>
 
<pre>

Revision as of 11:14, 7 May 2008

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 127 Printable Monobook Previous Next

created October 4, 2001 · complexity basic · author Jamis Buck · version 6.0


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.

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:

function PreviewHTML_TextOnly()
  let l:fname = expand("%:p" )
  new
  set buftype=nofile nonumber
  exe "%!lynx " . l:fname . " -dump -nolist -underscore -width " . winwidth( 0 )
endfunction
map <Leader>pt :call PreviewHTML_TextOnly()<CR>

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_.

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.

Comments

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.


It gives me an error when I try it:

E15: Invalid expression: ; . expand( "%:p") . ")\""


You can also open visually selected url or clipboard content by modifying the first few line from

function PreviewHTML_TextOnly()
  let l:fname = expand("%:p" )

to

function PreviewHTML_TextOnly(...)
  let l:fname = a:1

Then add the following map to your .vimrc

"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>

Proposition of merge this tip with Open content of website in new buffer (using elinks text web browser)

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#1053, but it requires a +perl 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:

function ViewHTML(url)
  if a:1 == ""
    return
  endif
  
  new
  set buftype=nofile nonumber
  "using lynx
  "exe "%!lynx " . l:fname . " -dump -nolist -underscore -width " . winwidth(0)
  "usnig elinks
  exe "%!elinks " . l:fname . " -dump -dump-width " . winwidth(0)
endfunc

Proposed mappings are:

"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>

Paluh 10:33, 7 May 2008 (UTC)