Dynamic title for Treemacs workspace
emacs emacs-lisp ~1 minutes read

Thought that I can share snippets of my Emacs config from time to time here, just like @clemera does on with-emacs.com. I highly recommend you to check it out, there are many great recipes and articles.

A while ago I’ve added static Treemacs title to Treemacs buffer for aesthetic purposes - it adds good alignment with tabs in other window. But recently I’ve thought that it could also be useful, and indicate workspace that I’m currently in. I have many workspaces, so this is good info to have. Here’s my little piece of code that creates dynamic title for package:

(defun aorst/treemacs-setup-title ()
  (let ((bg (face-attribute 'default :background))
        (fg (face-attribute 'default :foreground)))
    (face-remap-add-relative 'header-line
                             :background bg :foreground fg
                             :box `(:line-width ,(/ (line-pixel-height) 2) :color ,bg)))
  (setq header-line-format
        '((:eval
           (let* ((text (treemacs-workspace->name (treemacs-current-workspace)))
                  (extra-align (+ (/ (length text) 2) 1))
                  (width (- (/ (window-width) 2) extra-align)))
             (concat (make-string width ?\s) text))))))

Then we need to hook this function to treemacs-mode-hook, so it is called when Treemacs sets it’s mode in the buffer.

What this function essentially does is, it creates header-line for Treemacs buffer, adds nice box for it with the width half the size of line height, and set’s it’s background and foreground based on your theme. After that it gets Treemacs workspace name, calculates its length, and needed amount of spaces in order to center text in the window. The ending result is like this:

Or with Emacs default theme:

Full Treemacs configuration can be found in my .dotfiles.