Emacs行号显示

来源:互联网 发布:网上电商系统java 编辑:程序博客网 时间:2024/05/16 06:19

Emacs行号显示

环境

  1. 操作系统: Ubuntu 16.04 64-bit, Windows 10 64-bit
  2. Emacs: version 24.5

问题描述

在编写程序时,总是习惯在文件的左侧显示行号以方便查看(估计是有强迫症了)。Emacs中使用linum提供了这样的功能,在GUI下面显示一切正常,但是在终端中显示时,文件内容与行号靠得太近,不容易区分。而Vim无论在GUI下还是终端下都能提供额外的空格区分行号的文件内容。因此,我想在终端下实现类似Vim的显示效果。

解决方案

在进行一番搜索后,发现可以通过设置linum-format格式来实现这样的效果,命令如下:

(setq linum-format "%d ")

备注:format的格式可以通过命令C-h v format查看。

但是,由于Emacs终端和GUI程序共用一个配置文件,因此GUI显示中就会在行号后多出一个空格,个人觉得很不爽。

之前在配置Vim的时候也遇到过终端和GUI需要不同配置的情况,因此就想通过针对终端和GUI来实现不同的配置。Emacs默认提供的是GUI界面的应用程序,但是可以通过命令行参数-nw, --no-window-system让其运行在终端下。我想命令行参数应该是在启动的时候进行解析的,因此就翻看了startup.el文件,发现它提供了一个window-system变量指定显示的框架。在Emacs使用C-h v windows-system查看该变量的定义,如下所示:

window-system is a variable defined in `C source code'.Its value is xIt is a terminal-local variable; global value is the same.Documentation:Name of window system through which the selected frame is displayed.The value is a symbol: nil for a termcap frame (a character-only terminal), 'x' for an Emacs frame that is really an X window, 'w32' for an Emacs frame that is a window on MS-Windows display, 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display, 'pc' for a direct-write MS-DOS frame.Use of this variable as a boolean is deprecated.  Instead,use `display-graphic-p' or any of the other `display-*-p'predicates which report frame's specific UI-related capabilities.

于是我在Emacs的配置文件中加入下面的代码,希望能满足我的要求,

(if (eq (window-system) nil)     (setq linum-format "%d "))

经测试,一切正常,但是美中不足的是终端中对齐采用的是左对齐,我的强迫症又犯了 :( 于是又开始找寻右对齐的方式。发现原来大牛们都觉得linum的速度太慢了,都改换了nlinum,于是我也赶紧换掉,安装了nlinum,然后修改配置为:

(global-nlinum-mode t)(if (eq (window-system nil)    (setq nlinum-format "%d "))

赶紧测试一下,并没感觉速度有多块,估计我是我文件太小了。虽然速度没体验到,但却满足了我右对齐的要求 :),也还不错啦。

参考文献

  1. Line Numbers
  2. emacs显示行号
  3. emacs行号显示的三种方法
  4. emacs 如何默认显示行号?
0 0