Is there an easy way to give the time (as displayed by M-x display-time-mode) a separate face in the modeline?

I don't need anything too fancy like powerline, I'd just like to be able to have the time in its own colour.



Display Time Mode constructs a dynamic mode line based on display-time-string-forms, which is a customizable option. The value of this option is a list of Emacs Lisp expressions. Display Time Mode constructs a (:eval …) mode line data from these expressions.

You can do whatever you want with these expressions, as long as they return strings. Specifically, you can use the built-in propertize function to change the face of the string. For instance, the following construct will show the current time in 24hr format in bold face:

(setq display-time-string-forms
      '((propertize (format-time-string "%H:%M" now) 'face 'bold)))

Of course you can use whatever face you like instead. Take a look at the default value of this variable in time.el to get an idea of what is possible. You can visit the definition with M-x find-variable RET display-time-string-forms.

Note that if you customize display-time-string-forms this way, several other customization options of Display Time Mode (e.g. display-time-format, display-time-24hr-format, etc.) and mail display cease to work. It doesn't matter since you are defining your very own display style, but you should be aware of.

PS: While writing this answer I realized that time.el is a bloody piece of 💩 implementation-wise. Notably, it defines a dozen of unprefixed dynamic variables with names as innocent as now. If you enable Display Time Mode, you'd better make sure that you don't use any package which binds now as a closure variable.

In other words, be prepared for strange issues with other packages if you enable Display Time Mode.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.