vimtricks.wiki Concise Vim tricks, one at a time.

How do I open a URL under the cursor in my web browser from Vim?

Answer

gx

Explanation

The gx command opens the URL, file path, or identifier under the cursor using the system's default handler. For URLs, this opens your web browser. For files, it opens the associated application.

How it works

  • Place your cursor on a URL like https://example.com
  • Press gx
  • Vim detects the URL and opens it with the system's default browser

What gx can open

  • URLs: https://github.com/vim/vim → opens in browser
  • File paths: /etc/hosts → opens with default file handler
  • Plugin names: In a vim-plug config, gx on 'tpope/vim-surround' may open the GitHub page

Configuration

Vim uses the netrw plugin's g:netrw_browsex_viewer setting to decide how to open:

" Linux
let g:netrw_browsex_viewer = 'xdg-open'

" macOS
let g:netrw_browsex_viewer = 'open'

" WSL
let g:netrw_browsex_viewer = 'wslview'

In Neovim, gx is built-in and uses vim.ui.open() which auto-detects the platform.

Tips

  • gf opens the file under the cursor in Vim (as a buffer), while gx opens it externally
  • If gx doesn't work, check that netrw is loaded (:echo exists('g:loaded_netrw'))
  • Works with DOIs, email addresses (opens mail client), and other URI schemes
  • In Neovim, gx works without netrw since version 0.10

Next

How do I always access my last yanked text regardless of deletes?