sponsor Vim development Vim logo Vim Book Ad

basic Tip #706: Make html auto-readable in vim

 tip karma   Rating -21/33, Viewed by 2258 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   April 21, 2004 16:02      complexity:   basic
author:   http://www.cs.albany.edu/~mosh - Mohsin      as of Vim:   6.0

Make html readable in gvim, add this to your ~/_vimrc

function! Mosh_html2text()
    :silent! %s/&lt;/</g
    :silent! %s/&gt;/>/g
    :silent! %s/&amp;/&/g
    :silent! %s/&quot;/"/g
    :silent! %s/&nbsp;/ /g
    :silent! %s/&ntilde;/\~/g
    :silent! %s/<P>//g
    :silent! %s/<BR>/^M/g
    :silent! %s/</\?[BI]>/ /g
    :set readonly
endfun
:autocmd BufRead *.htm* :call Mosh_html2text()

" -- Mohsin Ahmed, http://www.cs.albany.edu/~mosh    ;

 rate this tip  Life Changing Helpful Unfulfilling 

<< Make html auto-readable in vim | Map xmmsctrl commandos in you vimrc file >>

Additional Notes

Anonymous, April 23, 2004 7:07
When I open HTML in Vim, it's typically to edit it.  Once I've converted all the character entities using your autocommand, I can't save the HTML file and have it work. . .
Anonymous, April 23, 2004 8:33
This tip is crap. And you totally screwed up the tip counts.  Thanks alot.  

If you really want to read html text... how about opening the htm file in a browser and copy and paste the text into vim.
Anonymous, April 23, 2004 12:38
As far as I remember I once had problems when trying to add a tip here. In the end I ended up with three or four tips with same content. So I guess it's not Mohsin's fault, it's just a badly maintained site.
nick@kocharhook.com, April 26, 2004 0:28
How about :%s/<[^>]*>//g ?

And really, this would be better achieved by opening said HTML file in lynx (:!lynx %) or your default web browser (:!open %). You'd preserve the HTML formatting.

Oh, wait. You're running Windows. Never mind.
rpilkey@magma.ca, April 29, 2004 18:10
There are already scripts that do this kind of switcheroo that are a better place to start than the code in this note.  e.g. my own unicodeswitch.vim (vimscript #909) or unilatex.vim (vimscript #284), or html_umlaute.vim (vimscript #907)

Rog
antoine.mechelynck@belgacom.net, May 28, 2004 13:52
Even on Windows, I can open the file in Lynx (now that I've installed it, from http://lynx.browser.org/ ) or in my default browser (using ": !start %"). And me too, when I open an html file in Vim it is in order to modify it (to update my web site, fot instance), so use the proposed tip as a function or a script to be run on demand, why not (though it's neater in a browser anyway, and what about entities like é Æ œ &8212; ?), but as an autocommand for HTML, no.

Regards,
Tony.
antoine.mechelynck@belgacom.net, May 28, 2004 13:59
Oops... I didn't want my entities to be interpreted. What about

&eacute;    (e with acute)
&AElig;    (AE ligature)
&#339;    (oe ligature)
&#8212;    (em dash)

etc. There would be quite a number if we wanted to be complete, and by the time we were, we would just have a poor imitation of Lynx.

Tony.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo