Emacs:如何在模式行中添加主机名?

11
我想在我的模式行中显示(getenv“HOSTNAME”)的输出。我的display-time-mode设置为't',因此我已经在模式行中显示时间、负载级别和邮件标志。有没有一种简单的方法将主机名也放在那里?
我想要这个功能是因为我通过ssh连接到3台远程机器,它们都从共同的初始化文件运行emacs,我想知道我正在哪台机器上工作,需要一种快速、简单、不显眼的方式来实现。
3个回答

10

在Sean Bright的回答基础上,你可以按如下方法实现:

(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")))

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

我在我的网站上详细介绍了有关emacs modeline的内容以及其他发现的事情,您可以通过这篇文章进行查看。


2

您还可以将无用信息附加到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 的定义,里面有一个很好的例子。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接