Open a window in Microsoft Explorer from an emacs buffer
Facts - Editor: emacs
Saturday, 26 July 2008 18:05

Here is a tip for using emacs in a Microsoft Windows environment.

Sometimes we want to quickly browse the folder of a file when editing it in Emacs. The simplest way for quickly browsing a folder when editing a file in emacs is just pressing "ctrl-x ctrl-f return". This switches to a buffer listing all files in a directory in "Dired" mode. But this does not help us when we want to open a file that can not be edited or displayed in emacs such as a pdf. So in order to browse the directory of a file, add the following lines to your .emacs file:

; Windows explorer to go to the file in the current buffer
(defun open-folder-in-explorer ()  
  "Call when editing a file in a buffer. Open windows explorer in the current directory and select the current file"  
    "open" "explorer"  
    (concat "/e,/select," (convert-standard-filename buffer-file-name))

When executing Meta-x open-folder-in-explorer an explorer window will open displaying the directory of the edited file with the file selected.

If you get an error message indicating that the path passed by emacs contains forward slashes instead of backslashes you can try to add the following to your .emacs file:

(defun w32-shell-dos-semantics() t)

This overrides the function that decides whether paths should contain forward slashes or backward slashes.

Note that this open-in-explorer emacs function only works when called from a buffer editing a file. It does not work when in Dired mode or in Shell mode. From shell mode you can first open the directory in Dired mode, and then open a file in the directory and then execute the new function.

Also note that you can directly open an HTML file in a browser from an emacs buffer in HTML mode by pressing CTRL-c CTRL-v. This invokes the emacs command browse-url-of-buffer.