Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs dired-mode: How to "exit" and end up in a shell in the current directory?

Tags:

emacs

dired

On Mac OS I can use the terminal, write "cd ", and then drag & drop folders from Finder to the terminal. I then obtain something like "cd /Users/..." which allows me to quickly change to the corresponding directory. If I open an emacs shell with M-x shell and drag & drop a folder to it, emacs changes in dired mode and displays me the content of the folder I dropped. How can I "exit" or "quit" dired-mode and obtain a shell having directory changed to the folder I dropped? That would give me something like above and that would be quite useful.

like image 669
Marius Hofert Avatar asked Nov 08 '11 15:11

Marius Hofert


People also ask

What is dired mode in Emacs?

Dired makes an Emacs buffer containing a listing of a directory, and optionally some of its subdirectories as well. You can use the normal Emacs commands to move around in this buffer, and special Dired commands to operate on the listed files. Dired works with both local and remote directories.

How do I move files in dired?

Open dired for a directory containing files you want to work with. Then use C , R , or D when the cursor is on the line of a file to copy, rename/move or delete the file, respectively. This can also be done for multiple files by marking them.


1 Answers

You can implement a function to open a shell instead of dired buffer. This function is useful in many other cases, not only in a case of DnD

(require 'dired)
(define-key dired-mode-map "c" 'shell-instead-dired)

(defun shell-instead-dired ()
  (interactive)
  (let ((dired-buffer (current-buffer)))
    (shell (concat default-directory "-shell"))
    (kill-buffer dired-buffer) ;; remove this line if you don't want to kill the dired buffer
    (delete-other-windows)))

EDIT In this case you need to DnD a directory in Emacs and press 'c' to call a shell in this directory.

Otherwise you may install a smart-dnd package and configure it to open a shell. I provides also other useful stuff like creating <img ...> tags in html mode if you drop a jpg or #include<...> in c-mode if you drop a header.

like image 137
Oleg Pavliv Avatar answered Sep 19 '22 20:09

Oleg Pavliv