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?
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?
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.
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))
)