Dynamic eshell buffer names
Dec. 1st, 2005 05:55 pmBeing a bit vague, I like having my current working directory
displayed in the title bar of my terminal window. This isn't normally
a problem, because my usual terminal emulators (putty, screen,
xterm) respect the magic escape sequences in my bash prompt and set
their titles correctly, but at the moment I've only got access to emacs
eshell on Win XP, so my bash stuff is no help whatsoever.
So, how to solve the problem? Maybe by writing a lisp function that changes the name of the of the buffer to include the working directory every time a new prompt is displayed:
; Set a counter to ensure buffer name uniqueness
(defvar eshell-buffer-count 0)
(defun eshell-exotic-prompt-function ()
"Set the buffer title as well as the prompt."
(let* ((pwd (eshell/pwd))
(host (system-name)))
; Set the name of the buffer using the username, the hostname,
; the eshell-buffer-count and current working directory
(if (equal mode-name "EShell")
(rename-buffer
(format
"%s@%s (%s) : %s"
(user-login-name) host eshell-buffer-count pwd)))
; Set the prompt string
(concat
host
(if (= 0 (user-uid))
"# "
"> "))))
; Set up the prompt and set the regexp to match the
; prompt string
(setq eshell-prompt-function 'eshell-exotic-prompt-function
eshell-prompt-regexp "^.*[>|#] ")
; Increase the buffer count when a new eshell is created
(add-hook 'eshell-mode-hook
'(lambda ()
(setq eshell-buffer-count
(1+ eshell-buffer-count))))
; Decrease the buffer count when an eshell is destroyed
(add-hook 'eshell-exit-hook
'(lambda ()
(setq eshell-buffer-count
(1- eshell-buffer-count))))
Not particularly efficient, but it'll do as a quick hack to work around my current irritation...