Opening files in Github from Vim

I spend a lot of time nowadays doing code reviews and helping spin people up on Stripe (or more generally, distributing knowledge about our code base). One thing I found myself needing over and over again was the ability to show someone a file or some code over IM or Slack. Since we use Github to host our repositories, this is as easy as linking to the online file.

However, it's pretty annoying to have to use Github's UI to navigate through folders, and I almost always mess up trying to type the full path in the URL (blob? tree?). I also use Command-T with Vim, which means I never remember file paths or exact file names. (If you don't use Command-T or something similar, you should—it's the single biggest improvement to my code reading/writing productivity over the last couple of years.)

"Wouldn't it be awesome if I could just open a file in Github directly from Vim with some keyboard shortcut?", I thought to myself.

I did a bunch of research to try to figure out how hard it would be to build myself (I've never written a Vim plugin before), and stumbled upon git web--browse. From the man page:

NAME
    git-web--browse - git helper script to launch a web browser

SYNOPSIS
    git web--browse [OPTIONS] URL/FILE ...

DESCRIPTION
    This script tries, as much as possible, to display the URLs and FILEs
    that are passed as arguments, as HTML pages in new tabs on an already
    opened web browser.

Sounds like exactly what I want!

Amazingly, fugitive, a Git wrapper for Vim, actually has this functionality built in. If you have fugitive installed already, try it right now: go to a file that's hosted on Github, then type in the command :Gbrowse. It'll automagically open the corresponding link (keeping in mind what branch you're on, and everything) in your browser. You can even highlight a couple lines in visual-mode to have those be highlighted on Github.

I ended up remapping :Gbrowse in my .vimrc to something easier to type, you should too:

noremap <leader>w :Gbrowse<cr>

I use this at least a few times every single day. Hope you find it useful too!