24

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?

11
  • 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. Mar 31, 2015 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). Mar 31, 2015 at 13:29
  • 1
    It's a variable, not an option, so you need to use let. Mar 31, 2015 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. Apr 4, 2015 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, 2015 at 11:05

2 Answers 2

15

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).

2
  • 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, 2015 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, 2015 at 21:28
2

On my Mac, I had to use the following in my .vimrc

nmap gx viW"ay:!open <C-R>a &<CR>

Explanation:

  • viW"ay copies to the a register
  • <C-R>a pastes from it
  • !open calls MacOS default open util.
1
  • This is awesome and nearly too simple to not be angry about myself to have lived w/o that for so long :-) That way I can add custom openers e.g. for path only github repos in my plugins.lua file nmap gh viW"ay:!firefox https://github.com/<C-R>a &<CR>
    – Red Pill
    Jul 13, 2021 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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