Emacs默认语法高亮

默认安装的 Vim 有对多种语言的关键字高亮,比如 golang, c++, javascript等,不用自己去下载插件。默认安装的 Emacs 好像不行。我想知道能不能只使用 Emacs 的默认功能就实现对不同语言的源代码的高亮?

Emacs has programming language modes for Lisp, Scheme, the Scheme-based DSSSL expression language, Ada, ASM, AWK, C, C++, Fortran, Icon, IDL (CORBA), IDLWAVE, Java, Javascript, M4, Makefiles, Metafont (TeX’s companion for font creation), Modula2, Object Pascal, Objective-C, Octave, Pascal, Perl, Pike, PostScript, Prolog, Python, Ruby, Simula, SQL, Tcl, Verilog, and VHDL. An alternative mode for Perl is called CPerl mode. Modes are also available for the scripting languages of the common GNU and Unix shells, and MS-DOS/MS-Windows ‘BAT’ files, and for makefiles, DNS master files, and various sorts of configuration files.

Ideally, Emacs should have a major mode for each programming language that you might want to edit. If it doesn’t have a mode for your favorite language, the mode might be implemented in a package not distributed with Emacs (see Packages); or you can contribute one.

自带了很多,但不是默认开启,你直接装个 Doom emacs 十全大补包好了,emacs 和 vim 都具备了。

 > find emacs-27.1-mac-8.1 -iname '*mode.elc'
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/ruler-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/gnus/score-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/net/sieve-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/net/snmp-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/calendar/todo-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/eshell/esh-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/image-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/obsolete/ws-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/obsolete/otodo-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/obsolete/pc-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/nxml/nxml-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/text-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/less-css-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/nroff-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/dns-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/conf-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/bib-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/css-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/mhtml-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/tex-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/textmodes/sgml-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/arc-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/tar-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/calc/calc-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/cedet/srecode/mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/cedet/srecode/srt-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/cedet/semantic/decorate/mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/cedet/semantic/db-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/vc/smerge-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/vc/diff-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/bat-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/dcl-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/make-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/vhdl-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/meta-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/ruby-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/ps-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/cc-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/elisp-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/vera-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/mixal-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/verilog-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/cperl-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/antlr-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/perl-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/asm-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/prog-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/progmodes/m4-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/emacs-lisp/easy-mmode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/emacs-lisp/lisp-mode.elc
emacs-27.1-mac-8.1/share/emacs/27.1/lisp/help-mode.elc

感谢回复。应该是我没有开启对应的 mode。我不想装不熟悉的插件,只是希望在服务器上看代码的时候能够提供一个简单的语法高亮+跳转就好了。代码跳转可以用 Emacs 的 etags 实现,但是有些语言比如 Go 的语法高亮一直没有找到解决办法。我看你给出的列表中没有 Go 的包,默认安装的 Emacs 应该是不支持 Go 的高亮了。

有个很笨的办法,不想装任何插件,又需要某个语言的语法高亮,emacs又不内置,比如emacs没有php-mode,我曾经设置过 (add-to-list 'auto-mode-alist '("\\.php\\'" . js-mode)),这是尝试了c+,java,python等以后发现js的语法关键字和php是最像的。

emacs默认也没有go-mode,但那个语言的语法关键字和go是最像的,这个就不知道了。至etags,ctags这个应该与主模块没有关系,只要TAGS文件能正常生成就可能跳转

你的脑洞还挺大的 :grinning: Emacs 不支持某些 Go 的语法高亮也没关系,就不去找了。没有高亮也可以看代码。

Emacs 默认没有这么多,可能是因为协议问题。插件作者要想让插件进入 emacs 本体,还是比较麻烦的。