如何为Emacs文件设置语法高亮显示

时间:2021-10-05 23:13:06

My .emacs is like a roadmap for me where I source many files. Their extension is .emacs: for instance,

我的.emacs就像是我的路线图,我在那里获取了很多文件。他们的扩展名是.emacs:例如,

 fileName.emacs

The problem is that only ~/.emacs has syntax highlighting.

问题是只有〜/ .emacs有语法高亮。

I would like to have the syntax highlighting for all sourced files which end with .emacs.

我想为所有以.emacs结尾的源文件突出显示语法。

How can you put syntax highlighting on to all sourced .emacs -files?

如何将语法高亮显示在所有源.emacs -files上?

2 个解决方案

#1


Yes. I assume these are lisp files, so you need Emacs to automatically be in lisp-mode when viewing these files. There's two solutions:

是。我假设这些是lisp文件,因此在查看这些文件时,您需要Emacs自动处于lisp模式。有两种解决方案:

  1. The simplest thing is to change the extension to .el. By default, those are opened in lisp-mode.

    最简单的方法是将扩展名更改为.el。默认情况下,这些是以lisp模式打开的。

  2. If, for some reason you really want to use the .emacs extension, you'll want to add this somewhere in your ~/.emacs file:

    如果由于某种原因你真的想使用.emacs扩展名,你会想要在〜/ .emacs文件中的某处添加:

    (setq auto-mode-alist 
          (append '((".*\\.emacs\\'" . lisp-mode))
                  auto-mode-alist))
    

auto-mode-alist is the list Emacs uses to determine the major mode to use. Each item is a list, the first is the Emacs regular expression Emacs uses to test the filename against, and if true, it uses the mode given in the third item.

auto-mode-alist是Emacs用于确定要使用的主要模式的列表。每个项目都是一个列表,第一个是Emacs用于测试文件名的正则表达式,如果为true,则使用第三个项目中给出的模式。

(I don't know what the second item is, I've never used it.)

(我不知道第二项是什么,我从来没用过它。)

I strongly suggest option 1 though.

我强烈建议选项1。

#2


You can set the mode in the first non-blank line of the file:

您可以在文件的第一个非空行中设置模式:

;-*-Lisp-*-

This is a comment for Lisp, but causes Emacs to switch to Lisp mode upon reading it into the buffer (reference).

这是对Lisp的注释,但是当Emacs将其读入缓冲区(引用)时会导致切换到Lisp模式。

#1


Yes. I assume these are lisp files, so you need Emacs to automatically be in lisp-mode when viewing these files. There's two solutions:

是。我假设这些是lisp文件,因此在查看这些文件时,您需要Emacs自动处于lisp模式。有两种解决方案:

  1. The simplest thing is to change the extension to .el. By default, those are opened in lisp-mode.

    最简单的方法是将扩展名更改为.el。默认情况下,这些是以lisp模式打开的。

  2. If, for some reason you really want to use the .emacs extension, you'll want to add this somewhere in your ~/.emacs file:

    如果由于某种原因你真的想使用.emacs扩展名,你会想要在〜/ .emacs文件中的某处添加:

    (setq auto-mode-alist 
          (append '((".*\\.emacs\\'" . lisp-mode))
                  auto-mode-alist))
    

auto-mode-alist is the list Emacs uses to determine the major mode to use. Each item is a list, the first is the Emacs regular expression Emacs uses to test the filename against, and if true, it uses the mode given in the third item.

auto-mode-alist是Emacs用于确定要使用的主要模式的列表。每个项目都是一个列表,第一个是Emacs用于测试文件名的正则表达式,如果为true,则使用第三个项目中给出的模式。

(I don't know what the second item is, I've never used it.)

(我不知道第二项是什么,我从来没用过它。)

I strongly suggest option 1 though.

我强烈建议选项1。

#2


You can set the mode in the first non-blank line of the file:

您可以在文件的第一个非空行中设置模式:

;-*-Lisp-*-

This is a comment for Lisp, but causes Emacs to switch to Lisp mode upon reading it into the buffer (reference).

这是对Lisp的注释,但是当Emacs将其读入缓冲区(引用)时会导致切换到Lisp模式。