Skip to content

Instantly share code, notes, and snippets.

@kristianhellquist
Created July 10, 2012 09:50
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kristianhellquist/3082383 to your computer and use it in GitHub Desktop.
Save kristianhellquist/3082383 to your computer and use it in GitHub Desktop.
Emacs, copy current file and line number to clipboard
(defun copy-current-line-position-to-clipboard ()
"Copy current line in file to clipboard as '</path/to/file>:<line-number>'"
(interactive)
(let ((path-with-line-number
(concat (buffer-file-name) ":" (number-to-string (line-number-at-pos)))))
(x-select-text path-with-line-number)
(message (concat path-with-line-number " copied to clipboard"))))
(define-key global-map (kbd "M-l") 'copy-current-line-position-to-clipboard)
@anuvyklack
Copy link

I add option to replace $HOME with ~ only if C-u argument was passed:

(defun copy-current-line-position-to-clipboard ()
  "Copy current line in file to clipboard as '</path/to/file>:<line-number>'."
  (interactive)
  (let* ((path (if (equal current-prefix-arg '(4))
                   (string-replace (getenv "HOME") "~" (buffer-file-name))
                 (buffer-file-name)))
         (path-with-line-number (concat path ":" (number-to-string (line-number-at-pos)))))
    (kill-new path-with-line-number)
    (message (concat path-with-line-number " copied to clipboard"))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment