Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-04-2009, 07:42 AM   #1
LQ Newbie
Registered: Apr 2009
Location: different day, different place
Distribution: SuSE, considering Mint
Posts: 15

Rep: Reputation: 0
Using mapping in VIM to add HTML?

Hi, I am trying to figure out how to make VIM automatically (if it is possible, though most things are with vim) turn a text file into an HTML file.

That is, I have a list of movies that i have, the list contains the Directory Path and the name of the movie like this:
SciFi & Fantasy\Alien Raiders (2008)
SciFi & Fantasy\Cube (1997)
SciFi & Fantasy\Gattaca
SciFi & Fantasy\Equilibrium
SciFi & Fantasy\Day Watch
SciFi & Fantasy\Men In Black
SciFi & Fantasy\Sixth Sense
SciFi & Fantasy\Next (2007)
SciFi & Fantasy\Night Watch
SciFi & Fantasy\Men In Black 2

I want to make it look like this

SciFi & Fantasy\<a href="">Alien Raiders (2008)</a><br>
SciFi & Fantasy\<a href="">Cube (1997)</a><br>
SciFi & Fantasy\<a href="">Gattaca</a><br>
SciFi & Fantasy\<a href="">Equilibrium</a><br>
SciFi & Fantasy\<a href="">Day Watch</a><br>
SciFi & Fantasy\<a href="">Men In Black</a><br>
SciFi & Fantasy\<a href="">Sixth Sense</a><br>
SciFi & Fantasy\<a href="">Next (2007)</a><br>
SciFi & Fantasy\<a href="">Night Watch</a><br>
SciFi & Fantasy\<a href="">Men In Black 2</a><br>

about as far as i have gotten is this:
:map _ $^[?\a<a href="

but i am not sure how to copy the text then add the "+" then paste etc

Any help would really be appreciated!!


Old 05-04-2009, 02:46 PM   #2
LQ Newbie
Registered: Apr 2009
Posts: 6

Rep: Reputation: 1
Looks like you need to take everything from the backslash until the end of the line, and surround the remainder with a block of text that duplicates the text.

So XXX\YYY becomes XXX\<a ...YYY>YYY</a> (well, pretty much ...)

Part of it is tricky, cos you need to do a bit of jiggery pokery with the spaces (turning them into +s), but let's gloss over that for now ...

:%s/\\\(.*\)/\\<a href=...\1...>\1<\\a><br>/

The triple-\ is a bit of a red herring: the first two represent an escaped backslash (cos you've really got backslashes in your text file) and the next one opens up a \(-\) block, which surrounds a block of text that we'll pick up later with \1, which occurs twice in the replacement text, cos you really do want to see it twice in your HTML output.

To turn the spaces into +s, you need to get a bit fiddly: you could replace them all first, and then turn some of them back afterwards, I guess. Can't produce the fiddly vim business of the top of my head right now, but if I think of something inspired, I'll post it.

By the way, I'm not sure why you want to do this with mapping: I guess it would let you store the mapping and use it loads of times, but I'm not sure if you really need to do that, or if you just think that mapping is cool.

Using sed might be a bit easier: the commands are pretty much the same as vi, but it's non-interactive. (That's not the complete truth of the distinction between vi and sed, but it's close.)

Hope this helps.
Old 05-04-2009, 06:43 PM   #3
Senior Member
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
While I'm not sure about doing it natively in VIM, the above shouldn't take more than a few lines of perl, or even with judicious use of cat, awk, and a temp file. In either case, I'm interested in what you come up with.
Old 05-04-2009, 11:57 PM   #4
LQ Newbie
Registered: Apr 2009
Location: different day, different place
Distribution: SuSE, considering Mint
Posts: 15

Original Poster
Rep: Reputation: 0
Hello and thanks to Dave for his input. I had also posted on the comp.editors newsgroup and one of the regex masters suggested the following which worked flawlessly:

%s/\\\zs[^\\]*$/\=printf('<a href="http:\/\/\/find?s=all&q=%s&x=0&y=0">%s<\/a><br>',join(split(submatch(0),' '),'+'), submatch(0))/

As for why i wanted mapping, i didn't i'm just a noob and didn't know i could run a command like that, for some reason i thought it had to be mapped.

Cheers (and thanks for the input!)



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please add a group for Editors like Xemacs, VIM, etc. MrUmunhum LQ Suggestions & Feedback 1 08-26-2008 03:11 PM
LXer: Mind mapping tools add new dimensions to old technique LXer Syndicated Linux News 0 08-06-2007 07:31 PM
vim add # automatically noir911 Linux - Software 11 09-13-2006 10:56 PM
mapping a key to compile a program in vim dvm Linux - Software 1 01-26-2005 07:56 AM
Mapping Esc to windows key in Vim frankie_DJ Linux - Newbie 2 10-14-2004 02:56 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration