14

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

2
  • 1
    I 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. Jan 27, 2015 at 16:05
  • @JordonBiondo it works! Please convert your comment to an answer.
    – Name
    Jan 27, 2015 at 16:07

5 Answers 5

16

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.

7
  • There has been an small incompatibility with emacs 25.* in Windows, but the solution works fine with emacs 26.1 on Windows.
    – Name
    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 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/…
    – gsl
    Jul 24, 2020 at 17:55
  • 1
    This opens dired in Emacs 28 instead of the OS's file browser.
    – scribe
    Jun 2, 2021 at 18:39
  • 1
    @scribe Just change browse-url-of-file to browse-url in the code.
    – Name
    Jul 7, 2022 at 16:17
7

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.

0
2

Run shell-command (M+!) with the default explorer program and the current folder, e.g. for MS Windows, explorer .

2
(defun open-this-buffer-directory-on-windows()
  (interactive)
  (shell-command "explorer .")
  )
2
  • 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
    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 with start-process, e.g. (start-process "EXPLORER" nil "explorer" "."). Mar 11 at 14:25
0

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")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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