16.11 一个简单扩展:line-to-top-of-window

这里是一个 Emacs 简单扩展,它会将光标所在行移动到 框架 顶部。我经常使用该功能,让文本更易于阅读。

你可以将下面的代码放入独立文件,再从 .emacs 文件加载,也可以直接写在 .emacs 文件内部。

定义如下:

;;; 将行移至 框架 顶部;
;;; 替代三次按键序列  C-u 0 C-l
(defun line-to-top-of-window ()
  "将光标所在行移至 框架 顶部。"
  (interactive)
  (recenter 0))

接下来设置按键绑定。

功能键、鼠标按键事件以及非ASCII字符都写在方括号内,不加引号。

我将 line-to-top-of-window 绑定到 F6 功能键,写法如下:

(keymap-global-set "<f6>" 'line-to-top-of-window)

更多信息请参考 Rebinding Keys in Your Init File in The GNU Emacs Manual

如果你同时运行两个版本的 GNU Emacs(例如 27 版和 28 版),并且共用一个 .emacs 文件,可以使用下面的条件语句选择要执行的代码:

(cond
 ((= 27 emacs-major-version)
  ;; evaluate version 27 code
  ( ... ))
 ((= 28 emacs-major-version)
  ;; evaluate version 28 code
  ( ... )))

例如,新版本 Emacs 默认会让光标闪烁。我很讨厌光标闪烁以及其他一些特性,因此在 .emacs 文件中加入了以下配置22:

(when (>= emacs-major-version 21)
  (blink-cursor-mode 0)
  ;; 在缓冲区末尾按 'C-n' (next-line) 时自动插入换行
  (setq next-line-add-newlines t)
  ;; Turn on image viewing
  (auto-image-file-mode t)
  ;; Turn on menu bar (this bar has text)
  ;; (Use numeric argument to turn on)
  (menu-bar-mode 1)
  ;; Turn off tool bar (this bar has icons)
  ;; (Use numeric argument to turn on)
  (tool-bar-mode nil)
  ;; Turn off tooltip mode for tool bar
  ;; (This mode causes icon explanations to pop up)
  ;; (Use numeric argument to turn on)
  (tooltip-mode nil)
  ;; 若开启提示,让提示快速出现
  (setq tooltip-delay 0.1)  ; 默认 0.7 秒
   )

Footnotes

(22)

启动不加载 .emacs 和站点配置的 Emacs 实例时,我也会关闭闪烁:

emacs -q --no-site-file -eval '(blink-cursor-mode nil)'

如今还可以使用更完善的选项组合: