[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Hack to get nicer buffer names for shell buffers that use tramp.

From: Ben Hyde
Subject: Hack to get nicer buffer names for shell buffers that use tramp.
Date: Fri, 19 Apr 2019 16:46:41 -0400

Friday afternoon. Thought I’d share this which I’ve been using for a year or two.

Imagine your viewing a remote file on, via tramp of course.

If you do M-x shell you get a new buffer with an interactive shell.

The code below will bless this new shell buffer with a nicer name,
e.g. instead of say shell.

      (defun auto-name-shell-buffers (original-shell-fn &optional buffer)
        "Wrap ORIGINAL-SHELL-FN to avoid tramp shell buffers name *shell*, do nothing if BUFFER."
         ((or buffer
              (not (tramp-tramp-file-p default-directory)))
          (funcall original-shell-fn buffer))
         ((not (zerop (call-process "/usr/bin/ssh-add" nil nil nil "-l")))
          (message "ssh auth socket is bork'd"))
          (with-parsed-tramp-file-name default-directory tp
            (let* ( ; (tramp-path (tramp-dissect-file-name default-directory))
                   (host (tramp-file-name-host tp))
                   (user (if (tramp-file-name-user tp)
                             (format "%s@" (tramp-file-name-user tp)) ""))
                   (new-buffer-name (format "*shell:%s%s*" user host)))
              (funcall original-shell-fn new-buffer-name))))))

      (advice-add 'shell :around #'auto-name-shell-buffers)

No doubt other crafts people would write this in far more elegant ways.
Feel free to provide feedback.

For example I don’t recall how I determined that (tramp-tramp-file-p directory)
was a fine way to tell if the current buffer is leveraging tramp.

ps. The cond clause that checks for a valid ssh agent can be removed.
I always have an agent, so I like it.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]