This little bit of elisp is probably my most used command. I use it a dozen times a day for sending links to colleagues. When run with my cursor over a line of code it will generate a GitHub url linking straight to the line of code.
Someday I'll make it smart enough to read the git remote and pull the url from there so it can work with non-GitHub repos.
(global-set-key (kbd "C-c cg")
'ec-generate-github-file-url)
(defun ec-generate-github-file-url ()
"Generate a GitHub URL for the current buffer file and line number."
(interactive)
(if buffer-file-name
(progn
(kill-new
(format "https://github.com/%s/blob/%s/%s#L%d"
(replace-regexp-in-string
"https://github.com/\\(.*?\\)\\(.git\\)?$" "\\1"
(string-trim (shell-command-to-string
"git config --get remote.origin.url")))
(string-trim (shell-command-to-string "git rev-parse HEAD"))
(file-relative-name buffer-file-name
(vc-git-root buffer-file-name))
(line-number-at-pos)))
(message "Copied GitHub URL"))
(message "Not visiting a file.")))