Andrey Listopadov

Dynamic title for Treemacs workspace

Thought that I can share snippets of my Emacs config from time to time here, just like @clemera does on 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 windows. But recently I’ve thought that it could also be useful, and indicate the 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 a dynamic title for the 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
           (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 its 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 its background and foreground based on your theme. After that, it gets the Treemacs workspace name, calculates its length, and the 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.