Some file managers provide a shortcut to quickly embed some terminal that’s initialized with the current directory as cwd. Wouldn’t that be cool to have in emacs?
For example, you edit some file, and then you want to quickly commit it. Using the code below, you can do that like this:
$ svn diff | colordiff
$ svn ci -m "some changes" RET
Here’s the code:
(defvar th-shell-popup-buffer nil) (defun th-shell-popup () "Toggle a shell popup buffer with the current file's directory as cwd." (interactive) (unless (buffer-live-p th-shell-popup-buffer) (save-window-excursion (shell "*Popup Shell*")) (setq th-shell-popup-buffer (get-buffer "*Popup Shell*"))) (let ((win (get-buffer-window th-shell-popup-buffer)) (dir (file-name-directory (or (buffer-file-name) ;; dired dired-directory ;; use HOME "~/")))) (if win (quit-window nil win) (pop-to-buffer th-shell-popup-buffer nil t) (comint-send-string nil (concat "cd " dir "\n"))))) (global-set-key (kbd "<f12>") 'th-shell-popup)
Using that, hitting F12 will popup a
*Popup Shell* buffer, initialized in the directory containing the file your are currently editing. If the current buffer is not associated with a file, then the shell’s cwd is your HOME directory.
Hitting F12 while the popup shell buffer is visible will hide it again. So you can use it as a simple toggle.
The code ensures that the same shell buffer is reused over and over again (unless you kill it), so that you don’t end up with hundredth of them.
And here’s a screenshot:
- 2011-10-13 08:03: Made it work for dired buffers.
- 2012-02-24 14:41: Use quit-window instead of delete-window.