Skip to content

Instantly share code, notes, and snippets.

@callahanrts
Last active November 25, 2021 09:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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)#git@github.com:}%.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>
@callahanrts
Copy link
Author

Any suggestions on making this a multi-line function would be great.

@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)#git@github.com:}%.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