when editing a file (for example a .pl file)

Whats the quickest way to get the full path (path + filename) of the currently editing file in to the OS clipboard.

I'm on macOS, where echo "hello" | pbcopy works btw


Here's the function I use, which you can bind to a keybinding. The link to the original author of the function is also provided.

;; http://camdez.com/blog/2013/11/14/emacs-show-buffer-file-name/
(defun cpm/show-and-copy-buffer-filename ()
  "Show the full path to the current file in the minibuffer and copy to clipboard."
  (let ((file-name (buffer-file-name)))
    (if file-name
          (message file-name)
          (kill-new file-name))
      (error "Buffer not visiting a file"))))

| improve this answer | |
  • great answer, only gripe is I feel this would put it in emacs clipboard, but not in OS clipboard. – american-ninja-warrior Oct 8 '19 at 14:52
  • 1
    ah - right. It puts it on to my mac clipboard, but that may be because I use (setq select-enable-clipboard t) – mclear Oct 8 '19 at 14:54
  • I meant to say "wouldn't" but I wrote "would" – american-ninja-warrior Oct 8 '19 at 15:04
  • 1
    With (setq select-enable-clipboard t) the function puts this both on osx clipboard and in the kill ring. I also set (setq save-interprogram-paste-before-kill t), which means that it doesn't replace whatever is currently in the next kill ring slot. So I think, with those settings, the function should do everything you need. Or is the problem that you don't want to share the kill ring with the clipboard? – mclear Oct 8 '19 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.