This page has been locked and cannot be edited.
!!! The Emacs Modeline The _modeline_ is the descriptive line at the bottom of your emacs window (right above the minibuffer, to be precise). It provides the basic information like what file you are working on, what mode you are in and so on. I decided to modify my modeline to solve a particular problem: I needed to know what host I was running on. The reason I need to do this is I now run [gnu screen | http://www.gnu.org/software/screen/] on my local laptop, and inside one of the screen windows, I run another remote screen on my work computer. Since I am an inveterate emacs user, I want to run it in both the local and remote screen session. Yes, I know about tramp-mode to access files remotely in emacs, but I don't want to deal with that. I just needed a way to quickly identify whether I was in the local or remote emacs, since they look pretty much identical on the console in the default configuration. The solution as I mentioned was to modify my modeline on both systems. I would put the hostname in the modeline on each system, and change the modeline colors for quick identification. This [stack overflow question|http://stackoverflow.com/questions/778508/emacs-add-hostname-to-mode-line] seemed to have the answer, but I had zero luck getting that solution to work. I then started reading the [modeline docs|http://www.gnu.org/software/emacs/elisp/html_node/Mode-Line-Top.html] and [things got real|ShitJustGotReal]. A few hours later I realized that messing with your modeline is a fools errand. Luckily for you, I did the work and wrote it down here so you don't have to waste any time. Basically, the modeline configuration is controlled by the variable <code>mode_line_format</code> (except for the color, which I will get to in a bit). In older versions of emacs, you put various <code>%X</code> escape sequences in a list in this variable. Newer emacsen still support this syntax, but it's use is discouraged. Instead you can insert various [modeline variables|http://www.gnu.org/s/emacs/manual/html_node/elisp/Mode-Line-Variables.html]. One of these (<code>system-name</code>) contains the hostname and that ended up being just what I needed. Here's what you need to add to your .emacs to get my modeline. Note the use of <code>setq-default</code> instead of just setq. This is necessary to make the change global - if you just use setq, the variable becomes mode-specific. <verbatim> ;; Set the modeline to tell me the filename, hostname, etc.. (setq-default mode-line-format (list " " ; */% indicators if the file has been modified 'mode-line-modified "--" ; the name of the buffer (i.e. filename) ; note this gets automatically highlighted 'mode-line-buffer-identification "--" ; major and minor modes in effect 'mode-line-modes ; if which-func-mode is in effect, display which ; function we are currently in. '(which-func-mode ("" which-func-format "--")) ; line, column, file % 'mode-line-position "--" ; if vc-mode is in effect, display version control ; info here `(vc-mode vc-mode) "--" ; hostname 'system-name ; dashes sufficient to fill rest of modeline. "-%-" ) ) </verbatim> This results in a modeline like this: <verbatim> **--.emacs --[(Emacs-Lisp)]--[prefix-region]--28% (201,23) --ourtownadd-lm---------------------- </verbatim> which is the info I need. Now, on to the color. You set the modeline color with <code>set-face-foreground</code> and <code>set-face-background</code>. Bizarrely, you must have <code>mode-line-inverse-video</code> set, otherwise modeline colors are ignored. This inverts the color meaning, so setting foreground actually sets the modeline background. Here's how I dealt with that: <verbatim> ;; set the modeline to white on red ; you HAVE to set mode-line-inverse-video to t, otherwise the face ; gets ignored. Retarded. That means you have to reverse the colors - ; setting foreground color actually sets background. ; see this gem: http://www.delorie.com/gnu/docs/emacs/emacs_89.html (cond ((not window-system) (setq mode-line-inverse-video t) (set-face-foreground 'modeline "red") (set-face-background 'modeline "white") )) </verbatim> Note I wrap this in a check to see if I'm running in a gui, because white text on red will probably look horrible in that case. If I do run a gui emacs, I let the system use it's default colors. And there you go - hopefully this is the definitive reference on how to modify your emacs modeline. Let me know if it works for you. CategoryGeekStuff CategoryEmacs CategoryBlog
comments powered by Disqus.
What links here
Recent Changes Cached
No changes found