27

Say we have a Dired buffer open on some directory. How can I send the directory path to the clipboard?

Also, how can I do the same when the Dired cursor is on a specific file, without opening the file first?

2 Answers 2

37

Move the cursor to the directory header line (where the directory is shown - e.g., use M-<), then hit w. That copies the directory name to the kill ring. (w copies any file name from Dired, and it works for the directory header too.)

And if you haven't already done so, customize x-select-enable-clipboard to non-nil, so selection is copied to the clipboard.


@Constantine's comment makes a good point. Instead of moving to the directory header line to get the absolute name, you can just use C-0 w on any file or directory line. That will put the absolute file name in the clipboard. When you paste it you can just remove the relative file-name part, to get the absolute directory name.

7
  • 22
    I think it's worth mentioning that the C-h k w in a dired buffer says "With a zero prefix arg, use the absolute file name of each marked file.", i.e. "press C-0 w to get the absolute path." Oct 22, 2015 at 17:58
  • @Constantine: Good point. I updated the answer to mention this.
    – Drew
    Oct 22, 2015 at 18:16
  • 9
    @Constantine: you could use "0 w" (it is the same thing as "C-0 w" in a dired buffer)
    – jfs
    Aug 2, 2017 at 6:08
  • 1
    In dired mode use "C-0 w". This copy full path (path + file name) to kill ring. Sep 22, 2018 at 13:37
  • How does one search this in `Emacs commands?
    – jjk
    Aug 7, 2021 at 19:05
1

I set x-select-enable-clipbard to t as suggested but it didn't work. I use this piece of code from here: http://blog.binchen.org/posts/copy-file-name-or-full-path-of-file-in-emacs-dired-buffer-into-system-clipboard.html

;; {{ copy the file-name/full-path in dired buffer into clipboard
;; `w` => copy file name
;; `C-u 0 w` => copy full path
(defadvice dired-copy-filename-as-kill (after dired-filename-to-clipboard activate)
  (with-temp-buffer
    (insert (current-kill 0))
    (shell-command-on-region (point-min) (point-max)
                             (cond
                              ((eq system-type 'cygwin) "putclip")
                              ((eq system-type 'darwin) "pbcopy")
                              (t "xsel -ib")
                              )))
  (message "%s => clipboard" (current-kill 0))
  )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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