Skip to content

Instantly share code, notes, and snippets.

@callahanrts
Last active November 25, 2021 09:14
Show Gist options
  • Save callahanrts/9274ad952b3e79cccc44 to your computer and use it in GitHub Desktop.
Save callahanrts/9274ad952b3e79cccc44 to your computer and use it in GitHub Desktop.
Vim command to open a browser tab at the selected line of any git project.
function! ShowOnGithub()
let u = system("echo ${${${$(git --git-dir=.git config --get remote.origin.url)#[email protected]:}%.git}#https://github.com/} | xargs echo -n")
silent exec "!open "."https://github.com/".u."/blob/master/".@%.'\#L'.line(".")
endfunction
command! -nargs=0 ShowOnGithub call ShowOnGithub()
nnoremap <Leader>gh :ShowOnGithub<CR>
@vheon
Copy link

vheon commented Feb 27, 2015

Actually it doesn't work for me in the first place... I get some error from bash. Anyway for multiline you could try something like:

function! ShowOnGithub(line1, line2)
  let u = system("echo ${${${$(git --git-dir=.git config --get remote.origin.url)#[email protected]:}%.git}#https://github.com/} | xargs echo -n")
  let line = '#'.a:line1
  if a:line1 != a:line2
    let line .= '-L'.a:line2
  endif
  silent exec "!open "."https://github.com/".u."/blob/master/".@%.'\'.line
endfunction
command! -range -nargs=0 ShowOnGithub call ShowOnGithub(<line1>, <line2>)

nnoremap <Leader>gh :ShowOnGithub<CR>
xnoremap <Leader>gh :ShowOnGithub<CR>

I'm not a "vim-command-maker-expert" so take this until someone else comes by with a better solution 👍

@dstokes
Copy link

dstokes commented Feb 28, 2015

https://github.com/tpope/vim-fugitive does this pretty well via :Gbrowse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment