Writing huge swathes of text on Webpages without putting in hyperlinks looks – I dunno, somehow a bit impolite. So I wrote a vim/python macro that lets me wrap selected text with its corresponding URL, pulled from Google’s top entry. So I can write A Mighty Wind, select it, hit “,g” and – voila – it becomes A Mighty Wind. You can also hit “,g” in normal mode and it’ll wrap the word the cursor is sitting on. Here’s my hack – you’ll also need Mark Pilgrim‘s pygoogle code.
In your .vimrc, add these lines:
1 |
" ,g - create google a href for word under cursor map ,g <Esc>Bm`:r!ghref <cword><CR>"gdd``i<C-R><C-R>g<esc>dw vmap ,g yvgvdm`:r!ghref '<C-R>=substitute(@0, "['\n]", " ", "g")<cr>'<cr>"gdd``i<C-R><C-R>g<Esc> |
Now put this program somewhere in your PATH, saved as “ghref”
1 |
#!/usr/bin/python ## # ghref - takes a string on command line, looks it up in google, outputs # the HTML for a hyperlink to the "I'm feeling lucky" URL ## import google import sys import re def main(argv): search=argv[0] data = google.doGoogleSearch(search) if (data.results[0].summary): title = data.results[0].summary else: title = data.results[0].title title = re.sub('</*b>','',title) print '<a href="%s" title="%s">%s</a>' % (data.results[0].URL, title, argv[0]) if __name__ == '__main__': main(sys.argv[1:]) |
It’s just a fiddled-around recorded macro. I imagine there’s bugs – I fixed a couple just copying this out. But maybe it’ll inspire someone else to write a better version.