Open URL under cursor in Vim with browser

2020-05-11 03:09发布

I'm using Twitvim for the first time. Seeing all the URLs in there made me wonder, is there any way to open the URL under the cursor in your favorite browser or a specified one?

标签: vim
10条回答
冷血范
2楼-- · 2020-05-11 03:39

As described above by @kev, modified for Linux environments.

Aside: when this function was executed (Vim 8.1) in my terminal, the Vim screen was obfuscated (mostly "blanked;" i.e., the text was there but not visible). The :redraw! command (see https://stackoverflow.com/a/1117742/1904943) redraws the screen.

Add to ~/.vimrc:

nmap <leader>g :call Google()<CR>:redraw!<CR>
fun! Google()
  let keyword = expand("<cword>")
  let url = "http://www.google.com/search?q=" . keyword
  let path = "/usr/bin/"
  exec 'silent !"' . path . 'firefox" ' . url
endfun

查看更多
闹够了就滚
3楼-- · 2020-05-11 03:40

I'm pretty late to this party, but here's another way of doing this that works especially well on Windows 7.

  1. Install both vim-shell and vim-misc in vim I recommend doing this via ever-awesome Pathogen plugin and then simply cd ~/vimfiles/bundle & git clone git@github.com:xolox/vim-misc.git & git clone git@github.com:xolox/vim-shell.git from msysgit. (These two plugins open urls in your default browser without creating any extra command prompts or other silly nonsense usually required in windows. You can open urls in Vim like this: :Open http://duckduckgo.com. Try it. You'll like it.)

  2. Create some vim mappings so that you can quickly get the line under the cursor into the browser. I'd map this to u (for me, that's ,u from normal mode). Here's how:

    nnoremap u :exec "Open ".getline(".")

To use this mapping, type your Leader key from normal mode + u. It should read the line under your cursor and open it in your default browser.

查看更多
混吃等死
4楼-- · 2020-05-11 03:42

Add following line to .vimrc file:

nmap <leader><space> yiW:!xdg-open <c-r>" &<cr>

So in normal mode it pressing \ it selects current word and open it as address in web browser.

Leader by default is \ (but I've mapped it to , with let mapleader = ","). Similarly, using imap you can map some key sequence in insert mode (but then, if it is 2 key sequence, it probably will override some default behaviour).

查看更多
Viruses.
5楼-- · 2020-05-11 03:42

This is a sort of improved version of the script originally proposed by @tungd here https://stackoverflow.com/a/9459366/7631731. Keeps vim context and handles correctly URLS containing "#".

function! HandleURL()
  let s:uri = matchstr(getline("."), '[a-z]*:\/\/[^ >,;()]*')
  let s:uri = shellescape(s:uri, 1)
  echom s:uri
  if s:uri != ""
    silent exec "!open '".s:uri."'"
    :redraw!
  else
    echo "No URI found in line."
  endif
endfunction

nnoremap <leader>w :call HandleURL()<CR>¬
查看更多
男人必须洒脱
6楼-- · 2020-05-11 03:43

I use this script to search gooogle for keyword under cursor:

nmap <leader>g :call Google()<CR>
fun! Google()
    let keyword = expand("<cword>")
    let url = "http://www.google.com/search?q=" . keyword
    let path = "C:/Program Files/Mozilla Firefox/"
    exec 'silent !"' . path . 'firefox.exe" ' . url
endfun

You should use getline('.') and matchstr() to extract url under cursor. The rest is the same.

查看更多
不美不萌又怎样
7楼-- · 2020-05-11 03:44

Updated: from tpope's tweet today

Press gx. You can customize the browser. On Gnome and Mac OS X it's already use gnome-open/open. Generally you can set g:netrw_browsex_viewer to anything you want.


Original answer:

Don't remember where I get this function. There is a bug with hash (#) in the url, but the function works well enough that I won't bother fixing it.

function! HandleURL()
  let s:uri = matchstr(getline("."), '[a-z]*:\/\/[^ >,;]*')
  echo s:uri
  if s:uri != ""
    silent exec "!open '".s:uri."'"
  else
    echo "No URI found in line."
  endif
endfunction
map <leader>u :call HandleURL()<cr>

Note: If you are not on the Mac, use gnome-open/xdg-open for Linux, or 'path to your web browser' for Windows

查看更多
登录 后发表回答