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.”
(interactive)
(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.
Why don’t you just use dired? C-x d
Can you drag and drop from dired? That’s why I usually use Explorer; I have to use a Windows applications that *only* recognizes drag & drop.
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.
Why leave emacs? Just use “M-x dired’ or ‘C-x d’
Because sometimes I feel more familiar with explorer, and would like to start other applications from the explorer.
in dired you can open applications on files by executing them. under windows it’ll open default associated app.
Can you suggest the function? w32-shell-execute?
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))))
files))
nice, thanks!
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?
Nice; how about just using default-directory:
(defun open-buffer-path ()
“Run explorer on the directory of the current buffer.”
(interactive)
(shell-command (concat “explorer ”
(replace-regexp-in-string “/” “\\\\”
(expand-file-name default-directory)))))
good idea!:D
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:”)
(shell-command
(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.
nice note. I like the idea of ” &”, really.
Mac version: to open a Finder window in the dir of current buffer: M-! “open .” RET
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
Thanks! This is useful when you want to open the parent folders:
M-! explorer ..\\.. RET
Pingback: Beginning LISP Programming in Ubuntu | Common Lisp WebDev Insider
Pingback: uberVU - social comments
#vim = :execute ‘!start’ expand(‘%:p:h’)
just sayin’ guys
Pingback: Emacs: apri il file manager nella cartelle del buffer corrente | TuxFeed
Pingback: dieta dunkan