Copy github permalink for selected file and line

Hey all.

In neovim there was a very useful plugin called gitlinker which would grab a github permalink for the file and line you currently had selected. GitHub - linrongbin16/gitlinker.nvim: Maintained fork of ruifm's gitlinker, refactored with bug fixes, ssh aliases, blame support and other improvements.

I was missing it in kak so I figured I would try to replicate its core functionality. It uses OSC52 for the copy mechanism.

I figured other kak users might find it useful as well. Cheers!

define-command copy-github-permalink %{
    evaluate-commands %sh{
        file=$(printf "%s" "$kak_buffile" | sed "s|^$(pwd)/||")
        line="$kak_cursor_line"
        github_remote=$(git config --get-regexp '^remote\..*\.url' | grep github.com | head -1 | awk '{print $2}')
        if [[ -z "$github_remote" ]]; then
            printf "echo 'error: No github remote found'"
        else
            remote=$(echo "$github_remote" | sed 's|^.*://||;s|^git@||;s|:|/|;s|\.git$||')
            commit=$(git rev-parse HEAD)
            url="https://$remote/blob/$commit/$file#L$line"
            encoded=$(printf %s "$url" | base64 | tr -d '\n')
            printf "set-register '\"' '%s'\n" "$url"
            printf "nop %%sh{ printf '\\e]52;;%s\\e\\\\' '%s' >/dev/tty }\n" "$encoded"
            printf "echo 'Copied github permalink to clipboard'"
        fi
    }
}

map global user u ':copy-github-permalink<ret>' -docstring 'Copy github permalink'
1 Like