26

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?

Amelio Vazquez-Reina
  • 5,157
  • 4
  • 32
  • 47

2 Answers2

36

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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." – Constantine Oct 22 '15 at 17:58
  • @Constantine: Good point. I updated the answer to mention this. – Drew Oct 22 '15 at 18:16
  • 8
    @Constantine: you could use "0 w" (it is the same thing as "C-0 w" in a dired buffer) – jfs Aug 02 '17 at 06:08
  • 1
    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
  • How does one search this in `Emacs commands? – jjk Aug 07 '21 at 19:05
  • @jjk: I don't understand what you're asking. What is "this" that you want to search, and what does "in `Emacs commands" mean? – Drew Aug 08 '21 at 00:38
  • @Drew Nevermind, fuzzyfinding `dired-do-copy-filename` and reading what it does answered my question. I was looking for some kind of mnemonic for `C 0 w` in helm `Emacs commands`. – jjk Aug 08 '21 at 20:01
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))
  )
chriad
  • 386
  • 2
  • 8