What is the easiest way to open the folder containing the current file by the default explorer of the OS (e.g. explorer.exe in the case of Windows OS)?
-
1I have a hunch (browse-url-of-file default-directory) would do it, that works on osx with finder, I would think it would work with windows, but can't test it.– Jordon BiondoCommented Jan 27, 2015 at 16:05
-
@JordonBiondo it works! Please convert your comment to an answer.– NameCommented Jan 27, 2015 at 16:07
5 Answers
Using browse-url-of-file
should work when given a directory.
You could implement a command that opens the directory of the current file like this:
(defun browse-file-directory ()
"Open the current file's directory however the OS would."
(interactive)
(if default-directory
(browse-url-of-file (expand-file-name default-directory))
(error "No `default-directory' to open")))
Then M-x browse-file-directory should open up the directory in your OS's file browser.
-
There has been an small incompatibility with emacs 25.* in Windows, but the solution works fine with emacs 26.1 on Windows.– NameCommented Aug 3, 2018 at 11:36
-
Would it be possible to get the file selected, like in VS? See trick 20 in dev.to/devmount/23-lesser-known-vs-code-shortcuts-as-gif-80 Commented Jan 14, 2020 at 8:42
-
@user3341592 Use
(shell-command (concat "start explorer /e,/select,\"" (replace-regexp-in-string "/" "\\\\" (buffer-file-name)) "\""))
. See: davidcapello.com/blog/emacs/…– gslCommented Jul 24, 2020 at 17:55 -
1This opens dired in Emacs 28 instead of the OS's file browser.– scribeCommented Jun 2, 2021 at 18:39
-
1@scribe Just change
browse-url-of-file
tobrowse-url
in the code.– NameCommented Jul 7, 2022 at 16:17
For MS Windows:
Load library w32-browser.el
and use command w32explore
. It does exactly what you are requesting. See MS Shell Execute.
If you also use Dired+ then M-RET
on a file or dir name in Dired opens Windows Explorer for it.
Run shell-command
(M+!) with the default explorer program and the current folder, e.g. for MS Windows, explorer .
(defun open-this-buffer-directory-on-windows()
(interactive)
(shell-command "explorer .")
)
-
Welcome to Emacs.SE! Could you please a) format your code for the site, and b) provide enough commentary to explain what your code is doing.– Dan ♦Commented Apr 22, 2022 at 14:12
-
For some obscure reason, explorer appeared to return 1 even on success. Ended up having to replace
shell-command
withstart-process
, e.g.(start-process "EXPLORER" nil "explorer" ".")
. Commented Mar 11 at 14:25
copy the full path to the clipboard at first:
;; you need install xsel under Linux
;; xclip has some problem when copying under Linux
(defun copy-yank-str (msg &optional clipboard-only)
(unless clipboard-only (kill-new msg))
(cond
;; display-graphic-p need windows 23.3.1
((and (display-graphic-p) x-select-enable-clipboard)
(x-set-selection 'CLIPBOARD msg))
(t (with-temp-buffer
(insert msg)
(shell-command-on-region (point-min) (point-max)
(cond
((eq system-type 'cygwin) "putclip")
((eq system-type 'darwin) "pbcopy")
(t "xsel -ib")))))))
(defun cp-fullpath-of-current-buffer ()
"copy full path into the yank ring and OS clipboard"
(interactive)
(when buffer-file-name
(copy-yank-str (file-truename buffer-file-name))
(message "file full path => clipboard & yank ring")))