Skip to content

Instantly share code, notes, and snippets.

@danchoi
Last active December 22, 2015 09:38
Show Gist options
  • Select an option

  • Save danchoi/6453034 to your computer and use it in GitHub Desktop.

Select an option

Save danchoi/6453034 to your computer and use it in GitHub Desktop.
Custom Vim function to open hyperlinks
" Easily open hyperlinks in any Vim buffer:
" Place cursor before or at the beginning of a URL that
" begins with http: or https: and press <leader>o
let s:http_link_pattern = 'https\?:[^ >)\]]\+'
let g:browser_command = 'open '
func! s:find_href()
let res = search(s:http_link_pattern, 'cw')
if res != 0
let href = matchstr(expand("<cWORD>") , s:http_link_pattern)
return href
end
endfunc
func! s:open_href_under_cursor()
let command = g:browser_command . " '" . shellescape(s:find_href()) . "' "
call system(command)
endfunc
nnoremap <leader>o :call <SID>open_href_under_cursor()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment