Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: add hostname to mode line?

Tags:

emacs

I'd like to display the output of (getenv "HOSTNAME") somewhere in my mode line. My display-time-mode is set to 't', so I'm already displaying time, load level and a mail flag in the mode line. Is there an easy way to get the hostname in there, too?

I'd like to have this because I'm ssh'd into 3 remote machines, all running emacs from a common set of init files, and I'd like some fast easy unobtrusive way to know which machine I'm working on.

like image 250
Bill White Avatar asked Apr 22 '09 18:04

Bill White


3 Answers

To build on Sean Bright's answer, specifically you can do this:

(let ((pos (memq 'mode-line-modes mode-line-format)))
  (setcdr pos (cons (getenv "HOSTNAME") (cdr pos))))

This assumes that 'mode-line-modes is a part of your 'mode-line-format, which it is by default. Because you're modifying the list pointed to by the variable 'mode-line-format, you don't have to set the default value. If you were setting the variable itself, you'd have to do something like:

(setq-default mode-line-format (build-list-that-contains-(getenv "HOSTNAME")))
like image 187
Trey Jackson Avatar answered Nov 14 '22 08:11

Trey Jackson


I tried the above answers and was not particularly successful (I'm running emacs 23). After much investigation, I ended up just putting system-name into my mode-line-format as follows:

;; 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.
        "-%-"
        )
)

I've detailed this and other things I discovered about the emacs modeline in a posting on my website.

like image 7
Phil Hollenback Avatar answered Nov 14 '22 10:11

Phil Hollenback


You can also append junk to the global-mode-string variable:

(defvar my-hostname (concat " " (system-name)))
(setq global-mode-string (append global-mode-string '(my-hostname)))

Those two lines are probably sufficient for something static like your hostname.

If you have something more dynamic you can set up a timer with run-at-time to update the string (my-hostname in this example). Take a look at the definition of display-time-mode for a nice little example.

like image 2
mgalgs Avatar answered Nov 14 '22 09:11

mgalgs