-
-
Save kristianhellquist/3082383 to your computer and use it in GitHub Desktop.
(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) |
I made a slight change to change the $HOME in the copied path to ~
which makes it easier to share with colleagues if you have standardised directory paths:
(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 (dired-replace-in-string (getenv "HOME") "~" (buffer-file-name)) ":" (number-to-string (line-number-at-pos)))))
(kill-new path-with-line-number)
(message (concat path-with-line-number " copied to clipboard"))))
For unit testing in Elixir, we test like this: MIX_ENV=test mix test path/to/test.exs:110
. Now that I can just grab the file:lineno from where point is in my buffer of test.exs to run specific tests, my dev flow is much more streamline. Thanks...
git-link will figure out your remote repo on GitHub and copy to your clipboard a link to the line number.
I did another minor change to kill the file path and line number in the same format as expected by Org for an external link. Renamed the function to be able to keep both.
This way, you can paste it into an org buffer and org-open-at-point
on the link will work ...
(defun copy-file-link-to-clipboard ()
"Copy current line in file to clipboard as 'file:</path/to/file>::<line-number>'."
(interactive)
(let ((path-with-line-number
(concat "file:" (buffer-file-name) "::" (number-to-string (line-number-at-pos)))))
(kill-new path-with-line-number)
(message (concat path-with-line-number " copied to clipboard"))))
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"))))
A slightly different implementation worked for me