Last active
May 9, 2019 16:28
-
-
Save chemzqm/a235e5151d530cdef760 to your computer and use it in GitHub Desktop.
Open url in vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" ============================================================================ | |
" Description: Open url under cursor or git repository of a module | |
" Author: Qiming Zhao <[email protected]> | |
" 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