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
I'm pretty late to this party, but here's another way of doing this that works especially well on Windows 7.
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.)
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.
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
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
netrw
This is a solution for people who removed netrw
(:help netrw-noload) in vim/neovim. For example, they use a different file-manager like vim-dirvish
If you are using Vim 7.4 or later, in normal mode, put your cursor below the URL, then click gx
, the URL will be opened in browser automatic
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.