Skip to content

Instantly share code, notes, and snippets.

@bhyde
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhyde/66acf3c339a80b306d10 to your computer and use it in GitHub Desktop.
Save bhyde/66acf3c339a80b306d10 to your computer and use it in GitHub Desktop.
;;; package --- autoname remote shell buffers
;;; Commentary:
;; Adjustments to comint's shell mode.
;;; Code:
;; This is based on my-shell in http://www.emacswiki.org/emacs/ShellMode
;; M-x shell, make a buffer with a shell prompt in it, and it has
;; some clever knobs to adjust it's behavior. If you give it a
;; prefix arg then you can set the buffer name, overriding the default
;; *shell*. If you run it in a buffer that is visiting a file via
;; tramp it will, sort of, run the shell on the foreign host. Trouble
;; is that it won't do that if you already have a *shell* buffer. I
;; suspect the author does all, or most, of his dev work on a single
;; foreign host and his *shell* buffer is on that host.
;; If you work on lots of hosts then you need to carefully set the
;; buffer names for each one, or you could use the following.
;; This extentds the default buffer name based on the host name to
;; set it based on the host's name by advising M-x shell that when
;; creating a shell buffer on a remote file it should
;; default the buffer name to "*shell: <user>@<host> *". It reusing
;; the an existing buffer if possible.
;; FYI, I think the ssh.el package is probably obsolete at this point.
(require 'tramp)
(defun auto-name-shell-buffers (original-shell-fn &optional buffer)
(unless buffer
(let* ((tramp-path (when (tramp-tramp-file-p default-directory)
(tramp-dissect-file-name default-directory)))
(host (tramp-file-name-real-host tramp-path))
(user (if (tramp-file-name-user tramp-path)
(format "%s@" (tramp-file-name-user tramp-path)) ""))
(new-buffer-name (format "*shell:%s%s*" user host)))
(when host
(setf buffer new-buffer-name))))
(funcall original-shell-fn buffer))
(advice-add 'shell :around #'auto-name-shell-buffers)
(provide 'shell-auto-buffer-naming)
;;; shell-auto-buffer-naming.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment