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?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityMove 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.
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
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))
)