18

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?

24

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.

  • 16
    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." – Constantine Oct 22 '15 at 17:58
  • @Constantine: Good point. I updated the answer to mention this. – Drew Oct 22 '15 at 18:16
  • 5
    @Constantine: you could use "0 w" (it is the same thing as "C-0 w" in a dired buffer) – jfs Aug 2 '17 at 6:08
  • In dired mode use "C-0 w". This copy full path (path + file name) to kill ring. – a_subscriber Sep 22 '18 at 13:37
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, privacy policy and cookie policy

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