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
This is simple, just replace "start" with whatever your OS uses
GU for go url!
" Open url
if (has('win32') || has('win64'))
nmap gu :exec "!start <cWORD>"<cr>
else
nmap gu :exec "!open <cWORD>"<cr>
endif
You should take a quick look to this vim plugin
henrik/vim-open-url
It's basically what has been explained in some other responses, but specific configuration needed if you use a plugin manager :)
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>¬
Ok so using the answers from @tungd and @kev and a little research I ended up with this script, which works just the way I need to. Like @tungd said the # can give a problem if inside a url but I'm cool with that, if anyone has a better expression for the url then it will be welcomed.
function! OpenUrlUnderCursor()
let path="/Applications/Safari.app"
execute "normal BvEy"
let url=matchstr(@0, '[a-z]*:\/\/[^ >,;]*')
if url != ""
silent exec "!open -a ".path." '".url."'" | redraw!
echo "opened ".url
else
echo "No URL under cursor."
endif
endfunction
nmap <leader>o :call OpenUrlUnderCursor()<CR>
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).