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

  • 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. – Jordon Biondo Jan 27 '15 at 16:05
  • @JordonBiondo it works! Please convert your comment to an answer. – Name Jan 27 '15 at 16:07

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."
  (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.

|improve this answer|||||

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.

|improve this answer|||||

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

|improve this answer|||||

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))
   ;; 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)
                                  ((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"
  (when buffer-file-name
    (copy-yank-str (file-truename buffer-file-name))
    (message "file full path => clipboard & yank ring")))
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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