10

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
14

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. – Name Aug 3 '18 at 11:36
5

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.

1

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

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, privacy policy and cookie policy

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