2009-04-22 71 views
11

我想在我的模式行中显示(getenv“HOSTNAME”)的输出。我的显示时间模式设置为't',所以我已经在模式行中显示时间,负载水平和邮件标志。有没有简单的方法来获取主机名呢?Emacs:添加主机名到模式行?

我想这样做是因为我将ssh移入了3个远程机器,所有这些机器都是从一组常用的init文件中运行emacs,并且我想要一些快速简单的不显眼的方法来知道哪台机器“正在努力。

回答

7

我尝试了上面的回答,并没有特别成功(我运行的Emacs 23)。经过大量调查后,我最终只是把system-name到我mode-line-format如下:

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

我已经详细介绍这和其他的东西,我发现有关在posting on my website Emacs的模式行。

9

要建立在肖恩明亮的answer,具体情况,你可以这样做:

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

这是假设'mode-line-modes是你'mode-line-format的一部分,它是默认。由于您正在修改变量'mode-line-format指向的列表,因此不必设置默认值。如果你设置变量本身,你必须做一些事情,如:

(setq-default mode-line-format (build-list-that-contains-(getenv "HOSTNAME"))) 
1

您还可以追加垃圾到global-mode-string变量:

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

这两行很可能像你的主机名静态的东西足够。

如果你有更多的动态,你可以设置一个定时器run-at-time来更新字符串(本例中为my-hostname)。看看display-time-mode的定义,这是一个很好的例子。