Skip to content

Instantly share code, notes, and snippets.

@wsgac
Created June 11, 2025 09:59
Show Gist options
  • Save wsgac/173fdd77273ba65b0865139ced6dcb28 to your computer and use it in GitHub Desktop.
Save wsgac/173fdd77273ba65b0865139ced6dcb28 to your computer and use it in GitHub Desktop.
Elisp function for getting position in files + an opinionated keybinding
(require 'cl-lib)
(defun get-current-file-path (prefix)
"Get filesystem from current buffer, provided it's a file buffer. With
prefix 4 also append line number. With prefix 16 append column number to
everything. Put the resulting string in the kill-ring and also display
it int the minibuffer."
(interactive "p")
(let* ((file-name (buffer-file-name (current-buffer)))
(position (when (>= prefix 4) (list ":" (int-to-string (line-number-at-pos)))))
(position (if (>= prefix 16) (append position (list ":" (int-to-string (current-column)))) position)))
(if file-name
(let ((file-name-and-line (apply #'cl-concatenate 'string file-name position)))
(kill-new file-name-and-line)
(message file-name-and-line))
(message "Not a file buffer. Cannot get path and line number."))))
(keymap-global-set "M-g M-f" 'get-current-file-path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment