Skip to content

Instantly share code, notes, and snippets.

@chemzqm
Last active May 9, 2019 16:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemzqm/a235e5151d530cdef760 to your computer and use it in GitHub Desktop.
Save chemzqm/a235e5151d530cdef760 to your computer and use it in GitHub Desktop.
Open url in vim
" ============================================================================
" Description: Open url under cursor or git repository of a module
" Author: Qiming Zhao <chemzqm@gmail.com>
" Licence: Vim licence
" Version: 0.1
" Last Modified: January 16, 2016
" ============================================================================
if exists('did_open_loaded') || v:version < 700
finish
endif
let did_open_loaded = 1
function! s:Open()
let line = getline('.')
" match url
let url = matchstr(line, '\vhttps?:\/\/[^)''" ]+')
if !empty(url)
call s:system('open '. url)
else
let mail = matchstr(line, '\v([A-Za-z0-9_\.-]+)\@([A-Za-z0-9_\.-]+)\.([a-z\.]+)')
if !empty(mail)
call s:system('open mailto:' . mail)
endif
endif
endfunction
function! s:system(cmd)
let output = system(a:cmd)
if v:shell_error && output !=# ""
echohl Error | echon output | echohl None
endif
endfunction
nnoremap <leader>o :call <SID>Open()<cr>
imap <2-LeftMouse> <C-o>:call <SID>Open()<CR>
nmap <2-LeftMouse> :call <SID>Open()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment