17

By default, if a buffer in vim contains a URL such as http://www.google.com, gx in normal mode will open a browser with that URL. That's very handy. However, if the URL has URL parameters (e.g. http://www.google.com/search?q=stuff), then the URL parameters are missed out (i.e. just http://www.google.com/search is opened).

I am using MacVim 7.4 patch 183, with Chrome as the primary browser (on OS X 10.9).

Is there an easy way, ideally with minimal customization, to make gx recognise a full URL?

  • I can't reproduce that with vim 7.4 on linux. The parameter is passed to firefox correctly when using 'let g:netrw_browsex_viewer='firefox' for me. – PhilippFrank Mar 31 '15 at 13:27
  • @PhilippFrank The example URL doesn't work for me either (Vim 7.4, Linux) ... In any case, my solution is to select the URL in visual mode, and then use gx (which is not ideal but better than copy/paste). – Martin Tournoij Mar 31 '15 at 13:29
  • 1
    It's a variable, not an option, so you need to use let. – Martin Tournoij Mar 31 '15 at 13:50
  • 1
    I cannot reproduce with GVim 7.4 on Windows and Chrome. All the URLs you mentioned are opened exactly by gx. – Gonçalo Ribeiro Apr 4 '15 at 14:57
  • 1
    Confirming the OP's error. Tried with VIM 7.4.473 & 7.4.663. Tried from prestine user account (without .vimrc), with different terminals (urxvt, sakura) and different browsers (g:netrw_browsex_viewer set to 'uzbl-browser'/'firefox'/'google-chrome'. – user569825 Apr 6 '15 at 11:05
10

As @Carpetsmoker points out in his comment, g:netrw_gx determines what will be considered part of a URI.

By default it is set to "<cfile>". From the docs (:help <cfile>):

<cfile> is replaced with the path name under the cursor

It escapes me, why parameter parts (i.e. ?) of a URL are being disregarded as parts of a "path name", but I finally chose to set the following, as my solution:

" Use whole "words" when opening URLs.
" This avoids cutting off parameters (after '?') and anchors (after '#'). 
" See http://vi.stackexchange.com/q/2801/1631
let g:netrw_gx="<cWORD>"                                                                                                                   

Now this includes whatever W would select. (Try viW on a string in VIM.)

From the docs: <cWORD> is replaced with the WORD under the cursor


Hint: Using gx in visual mode, it will open the visually selected string (regardless of the above configuration).

  • Another strange behaviour: When the URI ends in a /, it's first downloaded (with curl) and then opened. Removing the trailing /, the URI is directly opened in the browser. – user569825 Apr 17 '15 at 18:10
  • 2
    "path name" is defined by the 'isfname' option, which doesn't include ? as a valid file name character by default. Changing this option requires care because it can't be set local to the buffer. Unless NetRW is changing this option, then that explains why the OPs URLs don't work with gx. Maybe there's a difference in NetRW versions being used, which would explain the different behavior Carpetsmoker sees. – jamessan Apr 17 '15 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.