Search code examples
emacsdired

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


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.


Solution

  • 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.