Vim Tips Wiki

Tlgrok/Open external browser

< User:Tlgrok

Revision as of 20:29, January 21, 2009 by Tlgrok (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
1,616pages on
this wiki

This is a draft, quarantined as a subpage of my own userpage until (and if) it is ready to face the world. Feel free to edit it.

Similar things have been done before, but none to my liking.

Say you have a URL written down, and would like to open it:

Following this tip will allow you to open it by placing the cursor anywhere on the URL and using a key binding.

Fixing 'isfname'Edit

The 'isfname' setting is a list which characters vim recognizes as belonging to a file name. To open URLs, you need to add '?' and '&' to the list.

Add the following to your .vimrc:

set isfname-=? isfname+=?
set isfname-=& isfname+=&

By this point, placing the cursor inside a URL and typing 'gf' (no quotes, in Normal mode) would allow you to view the source code of the document you requested (using the netrw plugin, which comes preinstalled with vim). This is great for text files, but not so much for HTML.

Setting browsersEdit

We'll define two browsers - one for viewing web pages inside the console, and another for opening an X-Windows (GUI) browser.

Add the following to your .vimrc:

let g:Browser_console = 'w3m'
let g:Browser_x = 'firefox'

(Change both browsers to your favorites - links, lynx, Konqueror, Opera and so on.)

If you're using GNU Screen, and have followed these instructions, you might want to replace the first line of the code above with:

let g:Browser_console = InScreen('w3m')

Add bindingsEdit

At last, it is time to add bindings. Add the following to your .vimrc:

nnoremap <silent> gb :exe('!'.g:Browser_console.' "'.expand('<cfile>').'"')<CR>
nnoremap <silent> gB :exe('!'.g:Browser_x.' "'.expand('<cfile>').'" &')<CR>

This binds 'gb' to the console browser, and 'gB' to the X browser. You may, of course, choose different bindings.

Tip TODOEdit

  • Also create Visual mode mapping - using highlighted text.


Good stuff. I thought there were lots of tips on this, but I see you found the only one (VimTip306) at the top of this page. I have made a comprehensive list of tips concerning web browsers at VimTip127.

I don't have time to think about this at the moment but I would hope to either replace 306 with this, or maybe just make this our "open URL" tip, and delete 306 (except I haven't read 306 lately). Perhaps the title should include "URL", maybe "Open URL in a web browser"?

Ideally, isfname would only be changed when required (again I haven't thought about it, but I imagine that changing isfname permanently in your vimrc would have disadvantages). I wonder if some clever trick would allow the idea from VimTip691 to be used (extend gf so it opens a URL in a browser if it starts with "http://" or "file://").

It needs to be made clearer which parts of this work only on Linux. Perhaps have a section for Windows (or a separate tip?).

Also, if you use that GNU_Screen_used code much, it might be better to wrap it in a function so you could have something like let g:Browser_console = InScreen('w3m') --JohnBeckett 04:14, 14 January 2009 (UTC)

Tip 306 does its thing line-wise, whereas the tip above is cursor-wise. Which is better is up to person preferences. As for Tip 691, I don't like it one bit, personally. 'gf' has a well-defined behavior for URLs, and I don't care much for the idea of changing it. I don't know which parts will work on Windows. Most of it, I'd imagine. You're obviously right about the need for an InScreen function... laziness got the better of me.

Regarding isfname: Well, isfname not containing '?' and '&' has a certain bug-ish quality about it - as said above, gf supposedly supports http:// URLs, and yet it doesn't work properly without these characters - and I did consider posting a note asking whether this is the case to the development mailing list. (Again, laziness triumphs.) I don't know which is the preferable route. You could backup the user's on isfname, add these characters, and restore isfname, but I see no harm in letting them stay. As usual, it boils down to personal preferences. tlgrok 10:07, 14 January 2009 (UTC)

Around Wikia's network

Random Wiki