I’ve been playing around with Vim’s
gx command, which in normal mode, when on a URL, opens that URL in your default browser (see
:h gx). However today I ran into an interesting problem– if a URL has a
? in it
gx thinks the URL ends at the
?. Note that I’m using OS X and Chrome is my default browser. I’m seeing this problem in both MacVim 7.4 (88) and Neovim v0.1.3-61-gf03ab69.
This might not seem like a big deal for most URLs, since usually the characters after a
? are superfluous tracking codes (though for my job they’re pretty important). However there are the very large exceptions of YouTube video URLs, like
https://www.youtube.com/watch?v=wlR5gYd6um0, and Google search results pages, like
Placing your normal mode cursor anywhere before the
? in either of those URLs and hitting
gx will open the URL up to but not past the
?, thus taking you to a different location than intended. There are probably other examples where the
? is non-trivial.
This same problem is described in this Stack Exchange question. Interestingly, the top answer to the Stack Exchange question says that the variable
g:netrx_gx “determines what will be considered part of a URI” and that by default this is set to “
Note there’s also Issue #190 in vim/vim which seems relevant, and presents some more clues. One user gives this answer, which points to a function in his vimrc that may work as a fix– I didn’t try it. Here’s what I did.
So it’s not pretty, but here goes:
The first clue I got was from the very end of that Stack Exchange answer: “Hint: Using
gx in visual mode, it will open the visually selected string (regardless of the above configuration).” The same feature is touched upon in the Vim Github issue that I mention above.
Kind of coincidentally, I recently installed the jceb/vim-textobj-uri Plugin (which requires you to install kana/vim-textobj-user first!). vim-textobj-uri, as you might guess, makes URIs text objects, and maps
au to interact with them.
Now that I had a well-made URI text object at my finger tips (which is helpful on it’s own!), I then typed this remapping into my vimrc:
nmap gx viugx<Esc>
As you can probably guess, this mapping visually selects the URI, then runs
gx (from visual mode), then Escapes you back into normal mode.
Later I refined this in two ways. First, I chose to use my
x mark in order to preserve my cursor position in the URI. And secondly, thanks to a comment from Carudo, I found that I could use the preferable
nnoremap by doing this:
nnoremap gx :normal mxviugx<Esc>`x
which you may prefer.
From my point of view this is a problem that should be solved by default by Vim itself, or at least there should exist something better to set
g:netrx_gx to. Right?
Other Strangeness with gx
Sometimes when I run
gx on some URLs (one example was:
http://vimcasts.org/episodes/meet-the-yank-register/), it seems like Vim runs a
curl command and attempts to download the source code of the file (sometimes it’s successfully and Vim opens the source code as a new buffer). This may be the case for URLs that end in trailing slashes, as this Stack Exchange user points out, though, frustratingly, I haven’t been able to reproduce the behavior consistently.