5

When I press gx over a link in vim, it opens it in an external application, when it does this it also suspends Vim.

How do I prevent it from suspending Vim, so that I can use Vim while browsing whatever it opened externally.

4
  • 1
    Can you specify your OS, Vim version? It does not suspend here (OSX, MacVim 8.0)
    – nobe4
    Commented Jul 17, 2017 at 16:13
  • Using GVim on Windows, I do not have this problem. However, at the bottom, vim says, "Press <cr> to continue," which is just Enter. Using terminal vim in MinGW (I know, all bets are off), it doesn't open a web page, but still doesn't freeze.
    – jpaugh
    Commented Jul 17, 2017 at 19:58
  • Debian Jessie 8.4.0, Vim 7.4. Hm... mine seems to be a bit iffy, if it opens it will suspend vim, if it doesn't it will not suspend. Commented Jul 20, 2017 at 2:48
  • Vim 8 with Fedora 25 here, works by default without suspending. Commented Oct 16, 2017 at 9:26

3 Answers 3

1

Use vim-shell plugin instead with this config:

let g:netrw_nogx = 1 " disable netrw's gx mapping.
nnoremap gx :Open<CR>
2
  • 1
    Another alternative might be my xdg_open.vim plugin. Commented Aug 16, 2017 at 21:54
  • This doesn't work for me, :Open is still blocking vim. I use NVIM v0.2.2 on lubuntu 17.10
    – chriad
    Commented Jun 21, 2018 at 20:31
1

What works for me with neovim v0.2.2 on lubuntu 17.10 is the open-browser.vim plugin (https://github.com/tyru/open-browser.vim) with the following settings in my init.vim.

let g:netrw_nogx = 1 " disable netrw's gx mapping.
    nmap gx <Plug>(openbrowser-smart-search)
vmap gx <Plug>(openbrowser-smart-search)
0

I had the same problem, so I wrote my own function for it. (Thanks to the guy below who had a way to do it without having to use the registry. I modified my own with this.)

The strategy is to store the url under the Vim-cursor to a variable

let url = expand('<cWORD>')

and then use bash to open firefox with the url as argument:

call feedkeys(':!bash firefox ' . url . ' &' . "\<CR>\<ESC>" )

The & is used to open firefox in the background, and the \<CR>\<ESC> is to enter the command and escape the message box that it prompts.

We put this into a function that we call OpenURL() and then remap gx to run that function.

nnoremap gx :call OpenURL()<CR>

The <CR> is again to actually execute the command.

In total it looks like this:

function! OpenURL()
    let url = expand('<cWORD>')
    call feedkeys(':!bash firefox ' . url . ' &' . "\<CR>\<ESC>" )
endfunction

nnoremap gx :call OpenURL()<CR>
1
  • 1
    Welcome to Vi and Vim! This answer would benefit greatly from an edit describing how the code works. (PS you can save/restore registers; combined with a try/catch, you could make sure not to lose whatever is already saved in @a, if you were interested. But you can skip registers entirely: let url = expand('<cWORD>').)
    – D. Ben Knoble
    Commented Aug 31, 2021 at 20:33

Your Answer

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

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