I’m Paul Herron, a full-stack developer and technical manager.
I focus mainly on back-end development with tools like Symfony
and devops with tools like Docker

Vim: get a link to the current file in Bitbucket or GitHub

by Paul Herron on 19 April 2020

I’m not sure why I didn’t think to set this up before, as linking someone to a specific line of a file in a Bitbucket (or GitHub) repository is something I’ve been doing many times a day.

Vim GitHub link

Luckily my projects tend to be stored in a local directory that matches the naming structure in the remote VCS - for example my Vim will be in ~/Projects/wirelesslogic/simpro while the repository in Bitbucket will be at https://bitbucket.org/wirelesslogic/simpro.

On that basis, some fairly generic mappings like the following in my ~/.vimrc tend to work for most of my projects (and the various microservices within them):

" Copy a BitBucket link to the current file (and line) to the clipboard
nmap <Leader>B :let projectPath = trim(execute('pwd')) \| let @+ = 'https://bitbucket.org/' . fnamemodify(projectPath, ':p:h:h:t') . '/' . fnamemodify(projectPath, ':p:h:t') . '/src/master/' . expand("%") . '#lines-' . line(".")<CR><CR>

" Copy a GitHub link to the current file (and line) to the clipboard
nmap <Leader>G :let projectPath = trim(execute('pwd')) \| let @+ = 'https://github.com/' . fnamemodify(projectPath, ':p:h:h:t') . '/' . fnamemodify(projectPath, ':p:h:t') . '/blob/master/' . expand("%") . '#L' . line(".")<CR><CR>

Vim local file

This makes it so if I’m looking at line 7 in the above file, I can do <Leader>G and get a GitHub link to that line of code (like https://github.com/paulherron/energenie/blob/master/nginx/energenie#L7) onto my clipboard.

Back to homepage Back to blog listing

Next: