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.
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")))
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With