Emacs Redux

Return to the Essence of Text Editing

Copy Filename to the Clipboard

| Comments

Sometimes I need to copy the name of the currently visited file to the clipboard. Emacs does not have a built-in command for that, but cooking one is pretty straightforward:

1
2
3
4
5
6
7
8
9
(defun copy-file-name-to-clipboard ()
  "Copy the current buffer file name to the clipboard."
  (interactive)
  (let ((filename (if (equal major-mode 'dired-mode)
                      default-directory
                    (buffer-file-name))))
    (when filename
      (kill-new filename)
      (message "Copied buffer file name '%s' to the clipboard." filename))))

Evaluate the new bit of code in Emacs (maybe by using C-M-x somewhere in the body of the function definition) and invoke the command with M-x copy-file-name-to-clipboard.

This command is part of Prelude(it’s named prelude-copy-file-name-to-clipboard there).

Comments