Skip to content

Hiding and replacing modeline strings with clean-mode-line

by mickey on September 10th, 2012

If you are like me, you are probably using one or more minor modes, and most of them add their own lighter to the modeline to tell you that they are running. Common examples include “Abbrev” for Abbrev mode; “yas” for Yasnippet mode and so on. Unfortunately, modeline real estate is expensive, and if you are stuck on a small screen — or if you split your Emacs windows a lot — you will find that it is often truncated, possibly hiding useful information that you actually care about, such as Which Function Mode.

There is no feature in Emacs to selectively hide or compress modeline flotsam, so I have hacked together some code to do the job for you.

For instance, in my Emacs I can collapse the following modeline string (Python Hi AC yas Eldoc Flymake:0/12 Abbrev) into (Py α υ Φ:0/12). I’ve chosen to completely obscure hi-lock-mode, abbrev-mode, and eldoc-mode but “compress” auto-complete-mode, yas/minor-mode, python-mode and flymake-mode.

I used Greek characters (How? C-u C-\ greek RET then C-\ to write Greek characters; see Diacritics in Emacs) because they look cool.

The code works rather well, and I have yet to experience any major issues with it, but do let me know if it breaks something! One important caveat is that a lot of minor modes that reach out and call other functions, like ERC or Flymake, often use functions in lieu of strings in mode-line-format and mode-line-modes, so replacing them with a string might break the minor mode, or cause it to not update properly! Please keep that in mind and experiment in a separate Emacs instance unless you know what you’re doing. Case in point: Flymake. I’ve replaced its own modeline updater function with one of my own with the “Flymake” string replaced; you could probably rejiggle it by advising the original function, then tweaking the modeline string post facto, blah, blah, blah — but I always opt for the simpler solution, which is what I’ve used below!

Here’s the code. Stick it in your emacs file, evaluate it, and you’re done. You can also force a refresh by running M-x clean-mode-line. Flymake will have to be turned off and back on for the changes to take effect in a buffer!

9:17pm update: Fixed a small bug in clean-mode-line !

  1. e20100633 permalink

    Hello, thanks for sharing this. This is very usefull.

    I think I’ve found a bug. I’ve added (auto-fill-mode . “a”) under (paredit-mode . ” π”) — ‘a’ is just for testing purpose. After eval-buffer, while I M-x I’ve got a nice “Wrong type argument: consp, nil”. I’ve tried other modes (minor and major), without much problem. Is that just me ?

    Thank you very much.

    • mickey permalink

      Two things:

      1) there’s a subtle bug in my code, which I have now fixed;
      2) the actual name used is not auto-fill-mode but auto-fill-function. If you do C-h v minor-mode-alist you should see it near the bottom with the string ” Fill”.

      Good job finding the bug.

  2. Chris permalink

    There is also diminish.el which I’ve used for a while.

  3. Yep, +1 for diminish.el, which does the same thing, or at least shares your goal.

  4. I was also using diminish. Now I’m trying Powerline

  5. drolive permalink

    Is it possible to write a Lisp function that would show the full (unmodified) modeline in the minibufer (using “message”)? That may be useful to hide most modes and to get the full modeline on demand only…

  6. Mike Mazurek permalink

    One way I reduce mode line clutter is to move date time and load information from
    the mode line to the frame title. Some code that achieves this effect is:

    (setq display-time-day-and-date t)
    (setq global-mode-string nil)

    (defun frame-title-prefix()
    (cond (multiple-frames (buffer-name))
    (t (concat invocation-name “@” system-name))))

    (setq frame-title-format
    ‘(“” (:eval (frame-title-prefix)) ” ” display-time-string))

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS