One of the thing I often need is a shell in the same directory as a file I’m editing. Emacs doesn’t have a quick way to do this, so I hacked one together.
(defun shell-here ()
"Open a shell in `default-directory'."
(interactive)
(let ((dir (expand-file-name default-directory))
(buf (or (get-buffer "*shell*") (shell))))
(goto-char (point-max))
(if (not (string= (buffer-name) "*shell*"))
(switch-to-buffer-other-window buf))
(message list-buffers-directory)
(if (not (string= (expand-file-name list-buffers-directory) dir))
(progn (comint-send-string (get-buffer-process buf)
(concat "cd \"" dir "\"\r"))
(setq list-buffers-directory dir)))))
I have this bound to C-c !, which is an open global binding. It’s working well for me, though there are some things I’d like to see improved, like the buffer-name comparison used to switch to the shell buffer. It would also be nice if you could specify the directory if you call it with a prefix.
Discussion