open a windows explorer at the path of the current buffer

I use emacs in Windows. As I edit some text based files in Emacs, sometimes I need to open a window at the path of the current buffer to visit other files. It would be nice if I can just press a key in Emacs, and have this window open. Here is my simple solution for this task in Emacs with Windows OS.

(defun open-buffer-path ()
“Run explorer on the directory of the current buffer.”
(shell-command (concat “explorer ” (replace-regexp-in-string “/” “\\\\” (file-name-directory (buffer-file-name)) t t))))

I bound this function to a key-binding:
(global-set-key [M-f9] ‘open-buffer-path)

Notes: the key points in the line
(shell-command (concat “explorer ” (replace-regexp-in-string “/” “\\\\” (file-name-directory (buffer-file-name)) t t)))
(1) shell-command call a command specified by a string from shell
(2) replace-regexp-in-string “/” “\\\\” replace the Unix-style path (using /) to Windows-style paht (using \), the optional arguments “t t” let replace-regexp-in-string replace literally.


24 thoughts on “open a windows explorer at the path of the current buffer

      • You can drag a file in a dired-displayed dir, that’s equivalent to copy.
        But you cannot drag a file out of diired to another windows application.

  1. Hoping that formating doesn’t get tangled…

    (defun dired-w32-open-files (&optional arg files)
    “*Open FILES using the applications registered to handle them.
    Return an error message if there is no application registered to handle the files.
    This command simply calls `w32-shell-execute’ and only works on Window systems.”
    (interactive (list current-prefix-arg (dired-get-marked-files nil current-prefix-arg)))
    (unless (fboundp ‘w32-shell-execute)
    (error “This command is not available on this system.”))
    (mapc (lambda (fn)
    (w32-shell-execute “Open” (subst-char-in-string ?/ ?\\ (expand-file-name fn))))

  2. I also use emacs on windows and have problem with deleting files with long paths (> 255 characters), bash inside cygwin also cannot delete them, but explorer can. Do you know how to delete dir using explorer from command line?

    • did you try temporarily change the default command interface to cmd.exe?
      Like some functions to change explicit-shell-file-name from bash.exe to cmd.exe temporarily?

  3. Nice; how about just using default-directory:

    (defun open-buffer-path ()
    “Run explorer on the directory of the current buffer.”
    (shell-command (concat “explorer ”
    (replace-regexp-in-string “/” “\\\\”
    (expand-file-name default-directory)))))

  4. I have a very similar function in my own config; however I’m using an argument which allows the user to specify the directory to open:
    (defun explore (directory)
    “Open the given directory in Windows Explorer.”
    (interactive “DDirectory:”)
    (concat “explorer ”
    (replace-regexp-in-string “/” “\\\\” directory) ” &”)))

    Since I use ido everywhere, and ido is awesome, I now have an awesome way to open the current directory, a nearby directory, or any directory I want. Also note that I concatenate ” &” to the end of the command passed to shell-command. This allows the explorer process to fork, so I can continue to work in emacs while I have the new explorer window open.

  5. Nice elisping! There are certainly times in my workflow, where dired won’t cut it and I have to pop open explorer to get something done directly in Windows Explorer.

    Here’s how I do it currently:

    M-! explorer . RET

  6. Pingback: Beginning LISP Programming in Ubuntu | Common Lisp WebDev Insider

  7. Pingback: uberVU - social comments

  8. Pingback: Emacs: apri il file manager nella cartelle del buffer corrente | TuxFeed

  9. Pingback: dieta dunkan

  10. (defun browse-current-folder ()
    “open the current buffer’s folder”
    (browse-url (concat “file://” (file-name-directory (buffer-file-name)))))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s