Skip to content

Instantly share code, notes, and snippets.

@offby1
Created August 16, 2017 14:18
Show Gist options
  • Save offby1/12eb47295bdd71db17dceb9abdab5dcf to your computer and use it in GitHub Desktop.
Save offby1/12eb47295bdd71db17dceb9abdab5dcf to your computer and use it in GitHub Desktop.
(defun copy-buffer-file-name ()
"Puts the file name of the current buffer (or the current directory,
if the buffer isn't visiting a file) onto the kill ring, so that
it can be retrieved with \\[yank], or by another program."
(interactive)
(let ((fn (or
(buffer-file-name (current-buffer))
;; Perhaps the buffer isn't visiting a file at all. In
;; that case, let's return the directory.
(expand-file-name default-directory))))
(when (null fn)
(error "Buffer doesn't appear to be associated with any file or directory."))
(kill-new fn)
(message "%s" fn)
fn))
(global-set-key (kbd "C-/") 'copy-buffer-file-name)
(global-set-key (kbd "<f8>") 'copy-buffer-file-name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment