GBrowse selected lines and copy to clipboard

Posted on February 15, 2022 development git vim

When talking with colleagues over chat about certain parts of the code it’s very helpful to show and give context as quickly as possible.

Here’s a way to quickly copy a link to GitHub (or GitLab) of the current visual selection in Vim. You need vim-fugitive installed and an upstream provider.

vim-fugitive provides the :GBrowse command while vim-rhubarb or shumphrey/fugitive-gitlab.vim know how to handle the upstream provider.

After installing those plugins, using the :GBrowse command should open the current file in Github/Gitlab. In order to make it work for visual selection of lines, the following can be used.

:'<,'>GBrowse!

Also to make things simpler and faster, I have it re-mapped to gb as follows:

vnoremap gb :'<,'>GBrowse!

That’s it!