Difference between revisions of "Emacs (简体中文)"

From ArchWiki
Jump to: navigation, search
(X 窗口下的字符显示问题)
(update interlanguage links (https://github.com/lahwaacz/wiki-scripts/blob/master/update-interlanguage-links.py))
 
(31 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 
[[Category:Text editors (简体中文)]]
 
[[Category:Text editors (简体中文)]]
 +
[[de:Emacs]]
 
[[en:Emacs]]
 
[[en:Emacs]]
 +
[[es:Emacs]]
 +
[[fr:Emacs]]
 +
[[ja:Emacs]]
 
[[sr:Emacs]]
 
[[sr:Emacs]]
{{TranslationStatus (简体中文)|Emacs|2012-10-17|229169}}
+
{{TranslationStatus (简体中文)|Emacs|2014-12-08|229169}}
 
{{Translateme (简体中文)}}
 
{{Translateme (简体中文)}}
{{Article summary start|Summary}}
 
{{Article summary text|Tutorial on acquiring and using the Emacs text editor.}}
 
{{Article summary end}}
 
 
 
[[Wikipedia:Emacs|Emacs]]是一个扩展方便,定制能力强,文档丰富的动态交互编辑器。Emacs的核心构建在[[Wikipedia:Emacs Lisp|Emacs Lisp]]解释器之上,其中Emacs Lisp是大部分Emacs的内建函数和拓展模块的实现语言。Emacs可以在命令行界面下(CLI)工作良好,在图形界面系统下,使用GTK作为默认的图形界面构建工具。在文本编辑能力上,Emacs常常拿来和[[vim]]比较。
 
[[Wikipedia:Emacs|Emacs]]是一个扩展方便,定制能力强,文档丰富的动态交互编辑器。Emacs的核心构建在[[Wikipedia:Emacs Lisp|Emacs Lisp]]解释器之上,其中Emacs Lisp是大部分Emacs的内建函数和拓展模块的实现语言。Emacs可以在命令行界面下(CLI)工作良好,在图形界面系统下,使用GTK作为默认的图形界面构建工具。在文本编辑能力上,Emacs常常拿来和[[vim]]比较。
  
Line 13: Line 13:
  
 
== 安装 ==
 
== 安装 ==
Emacs有众多变体发行版本(有时候称作''emacsen''). 最常见的莫过于 [http://www.gnu.org/software/emacs/ GNU Emacs],在[[Official Repositories]]可以找到
+
Emacs有众多变体发行版本(有时候称作''emacsen''). 最常见的莫过于 [http://www.gnu.org/software/emacs/ GNU Emacs],在[[Official repositories]]可以找到
  
 
  $ pacman -S emacs
 
  $ pacman -S emacs
 +
 +
如果你总是在终端下工作的话,你可能会选择{{Pkg|emacs-nox}}。这是一个没有GTK+依赖的版本(也没有声音支持)。
 +
 +
终端版Emacs存在以下问题:它支持更少的颜色和更少的字体控制功能(缺少在线控制字体大小、在一篇文档中使用多种字体等功能)。而且对于一些需要高级特性的功能比如Speedbar或者GUD(调试环境),它也不支持。另外在控制复杂的外观(face)时,emacs-nox比Emacs要慢。
 +
 +
如果你想体验Emacs的所有扩展功能而不用装一堆依赖的话,你可以使用PKGBUILD来按你的需求定制Emacs。不使用{{ic|gtk3}}可以让Emacs避免使用gconf。图像和声音的支持也可以去除。在Emacs的源代码目录下运行{{ic|./configure --help}}可以看看有哪些配置选项。
 +
{{hc|PKGBUILD|<nowiki>
 +
# ...
 +
  ./configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib \
 +
    --localstatedir=/var --with-x-toolkit=gtk2 --with-xft \
 +
    --without-gconf --without-sound
 +
# ...
 +
</nowiki>}}
  
 
另外一个常见的变体就是{{Pkg|xemacs}}.
 
另外一个常见的变体就是{{Pkg|xemacs}}.
Line 27: Line 40:
  
 
===运行Emacs===
 
===运行Emacs===
 +
 +
在启动Emacs之前,你要先学会怎么关闭它(尤其是你要在终端里运行它的话):用{{ic|Ctrl+x}}{{ic|Ctrl+c}}来关闭它。
 +
 
====图形界面下打开方式====
 
====图形界面下打开方式====
 
图形界面下可以直接点击图标打开。
 
图形界面下可以直接点击图标打开。
Line 39: Line 55:
 
  $ emacs -nw
 
  $ emacs -nw
  
也可以马上打开一篇文章:
+
也可以直接用下面的命令来打开一个文件:
  
 
  $ emacs filename.txt
 
  $ emacs filename.txt
  
=====无色运行=====
+
=====去掉颜色=====
 
默认的emacs会带有颜色主题,如果不需要,可以关闭之:
 
默认的emacs会带有颜色主题,如果不需要,可以关闭之:
  
Line 68: Line 84:
  
 
更聪明点,你可以在.bashrc中加上下面的别名:
 
更聪明点,你可以在.bashrc中加上下面的别名:
 
+
{{bc|1=
$ alias e0='emacs --daemon'  #启动守护进程
+
alias em='emacsclient -t -a ""'  #在终端中开启emacs
$ alias e1='emacsclient -t -a ""'  #在终端中开启emacs
+
alias emc='emacsclient -nc -a ""'  #启动emacs图形界面
$ alias e2='emacsclient -c -a ""' #启动emacs图形界面
+
EDITOR='emacsclient -a ""'
 
+
}}
  
 
在[[xfce]]桌面环境中,如果你想使用 emacsclient -c 来代替 emacs %f 打开一个新文件, 你可以修改你的 /usr/share/applications/emacs.desktop 文件,把下面这一行
 
在[[xfce]]桌面环境中,如果你想使用 emacsclient -c 来代替 emacs %f 打开一个新文件, 你可以修改你的 /usr/share/applications/emacs.desktop 文件,把下面这一行
Line 91: Line 107:
 
在Emacs中,对于按键组合的约定你可能很陌生。比如:
 
在Emacs中,对于按键组合的约定你可能很陌生。比如:
  
'''C-x''' 代表 Control-x
+
'''C-x''' 代表 Ctrl-x
  
 
'''M-x''' 代表 Meta-x
 
'''M-x''' 代表 Meta-x
  
{{Note|'Meta'一般代表Alt键,也可以用Esc键替代。}}
+
{{注意|'Meta'一般代表Alt键,也可以用Esc键替代。}}
  
举个例子,退出Emacs使用下面的按键组合'''C-x C-c'''。这个可以读做,"按住Ctrol键再按'x',释放,再按住Control键再按'c'。虽然Emacs提供了一个菜单栏,但是强烈建议学习使用按键组合。这个指南将参考Emacs的按键绑定的约定。
+
举个例子,退出Emacs使用下面的按键组合'''C-x C-c'''。这个可以读做,"按住Ctrl键再按'x',释放,再按住Ctrl键再按'c'。虽然Emacs提供了一个菜单栏,但是强烈建议学习使用按键组合。这个指南将参考Emacs的按键绑定的约定。
  
 
===移动===
 
===移动===
Line 109: Line 125:
  
 
===文件和缓冲区===
 
===文件和缓冲区===
Emacs provides a series of commands to act upon files, the most common of which will be detailed here.  '''C-x C-f''' is used to open a file (this command is called 'find-file' in Emacs).  Should the file specified not exist, Emacs will open an empty buffer.  Saving a buffer will create the file with the buffer's contents.  '''C-x C-s''' can be used to save a buffer.  To save a buffer with a different filename, use '''C-x C-w''' (this is a mnemonic for the command 'write-file'), which will prompt for the new filename before writing it to disk.  It is also possible to ensure all buffers are saved with '''C-x s''', which, should a buffer be modified since its last save, a prompt will be displayed asking which action to take.
+
Emacs 提供了一系列命令来对文件操作,其中最常用的会在这里详细说明。'''C-x C-f''' 用来打开一个文件(在Emacs中叫做'查找文件')。如果指定的文件不存在,Emacs会打开一个空的缓冲区。保存一个缓冲区会创建一个包含缓冲区内容的文件。'''C-x C-s''' 就是用来保存缓冲区的。要保存一个文件名不一样的缓冲区,使用'''C-x C-w''' (这其实是'write-file'这条命令的助记符), 它会在写入磁盘之前提示输入新文件名。也可以使用'''C-x s'''来保存所有的缓冲区, 如果某个缓冲区在上次保存之后被修改了,则会提示进行哪项操作。
  
{{Note|'''C-x C-f''' does not read the file from disk again if a buffer corresponding to the file is still opened.  To re-read the file from disk, kill the buffer ('''C-x k''') prior to '''C-x C-f''' or use '''M-x revert-buffer'''.}}
+
{{Note|如果指向某个文件的缓冲区还在打开的话,'''C-x C-f''' 是不会重新从磁盘中读取文件的。要从磁盘中重新读取文件,先使用'''C-x k'''关掉缓冲区,再使用'''C-x C-f'''打开文件,或者使用'''M-x revert-buffer'''.}}
  
Many interactive commands such as "find-file" or "write-file" prompt for input in the bottom-most line of the Emacs window.  This line is referred to as the ''minibuffer''.  The minibuffer supports many basic editing commands as well as tab-completion similar to that which is available in many *nix shells.  '''<TAB>''' can be pressed twice in succession to display a list of completions, and if desired, the mouse can be also be used to select a completion from that list.  Completion in the minibuffer is available for many forms of input including commands and filenames.
+
很多互动的命令,比如"find-file" 或者 "write-file" 会在Emacs窗口的底部栏提示输入。这栏称为''minibuffer''。和很多*nix shell一样,minibuffer支持很多基本的操作和TAB补全。按两下'''<TAB>'''可以显示一个补全的选项列表,并且,如果你喜欢,可以用鼠标从列表中选择。minibuffer的补全在很多输入(包括命令和文件名)中都可以用。
  
The minibuffer also provides a history feature.  The previous items entered for a command can be recalled using the '''Up Arrow''' or '''C-p'''.
+
minibuffer也提供一个记住历史的特性。通过'''Up Arrow''' 或者 '''C-p'''可以取得这条命令的上一个条目.
  
To exit the minibuffer at any time, press '''C-g'''.
+
要在任意时刻退出minibuffer,使用'''C-g'''.
  
After opening several files, a way to switch between them is needed.  Opening a file corresponding to a buffer already available in Emacs, will cause Emacs to switch to that buffer.  But this is not the most effective way.  Emacs provides '''C-x b''', which prompts for the new buffer to be displayed (tab-completion is available here).  By entering the name of a buffer which does not exist, a new buffer with that name will be created.
+
打开几个文件后,切换缓冲区是非常必要的。打开一个指向那个缓冲区的文件可以切换到那个缓冲区。但是这不是最高效的方法。Emacs提供'''C-x b'''来提示要显示的新缓冲区(这里可以使用TAB补全)。输入一个不存在的缓冲区,则会新建一个空的缓冲区。
  
{{Note|To switch to the previous buffer use '''C-x b <RET>''', as the previous buffer is the default.}}
+
{{Note|要切换到上一个缓冲区,使用'''C-x b <RET>''', 因为上一个缓冲区是默认的缓冲区。}}
  
A list of all open buffers can be displayed using '''C-x C-b'''.  Should a buffer no longer be required, it can be removed with '''C-x k'''.
+
使用'''C-x C-b'''可以显示所有打开缓冲区的列表。如果某个缓冲区不需要使用了,使用'''C-x k'''来关掉它。
  
 
===编辑===
 
===编辑===
Many editing commands exist within Emacs.  Perhaps the most important command which has not yet been introduced is 'undo', which can be performed via '''C-_''' or '''C-/'''.  Movement commands generally also have a corresponding delete command. For example, '''M-<backspace>''' can be used to delete a word backwards, and '''M-d''' to delete a word forwards.  To delete to the end of the line, or the end of the sentence, use '''C-k''' or '''M-k''', respectively.
+
Emacs 内建有很多编辑命令。可能最重要的还没有介绍的是'undo',它的快捷键为 ''C-_'' 或者 ''C-/'' .移动光标的命令通常都有对应的删除字符的命令。例如, '''M-<backspace>''' 可以用来删除一个光标后的词,'''M-d'''可以用来删除光标前面的一个词。删除光标至行尾或者句尾的字符可以分别用'''C-k''' 或者 '''M-k'''
  
It is a rule-of-thumb that no line be allowed to exceed 80 characters.  This aids readability, especially in cases where the line wraps at the edge of a window.  Automatically inserting (or removing) line separator(s) is known as ''filling'' in Emacs.  A paragraph can be filled using '''M-q'''.
+
通常我们都约定一行不能超过80个字符。这是为了代码的可读性,尤其是一行中的字符可能会接触到窗口边缘。在Emacs,自动地插入或者删除换行符称为''filling''。我们可以用 '''M-q''' 重整当前的段落(重新分配换行符,删除段落中多余的空格和tab键)
  
Characters and words can be transposed using '''C-t''' and '''M-t''', respectively.  For example: <code>Hello World!</code> → <code>World! Hello</code>
+
字符和单词可以分别通过 '''C-t''' '''M-t''' 进行交换。比如: <code>Hello World!</code> → <code>World! Hello</code>
  
The case of words is also readily adjustable.  '''M-l''' downcases a word from point (<code>HELLO</code> → <code>hello</code>); '''M-u''' upcases a word from point (<code>hello</code> → <code>HELLO</code>) and '''M-c''' capitalizes the first character of a word from point while downcasing the remainder (<code>hElLo</code> → <code>Hello</code>).
+
单词的大小写也可以调整。'''M-l''' 把光标处的单词变成小写 (<code>HELLO</code> → <code>hello</code>); '''M-u''' 把光标处的单词变成大写(<code>hello</code> → <code>HELLO</code>)'''M-c''' 把光标处的单词的第一个字母变成大写,并把后面的变成小写(<code>hElLo</code> → <code>Hello</code>).
  
 
===移除,召回和区域===
 
===移除,召回和区域===
A region is a section of text between two positions.  One of those positions is referred to as ''mark'', and the other is point.  '''C-<SPC>''' is used to set the position of mark, after which point can be moved to create a region.  Within GNU Emacs 23.1 onwards, this region is visible by default.  There are a number of commands which act upon regions, among the most commonly used are ''killing'' commands.
+
一个''区域''(region)是指在两个位置之间的一段字。其中一个位置被称为''标记''(mark),另一个是光标。'''C-<SPC>'''用来设置标记的位置,紧接着就可以通过移动光标来创造一个区域。在GNU Emacs 23.1及以后的版本中,这个区域默认是可见的。有许多命令是针对区域的,其中最常用的就是''kill''命令。
  
In Emacs, cut and paste are referred to as ''kill'' and ''yank'', respectively.  Many commands which delete more than one character (including many of those in the above section, such as '''C-k''' and '''M-d''') actually cut the text and append it to what is known as the ''kill-ring''.  The kill-ring is simply a list of killed text.  The kill-ring stores up to the last 60 kills by default.  Successive kills are concatenated and stored at the head of the list.
+
在Emacs中,剪切和粘贴分别对应的命令叫做''kill''''yank''。许多删除多个字符的命令(包括上面提到的'''C-k''''''M-d'''命令)实际上是把文字剪切下来,附加到一个叫''kill-ring''的地方。kill-ring 就是一个被删掉的文字的列表。在默认情况下,kill-ring会保存最多60次删除记录。连续的删除会连在一起。
  
'''C-w''' and '''M-w''' can be used to kill and copy a region, respectively.
+
'''C-w''' '''M-w''' 可以用来删除或复制一个区域。
  
To insert killed text into a buffer (known as 'yanking'), use '''C-y''''''C-y''' can be used multiple times in succession to yank text repeatedly.  As mentioned, previous kills are stored in a list, however '''C-y''' only retrieves the first of them.  The earlier kills can be accessed via '''M-y'''.  This will remove the text inserted by 'yank' initially, replacing it with the text killed earlier.  '''M-y''' must be used immediately following '''C-y''' and can be used in many times succession to cycle through the kill-ring.
+
要想把删掉的文字插入进来(也就是'yanking'),可以用'''C-y'''命令。'''C-y''' 可以连续用好多次来重复插入。按刚刚所讲,之前删除的文字会排成一个列表,但是'''C-y'''只能获取列表中的第一个,也就是最后删掉的一个。再之前删掉的可以通过'''M-y'''来获取。它会把'''C-y'''刚刚插入的文字用再早删除的文字替换掉。'''M-y''' 必须紧跟着'''C-y'''命令执行,可以执行很多次以遍历kill-ring。
  
 
===查找和替换===
 
===查找和替换===
Searching for a string is common practise in text-editing.  This can be performed using '''C-s''' (to search forward) or '''C-r''' (to search backward).  These commands prompt for the string for which to search.  Searching is performed incrementally, and so it will match the next (or previous) occurrence as you type.  To move to the next or previous match, press '''C-s''' or '''C-r''' again, respectively.  Once a match has been found, '''<RET>''' can be used to end the search.  Alternatively, should you wish to return to the location you initiated the search, use '''C-g'''.
+
在文本编辑中进行字符串查找是很常见的任务。按'''C-s'''可以正向搜索,按'''C-r‘’‘则可以反向搜索。这些命令提示要搜索的字符串。搜索是增量的,你再次按键时会显示下一个匹配的位置,并且可以使用'''C-s’‘’向前或者使用'''C-r‘’‘向后。找到匹配结果之后,可以按下'''<RET>'''结束搜索。另外,如果你想回到发起搜索的位置,可以使用'''C-g'''
  
Once a search is completed (i.e., was not aborted with '''C-g''' or similar), the string which was searched for will be the default for any following search.  To make use of this, press '''C-s C-s''' or '''C-r C-r''' to search forward or backward again, respectively.
+
一旦搜索完成(不是由'''C-g'''之类的指令引起),当前搜索的字符串将会作为下一次搜索的默认参数。使用’‘’C-s C-s'''或者'''C-r C-r'''可以达到这样的效果。
  
Regular Expression searches behave identically to the searching described above except for the command to initiate the search.  Use '''C-M-s''' or '''C-M-r''' to initiate a regexp search forward or backward, respectively.  Once a Regular Expression search has commenced, '''C-s''' and '''C-r''' can be used to search forward or backward, just as with string searches.
+
I-search 有一些有用的命令,使用'''M-e'''来编辑当且搜索区域,使用'''M-c'''来触发大小写敏感匹配。
  
In addition to searching, it is also possible to perform string and regular expression replacement (via '''M-%''' and '''C-M-%''', respectively).  Prompts are provided for both the initial and replacement text, and then another prompt for the action to perform on the highlighted match.  Although many options are available (the full list is available by pressing '''?'''), the most commonly used are '''y''', to perform replacement, '''n''', to skip this match, and '''!''' to replace this, and all following matches.
+
正则表达式搜索除了指令不同之外,其他方面和以上都相同。使用'''C-M-s'''或者'''C-M-r'''来使用正则表达式搜索。相应地,用'''C-s'''和'''C-r'''可以前后移动,和普通的搜索没什么两样。
 +
 
 +
除了搜索之外,进行字符串替换也是有必要的。原始文本和替换文本都会有提示,替换的时候也会有提示。尽管有很多选项提供(按'''?'''可以了解完整的选项),最常用的还是'''y'''(进行替换),'''n'''(跳过),以及'''!'''(替换当前及之后所有匹配)。
  
 
===缩进和前缀参数===
 
===缩进和前缀参数===
Line 170: Line 188:
  
 
===窗口和外框架===
 
===窗口和外框架===
Emacs is designed for convenient editing of many files at a time. This is achieved by dividing the Emacs interface into three levels.  Namely, buffers, which have already been introduced, as well as ''windows'' and ''frames''.
+
Emacs的设计是可以同时方便地编辑多个文件。这是通过把Emacs的接口分成三个层次来实现的,即buffer(之前介绍过了),''window''''frame''
  
A ''window'' is a viewport used for displaying a buffer.  A window can display only one buffer at a time, however one buffer can be displayed in many windows.  Beneath each window exists a ''mode-line'', which displays information for that buffer.
+
''window'' 是显示buffer的一个viewport(社区)。一个window一次只能显示一个buffer。但是一个buffer可以在多个window中显示。在窗口下面有一个''mode-line'',它用于显示当前buffer的信息。
  
A ''frame'' is an Emacs "window" (in standard terminology.  i.e., 'window' in the sense of the modern desktop paradigm) which contains a title bar, menu bar and one or more 'windows' (in Emacs terminology.  i.e., the above definition of 'window').
+
''frame'' 是Emacs的一个"窗口"(这是标准的术语。比如,'窗口'是现代桌面的称谓),它包含了标题栏,菜单栏,还有一个或多个'window'(这是Emacs的术语,比如上面提到的'window')。
  
From now on the definition of these terms as they exist in Emacs will be used.
+
从现在起,这些Emacs中存在的名词的定义就可以使用了。
  
To split the window vertically or horizontally, use '''C-x 2''' or '''C-x 3''', respectively.  This has the effect of creating another window in the current frame.  To cycle between multiple windows, use '''C-x o'''.
+
要把window水平分隔和垂直分隔,分别使用'''C-x 2''''''C-x 3'''。这种效果其实就是在当前frame中开户另外一个window。要在多个window中循环切换,使用'''C-x o'''
  
The opposite of splitting a window, is deleting it.  To delete the current window, use '''C-x 0''' and '''C-x 1''' to delete all windows except the current.
+
与分隔一个window相反的是删除window。要删掉当前window,使用'''C-x 0''',要删掉除了当前window之外的其它window,使用'''C-x 1'''
  
As with windows, it is also possible to create and delete frames.  '''C-x 5 2''' creates a frame.  With '''C-x 5 0''' to delete the current frame and '''C-x 5 1''' to delete all except the current frame.
+
和window一样,创建和删除frame也是可以的。'''C-x 5 2'''会创建一个frame,'''C-x 5 0'''会删除当前的frame,'''C-x 5 1'''会删除除了当前frame之外的其它frame。
  
{{Note|These commands do not affect buffers.  For example, deleting a window does not kill the buffer it displays.}}
+
{{注意|这些命令不影响buffer。比如删除一个window并不会删除在window中显示的buffer。}}
  
 
===获得帮助===
 
===获得帮助===
Line 204: Line 222:
  
 
===模式===
 
===模式===
An Emacs mode is an extension written in Emacs Lisp that controls the behaviour of the buffer it is attached to. Usually it provides indentation, syntax highlighting and keybindings for editing that form of text. Sophisticated modes can turn Emacs into a full-fledged IDE (Integrated Development Environment). Emacs will generally use a file's extension to determine which mode should be loaded.
+
一个Emacs模式是用Emacs Lisp语言写的一个控制相关缓冲区行为的扩展。一般来说,它给编辑文本提供缩进,语法高亮和键绑定的功能。复杂的模式可以把Emacs变成一个完美的IDE (Integrated Development Environment). Emacs 一般会使用一个文件的扩展来确定应该加载那个模式。
  
Useful modes for editing shell scripts are sh-mode, line-number-mode and column-number-mode. They can be used in parallel and are invoked by:
+
一些编写shell脚本比较有用的模式有sh-mode, line-number-mode column-number-mode。它们可以并行地通过下面的方式激活:
  
 
'''M-x sh-mode <RET>'''
 
'''M-x sh-mode <RET>'''
Line 212: Line 230:
 
'''M-x column-number-mode <RET>'''
 
'''M-x column-number-mode <RET>'''
  
line-number-mode is enabled by default, though, it can be toggled on/off by issuing the command again:
+
line-number-mode 默认是激活的,它可以通过下面的命令来打开/关闭:
  
 
'''M-x line-number-mode <RET>'''
 
'''M-x line-number-mode <RET>'''
  
sh-mode is a ''major-mode''.  Major-modes adjust Emacs, and often also provide a specialised set of commands, for editing a particular type of text.  Only one major-mode can be active in each buffer.  In addition to syntax highlighting, and indentation support, sh-mode defines several commands to help write shell scripts.  The following shows a few of those commands:
+
sh-mode 是一个 ''major-mode''.  Major-modes 调整Emacs,并且经常提供了一些特定的命令来编辑某种类型的文本。一个缓冲区只能激活一种major-mode。除了支持语法高亮和缩进,sh-mode还定义了几条命令来帮助快速开发shell脚本。下面是其中的几条:
  
  '''C-c (''' Insert a function definition
+
  '''C-c (''' 插入一个函数定义
 
   
 
   
  '''C-c C-f''' Insert a 'for' loop
+
  '''C-c C-f''' 插入一个for循环
 
   
 
   
  '''C-c TAB''' Insert an 'if' statement
+
  '''C-c TAB''' 插入一条if语句
 
   
 
   
  '''C-c C-w''' Insert a 'while' loop
+
  '''C-c C-w''' 插入一个while循环
 
   
 
   
  '''C-c C-l''' Insert an indexed loop from 1 to n
+
  '''C-c C-l''' 插入一个从1到n的有下标的循环
  
'line-number-mode' and 'column-number-mode', are ''minor-modes''.  Minor-modes can be used to extend a major-mode and any number of minor-modes can be enabled at once.
+
'line-number-mode' 'column-number-mode' ''minor-modes''.  Minor-modes 可以用来扩充major-mode的功能,多个minor-mode可以同时激活。
  
 
==提示和技巧==
 
==提示和技巧==
While the previous sections has given an overview of the basic editing commands available, it has not given an indication of the possibilities of Emacs.  This section will cover some more advanced techniques and functionality.
+
前面的部分给出了基本编辑命令的概述,没有给出Emacs的一个指示。这个部分讲述一些高级的技巧和功能。
  
 
===TRAMP===
 
===TRAMP===
TRAMP (Transparent Remote Access, Multiple Protocols) is an extension which, as its name suggests, provides transparent access to remote files across a number of protocols.  When prompted for a filename, entering a specific form will invoke TRAMP.  Some examples:
+
TRAMP (Transparent Remote Access, Multiple Protocols) ,顾名思义,是一个可以通过很多协议透明访问远程文件的一个扩展。当提示输入一个文件名,输入特定的格式就可以使用TRAMPP。比如:
  
To prompt for the root password before opening /etc/hosts with root permissions:
+
在打开{{ic|/etc/hosts}}文件之前提示输入root的密码以获取root权限:
  
 
  C-x C-f /su::/etc/hosts
 
  C-x C-f /su::/etc/hosts
  
To connect to 'myhost' as 'myuser' via SSH and open the file ~/example.txt:
+
要通过SSH使用'myuser'用户名登录'myhost'主机并打开文件{{ic|~/example.txt}}:
  
 
  C-x C-f /ssh:myuser@myhost:~/example.txt
 
  C-x C-f /ssh:myuser@myhost:~/example.txt
  
The path for TRAMP is typically of the form '/[protocol]:[[user@]host]:<file>'.  TRAMP supports much more than the examples above might indicate.  For more information refer to the TRAMP info manual, which is distributed with Emacs.
+
TRAMP的路径一般是这种格式'/[protocol]:[[user@]host]:<file>'。TRAMP支持的不只上面的两个简单例子。请查看Emacs里面的TRAMPP info手册了解更多的信息。
  
 
===键盘宏和寄存器===
 
===键盘宏和寄存器===
This section will provide a practical demonstration of the use of a couple of more powerful editing features.  Namely, ''keyboard macros'' and ''registers''.
+
这一部分会提供一个实用的指南来使用一些更强大的编辑特性。也就是“键盘宏”和“寄存器”。
  
The aim will be to produce a listing of a series of characters and their corresponding position in this list.  While it is possible to format each of them by hand, this would be slow and error-prone.  Alternatively, some of Emacs' more powerful editing functionality could be leveraged.  Before describing a solution, some details behind the techniques which will be used follow.
+
我们的目标是产生一个字符列表和它们在这个列表中对应的位置。虽然这可以通过手工格式化来完成,但是这样会很慢而且容易出错。如果我们采用一些Emacs更高级的编辑功能却可以起到四两拨千斤的功效。在介绍这个方法之前,需要先了解一些技术背后的细节。
  
The first feature which will be introduced is ''registers''.  Registers are used to store and retrieve a variety of data types ranging from numbers to window configurations.  Each register is given a name of a single character: this character is used to access the register.
+
要介绍的第一个特性就是''寄存器''。寄存器的功能是用来保存和获取各种各样的数据。每个寄存器用一个字母来命名,这个字母就是用来调用这个寄存器的。
  
The other which will be demonstrated is ''keyboard macros''.  A keyboard macro stores a sequence of commands so they can be easily repeated later.  These changes will now be performed step-by-step.
+
另一个要介绍的就是''键盘宏''。一个键盘宏存储了一个命令序列以便以后可以重复使用。下面就一步一步地讲解这个方法。
  
Starting with a buffer containing our set of characters:
+
首先我们从一个包含如下字符的缓冲区开始:
 
  ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
 
  ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
  
Prepare a register by invoking the `number-to-register' command ('''C-x r n''') then storing the number '0' in register 'k':
+
通过`number-to-register' 命令 ('''C-x r n''')我们来准备一个寄存器,把数字'0'存储在寄存器'k'中:
  
 
  C-x r n k
 
  C-x r n k
  
With point at the beginning of the buffer, start a keyboard macro ('''C-x (''') and begin to format the characters:
+
当光标在缓冲区开头的时候,开始录制键盘宏 ('''C-x (''') 然后开始对字符串进行格式化:
  
 
  C-x ( C-f M-4 .
 
  C-x ( C-f M-4 .
  
Insert ('''C-x r i''') and increment ('''C-x r +''') the register 'k'. The prefix argument ('''C-u''') is used to leave point positioned after the inserted text:
+
插入 ('''C-x r i''') 然后将寄存器'k'加1 ('''C-x r +''') 。开头的 ('''C-u''') 命令是用来在插入文字之后让光标移到插入的文字后面:
  
 
  C-u C-x r i k C-x r + k
 
  C-u C-x r i k C-x r + k
  
Complete the formatting by inserting a newline.  Emacs can then repeat that process, beginning from the point where we started defining the keyboard macro, for the rest of the characters.  '''C-x e''' completes then invokes the keyboard macro.  The prefix argument, '''M-0''', causes the macro to repeat until it comes across an error.  In this case it aborts once it reaches the end of the buffer.
+
最后我们来插入一个回车来结束格式化。Emacs可以重复以上过程,从我们定义键盘宏的位置开始,直到最后一个字符。'''C-x e'''命令停止宏的录制并开始执行这段宏。开头的 '''M-0''' 命令是用来让宏在出错的时候停下来,这样在它走到这行的结尾就会停下来。
  
 
  <RET> M-0 C-x e
 
  <RET> M-0 C-x e
  
The result:
+
下面是结果:
  
 
   A....0
 
   A....0
Line 300: Line 318:
 
Finally, press '''!''' to apply this across the entire buffer.  All of the formatting that was performed in the previous section was performed with a single regexp replacement.
 
Finally, press '''!''' to apply this across the entire buffer.  All of the formatting that was performed in the previous section was performed with a single regexp replacement.
  
===定制===
+
==定制==
Emacs can configured by editing '~/.emacs' or using '''M-x customize'''. This section will focus on editing ~/.emacs by hand, and provide some example customizations to demonstrate commonly-configured aspects of Emacs.  The customize command provides a simple interface to make adjustments, though it may become restricting as you grow more familiar with Emacs.
+
Emacs能通过~/.emacs或者'''M-x customize'''来定制。本段落将着眼于手动定制 ~/.emacs,并且提供了一些常用配置的样例。配置命令提供了一个适应的途径,通过它你能够渐渐熟悉Emacs。
  
All of the examples here can be performed while Emacs is running.  To evaluate the expression within Emacs, use:
+
这里的所有例子都能在Emacs中奏效。比如,在Emacs中计算表达式:
 +
'''C-M-x''' 同时指到任何需要求值的地方。
  
'''C-M-x''' with point anywhere within the expression.
+
或者
  
or
+
'''C-x C-e''' 指到最后的“)”。
  
'''C-x C-e''' with point following the last ')'
+
'y''n'来代替频繁地输入'yes’和'no‘是一个好的方法:
 
+
For some users, typing 'yes' and 'no' in prompts can quickly become tiring.  To instead use the 'y' and 'n' keys at these prompts:
+
  
 
  (defalias 'yes-or-no-p 'y-or-n-p)
 
  (defalias 'yes-or-no-p 'y-or-n-p)
  
To stop the cursor blinking, use:
+
取消光标闪烁:
  
 
  (blink-cursor-mode -1)
 
  (blink-cursor-mode -1)
  
Similarly, to enable column-number-mode, as discussed in the previous section:
+
类似地,开启上一节提到的列号模式:
  
 
  (column-number-mode 1)
 
  (column-number-mode 1)
  
The similarities between the previous two commands are not a coincidence: blink-cursor-mode and column-number-mode are both minor-modes.  As a rule, minor-modes can be enabled given positive argument or disabled with a negative argument.  Should the argument be omitted, the minor-mode will be toggled on/off.
+
它们两者之间的相似不是偶然:光标闪烁模式和列号模式都是'副参模式',按照规则,'副参模式’能通过正负值来设置。如果参数省略,'副参模式'将被开/关。
 
+
Here are some more examples of minor-modes.  The following will disable the scroll bars, menu-bar and tool-bar, respectively.
+
  
 +
这里有其他一些'副参模式'的例子,下面的参数将会关闭滚动栏、菜单栏、工具栏。
 
  (scroll-bar-mode -1)
 
  (scroll-bar-mode -1)
 
  (menu-bar-mode -1)
 
  (menu-bar-mode -1)
 
  (tool-bar-mode -1)
 
  (tool-bar-mode -1)
  
The variable, 'auto-mode-alist', can be modified to change the major-mode used by default for certain file names.  The following example will make the default major-mode for '.tut' and '.req' files 'text-mode'.
+
变量'auto-mode-alist'修改之后能够改变默认的“主参模式”。下面的例子把'.tut''.req'修改成了'.text-mode'
  
 
  (setq auto-mode-alist
 
  (setq auto-mode-alist
Line 359: Line 375:
 
Almost anything within Emacs can be configured.  Browsing through the [http://emacswiki.org/ Emacs Wiki] should give a solid place to start.
 
Almost anything within Emacs can be configured.  Browsing through the [http://emacswiki.org/ Emacs Wiki] should give a solid place to start.
  
=== 拓展模块 ===
 
  
While Emacs includes hundreds of modes, libraries and other extensions, there are many more available to further Emacs' capabilities.  The majority of these come with instructions detailing any changes needed to be made to ~/.emacs.  These instructions are generally found in the comment block at the beginning of an elisp source file, or in a README (or similar) should the extension consist of multiple source files.
+
=== 多种配置 ===
  
A number of popular extensions are available as packages in the 'community' repository, and more still are available via the [[AUR]].  The name of such packages have a 'emacs-' prefix (for example, emacs-lua-mode).  In many cases, the changes which need to be made to ~/.emacs are shown during the installation of the package.
+
你可以使用少量配置然后告诉Emacs来加载其他配置。
  
Should instructions describing how to activate a specific extension not be available in the aforementioned location(s), check for a corresponding page in the [http://emacswiki.org/ Emacs Wiki], which will almost certainly provide an example configuration.  The Emacs Wiki is also an excellent resource for discovering even more extensions.
+
例如,我们来定义两个配置文件。
  
You can also use the [http://tromey.com/elpa/ Emacs Lisp Package Archive (ELPA)] to automatically install packagesSee the website for instructionsELPA is included with Emacs 24 (the newest version of Emacs); it is an accepted part of the Emacs ecosystem.
+
{{hc|.emacs|
 +
(load "~/.emacs.d/main" nil t)
 +
(load "~/.emacs.d/functions" nil t)
 +
(load "~/.emacs.d/modes" nil t)
 +
(load "~/.emacs.d/plugins" nil t)
 +
(load "~/.emacs.d/theme" nil t)
 +
}}
 +
 
 +
这是我们在后台载入的完整配置。但是plugins文件太大导致载入太慢,如果我们要打开一个新的Emacs窗口,可能就不会使用plugins配置,每次加载它实在是太笨重了。
 +
 
 +
{{hc|.emacs-light|
 +
(load "~/.emacs.d/main" nil t)
 +
(load "~/.emacs.d/functions" nil t)
 +
(load "~/.emacs.d/modes" nil t)
 +
(load "~/.emacs.d/theme" nil t)
 +
}}
 +
 
 +
现在我们这样来加载Emacs:
 +
 
 +
emacs -q -l ~/.emacs-light
 +
 
 +
你可以为这个命令创建一个别名。
 +
 
 +
=== 加载扩展程序 ===
 +
 
 +
你能用require来加载扩展程序,例如这样:
 +
 
 +
(require 'mediawiki)
 +
 
 +
如果你试着在一个买有安装mediawiki的机器上使用相同的配置,Emacs会提示错误。并且,所有制定的扩展代码都会失效。
 +
 
 +
一个小的技巧是测试require的返回值。
 +
 
 +
(if (require 'mediawiki nil t)
 +
    (progn
 +
      (setq mediawiki-site-alist '(
 +
              ("ArchLinux" "https://wiki.archlinux.org/" "UserName" "" "Main Page")
 +
              )
 +
            )
 +
      (setq mediawiki-mode-hook
 +
            (lambda ()
 +
              (visual-line-mode 1)
 +
              (turn-off-auto-fill)
 +
              ))
 +
  ))
 +
 
 +
=== Local and custom variables ===
 +
 
 +
You can define variables in your configuration file that can be later one modified locally for a file.
 +
 
 +
(defcustom my-compiler "gcc" "Some documentation")
 +
 
 +
Now in any file you can define local variables in two ways:
 +
* On the very first line, write
 +
// -*- my-compiler:g++; mode:c++ -*-
 +
* If you cannot (or do not want to) write this on the first line, you can put it at the end:
 +
// Local Variables:
 +
// my-compiler: g++
 +
// mode: c++
 +
// End:
 +
 
 +
Note that the beginning characters need to be comments for the current language, that's why here we used two backslashes for C++. For Elisp you would use
 +
;; -*- mode:emacs-lisp -*-
 +
 
 +
There is two functions that may help you in defining the variables: ''add-file-local-variable'' and ''add-file-local-variable-prop-line''.
 +
 
 +
Finally, custom variable are considered insecure by default. If you try to open a file that contains local variable redefining insecure custom variables, Emacs will ask you for confirmation.
 +
 
 +
If you know what you are doing, you can declare the variable as secure, thus removing the Emacs prompt for confirmation. You need to specify a predicate that any new value has to verify so that it can be considered safe.
 +
 
 +
(defcustom my-compiler "gcc" "Some documentation" :safe 'stringp)
 +
 
 +
In the previous example, if you attempt to set anything else than a string, Emacs will consider it insecure.
 +
 
 +
=== Custom colors and theme ===
 +
 
 +
Colors can be easily customized using the ''face'' facility.
 +
(set-face-background  'region                "color-17")
 +
(set-face-foreground  'region                "white")
 +
(set-face-bold-p      'font-lock-builtin-face t )
 +
 
 +
You can have let Emacs tell you the name of the face where the point is. Use the ''customize-face'' function for that. The facility will show you how to set colors, bold, underline, etc.
 +
 
 +
Emacs in console can handle 256 colors, but you will have to use an appropriate terminal for that. For instance URxvt has support for 256 colors. You can use the ''list-colors-display'' for a comprehensive list of supported colors. This is highly terminal-dependent.
 +
 
 +
=== SyncTeX support ===
 +
 
 +
Emacs is definitely one of the most powerful LaTeX editor. This is mostly due to the fact you can adapt or create a LaTeX mode to fit your needs best.
 +
 
 +
Still, there might be some challenges, like SyncTeX support. First you need to make sure your TeX distribution has it. If you installed TeX Live manually, you may need to install the ''synctex'' package.
 +
  # umask 022 && tlmgr install synctex
 +
 
 +
SyncTeX support is viewer-dependent. Here we will use Zathura as an example, so the code needs to be adapted if you want to use another PDF viewer.
 +
 
 +
(defcustom tex-my-viewer "zathura --fork -s -x \"emacsclient --eval '(progn (switch-to-buffer  (file-name-nondirectory \"'\"'\"%{input}\"'\"'\")) (goto-line %{line}))'\""
 +
  "PDF Viewer for TeX documents. You may want to fork the viewer
 +
  so that it detects when the same document is launched twice, and
 +
persists when Emacs gets closed.
 +
 +
Simple command:
 +
 +
  zathura --fork
 +
 +
We can use
 +
 +
  emacsclient --eval '(progn (switch-to-buffer  (file-name-nondirectory \"%{input}\")) (goto-line %{line}))'
 +
 +
to reverse-search a pdf using SyncTeX. Note that the quotes and double-quotes matter and must be escaped appropriately."
 +
:safe 'stringp)
 +
 
 +
Here we define our custom variable. If you are using AucTeX or Emacs default LaTeX-mode, you will have to set the viewer accordingly.
 +
 
 +
Now open a LaTeX source file with Emacs, compile the document, and launch the viewer. Zathura will spawn. If you press {{ic|Ctrl+Left click}} Emacs should place the point at the corresponding position.
 +
 
 +
== Documentation ==
 +
 
 +
You may find yourself overwhelmed by the amount of Emacs features. You may find it difficult to know how to use Emacs Lisp to customize your favorite modes, or even to create your own modes / packages. Thankfully Emacs takes a strong point to auto-documenting everything: its internals, current configuration, bindings, etc. Almost everything is documented.
 +
 
 +
=== Contextual help ===
 +
 
 +
Emacs is self-documenting by design.  As such, a great deal of information is available to determine the name of a specific command or its keybinding, for example.  The following is a listing of some of the most helpful of these:
 +
 +
'''C-h a'''        Find a command matching a description.
 +
 
 +
'''C-h b'''        List all active keybindings.
 +
 
 +
'''C-h f'''        Describe the given function.
 +
 
 +
'''C-h k'''        Find which command a key is bound to.
 +
 
 +
'''C-h m'''        Display information regarding the currently active modes.
 +
 
 +
'''C-h t'''        Start the Emacs tutorial.
 +
 
 +
'''C-h v'''        Describe the given variable.
 +
 
 +
'''C-h w'''        Find which key(s) a command is bound to.
 +
 
 +
=== The manuals ===
 +
 
 +
If you really want to master Emacs, the most recommanded source of documentation remains the official manuals:
 +
* Emacs: the complete Emacs user manual.
 +
* Emacs FAQ.
 +
* Emacs Lisp Intro: if you never used any programming language before.
 +
* Elisp: if you are already familiar with a programming language.
 +
 
 +
You can access it as PDFs from [http://www.gnu.org/software/emacs/manual/ GNU.org] or directly from Emacs itself thanks to the embedded 'info' reader: '''C-h i'''. Press '''m''' to choose a book.
 +
 
 +
Some users prefer to read books using 'info' because of its convenient shortcuts, its paragraphs adapting to window width and the font adapted to current screen resolution. Some find it less irritating to the eyes. Finally you can easily copy content from the book to any Emacs buffer, and you can even execute Lisp code snippets directly from the examples.
 +
 
 +
You may want to read the '''Info''' book to know more about it: '''C-h i m info <RET>'''.
 +
Press '''?''' while in info mode for a quick list of shortcuts.
 +
 
 +
== 拓展模块 ==
 +
 
 +
虽然Emacs包含了成百上千种模式(mode),库和其它扩展,还有更多的扩展来增强Emacs。大多数扩展会详细说明安装它的时候要对~/.emacs作什么改动。这些说明一般会在一个elisp源文件开头的注释中,或者在一个README中(如果这个扩展包含了多个源文件)。
 +
 
 +
在'community'仓库中有很多流行的扩展,更多的扩展还放在了[[AUR]]。这些软件包的名字有一个'emacs-'前缀(比如emacs-lua-mode)。在很多情况下,在安装的过程会显示怎样修改~/.emacs以安装该扩展到Emacs中。
 +
 
 +
想知道怎样激活一个不在上面提到的地方的扩展,查看[http://emacswiki.org/ Emacs Wiki]中的相应页面,一般会提供一个配置的例子。Emacs Wiki也是一个寻找扩展的优秀资源。
 +
 
 +
你也可以使用[http://tromey.com/elpa/ Emacs Lisp Package Archive (ELPA)] 来自动安装软件包。打开那个网站看说明。ELPA已经包括在了Emacs24中;它已经作为Emacs生态系统中的一部分了。
  
 
== 疑难杂症 ==
 
== 疑难杂症 ==
  
 
===彩色输出的问题===
 
===彩色输出的问题===
By default, the Emacs shell will show raw escape sequences used to print colors. In other words, it will display strange symbols in place of the desired colored output.
+
Emacs默认使用原生的转义串来输出颜色。也就是说,它会在要显示颜色的地方显示奇怪的字符。
  
Including the following into {{ic|~/.emacs}} amends the problem:
+
{{ic|~/.emacs}}中加入下面的代码解决这个问题:
 
  (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
 
  (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
  
 
===菜单显示为空===
 
===菜单显示为空===
A bug exists in GNU Emacs 23.1 (using the GTK toolkit) which may cause some menus to appear empty.  This appears to be fixed in Emacs' CVS trunk.  The corresponding [http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=550541 Debian bug report] contains a workaround.
+
一些菜单显示为空,这是GNU Emacs 23.1的一个bug(使用GTK toolkit的时候)。好像在Emacs的CVS trunk中已经修复了。对应的[http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=550541 Debian bug report] 有一个应对措施。
  
 
=== X 窗口下的字符显示问题 ===
 
=== X 窗口下的字符显示问题 ===
Line 384: Line 560:
  
 
=== 启动速度慢 ===
 
=== 启动速度慢 ===
Slow startup times are often caused by one of two things.
+
启动速度慢经常是由下面两种情况引起的。
  
To determine which it might be, run Emacs with:
+
要确定是哪种情况,这样打开Emacs:
  
 
  $ emacs -q
 
  $ emacs -q
  
If Emacs still starts slowly, refer to [[Emacs#Incorrect network configuration|Incorrect network configuration]].  If not, it is almost certainly a [[Emacs#Init file loads slowly|problem in your .emacs]].
+
如果Emacs还是启动很慢,则是[[Emacs#Incorrect network configuration|错误的网络配置]]。如果不是,则可以确定是[[Emacs#Init file loads slowly|.emacs的问题]]
  
 
====错误的网络配置====
 
====错误的网络配置====
  
Mistakes, particularly in /etc/hosts, will often result in a 5+ second delay when starting Emacs.  Refer to '[[Configuring_network#Set_the_hostname|set the hostname]]' in the network configuration guide for information.
+
当启动Emacs的时候,一些错误,特别是在/etc/hosts中的,经常会导致5秒以上的延迟。在网络配置指南中查看'[[Configuring_network#Set_the_hostname|set the hostname]]' 了解更多内容。
  
 
====初始化文件加载慢====
 
====初始化文件加载慢====
  
A simple way to search for the cause is to comment-out (i.e., prefix lines with ';') suspect sections of your ~/.emacs (or ~/.emacs.d/init.el) then start Emacs again to see if there's any change.  Keep in mind use of "require" and "load" can slow the startup down, especially when used with larger extensions.  They should, as a rule, only be used when their target is either: needed once Emacs starts or provides little more than "autoloads" for an extension.  Otherwise, use the 'autoload function directly.  For example, instead of:
+
一个很简单的方法查找原因是注释掉(比如在行开头使用';')你的~/.emacs(或者~/.emacs.d/init.el)里面可疑的地方,然后再启动Emacs,看速度是否有改善。记住,使用"require""load"会减慢启动速度,特别是用在很大的插件上。一般来说,他们应该用在当目标是Emacs启动的时候就需要或者提供仅仅是一个扩展的"autoloads"。否则,直接使用'autoload'函数。比如,不是这样:
  
 
  (require 'anything)
 
  (require 'anything)
  
you might use:
+
你应该这样:
  
 
  (autoload 'anything "anything" "Select anything" t)
 
  (autoload 'anything "anything" "Select anything" t)
  
=== 不能打开文件: ... ===
+
=== 不能打开配置文件: ... ===
  
The most common cause of this error is the 'load-path' variable not including the path to the directory within which the extension is located.  To solve this, add the appropriate path to the list to be searched prior to attempting to load the extension:
+
这个错误最常见的原因是'load-path'变量没有包含某些插件的目录。要解决这个问题,在加载插件前,把需要加载的插件目录加入到要搜索的list中:
  
 
   (add-to-list 'load-path "/path/to/directory/")
 
   (add-to-list 'load-path "/path/to/directory/")
  
When attempting to use packages for extensions and Emacs has been configured with a prefix other than '/usr', the load-path will need to be updated.  Place the following in ~/.emacs prior to the instructions provided by the package:
+
当尝试使用一个插件的包,而这个包又被Emacs加上了非'/usr'的前缀时,load-path需要更新。把下面的代码放到使用这个插件的包的代码的前面:
  
 
   (add-to-list 'load-path "/usr/share/emacs/site-lisp")
 
   (add-to-list 'load-path "/usr/share/emacs/site-lisp")
  
If compiling Emacs by hand, keep in mind that the default prefix is '/usr/local'.
+
如果手动编译Emacs,记住默认的前缀是'/usr/local'。
 +
 
 +
=== Dead-accent keys problem: '<dead-acute> is undefined' ===
 +
 
 +
Searching about this bug on Google, we find this link:
 +
http://lists.gnu.org/archive/html/help-gnu-emacs/2009-05/msg00167.html
 +
 
 +
Explaining the problem: in recent versions of
 +
b72
 +
Emacs, the normal way to use accent keys doesn't work as expected. Trying to accent a word like 'fiancé' will produce the message above.
 +
 
 +
A way to solve it is just put the line above on your startup file, {{ic|~/.emacs}}:
 +
 
 +
  (require 'iso-transl)
 +
 
 +
And no, it isn't a bug, but a feature of new Emacs versions. Reading the subsequent messages about it on the mail list, we found it (http://lists.gnu.org/archive/html/help-gnu-emacs/2009-05/msg00179.html):
 +
 
 +
:''It seems that nothing is loaded automatically because there is a choice betwee iso-transl and iso-acc. Both seem to provide an input method with C-x 8 or Alt-<accent> prefix, but what you and I are doing is just pressing a dead key (^, ´, `, ~, ¨) for the accent and then another key to "compose" the accented character. And there is no Alt key used in this! And according to documentation it seems be appropriate for 8-bit encodings, so it should be pretty useless in UTF-8. I reported this bug when it was introduced, but the bug seems to be
 +
a3b
 +
classified as a feature ... Maybe it's just because the file is auto-loaded though pretty useless. ''
 +
 
 +
=== C-M-% and some other bindings do not work in emacs nox ===
 +
 
 +
This is because terminals are more limited than Xorg. Some terminals may handle more bindings than other, though. Two solutions:
 +
* either use the graphical version,
 +
* or change the binding to a supported one.
 +
 
 +
Example:
 +
{{hc|.emacs|
 +
(global-set-key (kbd "C-M-y") 'query-replace-regexp)
 +
}}
 +
 
 +
=== Emacs client gets stuck when switching back to it ===
 +
 
 +
If you are using Emacs daemon, then you should know that input is blocking. If one Emacs instance is in the minibuffer (after an '''M-x''' for instance), then all other instance will wait for it to finish. Press '''C-g''' to cancel any input to make sure this Emacs session is not blocking.
 +
 
 +
=== Emacs-nox output gets messy ===
 +
 
 +
When working in a terminal, the color, indentation, or anything related to the output might become crazy. This is (probably?) because Emacs was sent a special character at some point which may conflict with the current terminal.
 +
There is not much to be done but restarting emacs. If someone has a workaround or a more detailed explanation on the issue, feel free to contribute.
 +
 
 +
Graphical Emacs does not suffer from this issue.
 +
 
 +
=== Shift + Arrow keys not working in emacs within tmux ===
 +
 
 +
First you must enable xterm-keys in your [[tmux]] config.
 +
{{hc|.tmux.conf|
 +
setw -g xterm-keys on
 +
}}
 +
 
 +
But, this will break other key combinations. To fix them, put the following in your emacs config.
 +
{{hc|.emacs|
 +
;; handle tmux's xterm-keys
 +
;; put the following line in your ~/.tmux.conf:
 +
;;  setw -g xterm-keys on
 +
(if (getenv "TMUX")
 +
    (progn
 +
      (let ((x 2) (tkey ""))
 +
(while (<&#61; x 8)
 +
  ;; shift
 +
  (if (&#61; x 2)
 +
      (setq tkey "S-"))
 +
  ;; alt
 +
  (if (&#61; x 3)
 +
      (setq tkey "M-"))
 +
  ;; alt + shift
 +
  (if (&#61; x 4)
 +
      (setq tkey "M-S-"))
 +
  ;; ctrl
 +
  (if (&#61; x 5)
 +
      (setq tkey "C-"))
 +
  ;; ctrl + shift
 +
  (if (&#61; x 6)
 +
      (setq tkey "C-S-"))
 +
  ;; ctrl + alt
 +
  (if (&#61; x 7)
 +
      (setq tkey "C-M-"))
 +
  ;; ctrl + alt + shift
 +
  (if (&#61; x 8)
 +
      (setq tkey "C-M-S-"))
 +
 
 +
  ;; arrows
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d A" x)) (kbd (format "%s<up>" tkey)))
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d B" x)) (kbd (format "%s<down>" tkey)))
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d C" x)) (kbd (format "%s<right>" tkey)))
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d D" x)) (kbd (format "%s<left>" tkey)))
 +
  ;; home
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d H" x)) (kbd (format "%s<home>" tkey)))
 +
  ;; end
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d F" x)) (kbd (format "%s<end>" tkey)))
 +
  ;; page up
 +
  (define-key key-translation-map (kbd (format "M-[ 5 ; %d ~" x)) (kbd (format "%s<prior>" tkey)))
 +
  ;; page down
 +
  (define-key key-translation-map (kbd (format "M-[ 6 ; %d ~" x)) (kbd (format "%s<next>" tkey)))
 +
  ;; insert
 +
  (define-key key-translation-map (kbd (format "M-[ 2 ; %d ~" x)) (kbd (format "%s<delete>" tkey)))
 +
  ;; delete
 +
  (define-key key-translation-map (kbd (format "M-[ 3 ; %d ~" x)) (kbd (format "%s<delete>" tkey)))
 +
  ;; f1
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d P" x)) (kbd (format "%s<f1>" tkey)))
 +
  ;; f2
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d Q" x)) (kbd (format "%s<f2>" tkey)))
 +
  ;; f3
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d R" x)) (kbd (format "%s<f3>" tkey)))
 +
  ;; f4
 +
  (define-key key-translation-map (kbd (format "M-[ 1 ; %d S" x)) (kbd (format "%s<f4>" tkey)))
 +
  ;; f5
 +
  (define-key key-translation-map (kbd (format "M-[ 15 ; %d ~" x)) (kbd (format "%s<f5>" tkey)))
 +
  ;; f6
 +
  (define-key key-translation-map (kbd (format "M-[ 17 ; %d ~" x)) (kbd (format "%s<f6>" tkey)))
 +
  ;; f7
 +
  (define-key key-translation-map (kbd (format "M-[ 18 ; %d ~" x)) (kbd (format "%s<f7>" tkey)))
 +
  ;; f8
 +
  (define-key key-translation-map (kbd (format "M-[ 19 ; %d ~" x)) (kbd (format "%s<f8>" tkey)))
 +
  ;; f9
 +
  (define-key key-translation-map (kbd (format "M-[ 20 ; %d ~" x)) (kbd (format "%s<f9>" tkey)))
 +
  ;; f10
 +
  (define-key key-translation-map (kbd (format "M-[ 21 ; %d ~" x)) (kbd (format "%s<f10>" tkey)))
 +
  ;; f11
 +
  (define-key key-translation-map (kbd (format "M-[ 23 ; %d ~" x)) (kbd (format "%s<f11>" tkey)))
 +
  ;; f12
 +
  (define-key key-translation-map (kbd (format "M-[ 24 ; %d ~" x)) (kbd (format "%s<f12>" tkey)))
 +
  ;; f13
 +
  (define-key key-translation-map (kbd (format "M-[ 25 ; %d ~" x)) (kbd (format "%s<f13>" tkey)))
 +
  ;; f14
 +
  (define-key key-translation-map (kbd (format "M-[ 26 ; %d ~" x)) (kbd (format "%s<f14>" tkey)))
 +
  ;; f15
 +
  (define-key key-translation-map (kbd (format "M-[ 28 ; %d ~" x)) (kbd (format "%s<f15>" tkey)))
 +
  ;; f16
 +
  (define-key key-translation-map (kbd (format "M-[ 29 ; %d ~" x)) (kbd (format "%s<f16>" tkey)))
 +
  ;; f17
 +
  (define-key key-translation-map (kbd (format "M-[ 31 ; %d ~" x)) (kbd (format "%s<f17>" tkey)))
 +
  ;; f18
 +
  (define-key key-translation-map (kbd (format "M-[ 32 ; %d ~" x)) (kbd (format "%s<f18>" tkey)))
 +
  ;; f19
 +
  (define-key key-translation-map (kbd (format "M-[ 33 ; %d ~" x)) (kbd (format "%s<f19>" tkey)))
 +
  ;; f20
 +
  (define-key key-translation-map (kbd (format "M-[ 34 ; %d ~" x)) (kbd (format "%s<f20>" tkey)))
 +
 
 +
  (setq x (+ x 1))
 +
  ))
 +
      )
 +
  )
 +
}}
  
 
== 替代方案 ==
 
== 替代方案 ==
  
There are numerous implementations of Emacs. GNU/Emacs is probably the most popular. <br>
+
有很多Emacs的实现。GNU/Emacs 可能是最受欢迎的了。<br>
Lighter Emacs compatibile alternatives can be found in Arch repositories or in [https://aur.archlinux.org/ AUR].
+
更轻量的兼容性较好的Emacs可以在Arch仓库或在[https://aur.archlinux.org/ AUR]中找到。
  
 
=== mg ===
 
=== mg ===
  
mg (originally called MicroGnuEmacs) is lightweight implementation of Emacs written in C.
+
mg(原来叫MicroGnuEmacs),是用C语言实现的轻量级Emacs。
  
It's possible to install mg right away from {{ic|community}}
+
可以从{{ic|community}中安装mg
 
  # pacman -S mg
 
  # pacman -S mg
or download source from official [http://homepage.boetes.org/software/mg/ page].
+
或者从官方网站[http://homepage.boetes.org/software/mg/ page]中下载源代码。
  
 
=== zile ===
 
=== zile ===
  
According to the offical web [https://www.gnu.org/software/zile/ page] "GNU Zile is a lightweight Emacs clone. Zile is short for Zile Is Lossy Emacs. Zile has been written to be as similar as possible to Emacs; every Emacs user should feel at home.".
+
引用官方网站[https://www.gnu.org/software/zile/ page]的描述,"GNU Zile is a lightweight Emacs clone. Zile is short for Zile Is Lossy Emacs. Zile has been written to be as similar as possible to Emacs; every Emacs user should feel at home.",意思是'GNU Zile'是一个轻量级的Emacs的克隆。Zile是'Zile Is Lossy Emacs'的缩写。
 +
Zile的实现与Emacs如此相似以至于每个Emacs用户使用Zile一定会有一种宾至如归的感觉。
  
zile can be found in {{ic|extra}}
+
zile 可以在{{ic|extra}}中找到
  
 
  # pacman -S zile
 
  # pacman -S zile
  
the latest taballs can be found in official GNU [http://ftp.sh.cvut.cz/MIRRORS/gnu/pub/gnu/zile/ mirrors].
+
最新的tarball可以在GNU的官方源[http://ftp.sh.cvut.cz/MIRRORS/gnu/pub/gnu/zile/ mirrors]中找到。
  
 
=== uemacs ===
 
=== uemacs ===
  
uemacs is "Micro-emacs version customized by Linus Torvalds".
+
uemacs 是由Linus Torvalds定制的微型Emacs版本。
It can be found in [https://aur.archlinux.org/ AUR] as [https://aur.archlinux.org/packages.php?ID=31502 uemacs].
+
它可以在 [https://aur.archlinux.org/ AUR] [https://aur.archlinux.org/packages.php?ID=31502 uemacs]中找到。
  
 
== 资源 ==
 
== 资源 ==

Latest revision as of 20:57, 1 September 2015

翻译状态: 本文是英文页面 Emacs翻译,最后翻译时间:2014-12-08,点击这里可以查看翻译后英文页面的改动。

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: please use the first argument of the template to provide more detailed indications.

Emacs是一个扩展方便,定制能力强,文档丰富的动态交互编辑器。Emacs的核心构建在Emacs Lisp解释器之上,其中Emacs Lisp是大部分Emacs的内建函数和拓展模块的实现语言。Emacs可以在命令行界面下(CLI)工作良好,在图形界面系统下,使用GTK作为默认的图形界面构建工具。在文本编辑能力上,Emacs常常拿来和vim比较。

Note: 入门建议直接使用starterkit扩展。本文档实际帮助不大

安装

Emacs有众多变体发行版本(有时候称作emacsen). 最常见的莫过于 GNU Emacs,在Official repositories可以找到

$ pacman -S emacs

如果你总是在终端下工作的话,你可能会选择emacs-nox。这是一个没有GTK+依赖的版本(也没有声音支持)。

终端版Emacs存在以下问题:它支持更少的颜色和更少的字体控制功能(缺少在线控制字体大小、在一篇文档中使用多种字体等功能)。而且对于一些需要高级特性的功能比如Speedbar或者GUD(调试环境),它也不支持。另外在控制复杂的外观(face)时,emacs-nox比Emacs要慢。

如果你想体验Emacs的所有扩展功能而不用装一堆依赖的话,你可以使用PKGBUILD来按你的需求定制Emacs。不使用gtk3可以让Emacs避免使用gconf。图像和声音的支持也可以去除。在Emacs的源代码目录下运行./configure --help可以看看有哪些配置选项。

PKGBUILD
# ...
  ./configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib \
    --localstatedir=/var --with-x-toolkit=gtk2 --with-xft \
    --without-gconf --without-sound
# ...

另外一个常见的变体就是xemacs.

快速入门

一般印象是Emacs十分复杂,学习曲线陡峭,但很多资深学习者并不这样认为,反而认为其非常易懂和可定制。因为其源码和配置文件语义化程度较高。简单了解下自定义和高扩展带来的好处花不了多少时间。何况还有很多成熟的功能拓展模块,很方便添加,可以让Emacs为任何文本编辑的需求配置强大的环境。

Emacs自带一个入门教程,你可以点击欢迎界面上的第一个链接来打开它; 或者从菜单栏中选择Help->Emacs Tutorial,或者按'F1'键然后按't'. 我们设计这篇文章来为你在Emacs入门学习中提供额外的资源。

Emacs也包括一系列引用链接,既有适合初学者的内容,也有骨灰级玩家所喜爱的.参见/usr/share/emacs/<version>/etc/refcards/ (将<version>换成你的emacs版本).

运行Emacs

在启动Emacs之前,你要先学会怎么关闭它(尤其是你要在终端里运行它的话):用Ctrl+xCtrl+c来关闭它。

图形界面下打开方式

图形界面下可以直接点击图标打开。

虚拟终端下的常见方式

打开Emacs:

$ emacs

不打开图形界面,直接在终端中运行:

$ emacs -nw

也可以直接用下面的命令来打开一个文件:

$ emacs filename.txt
去掉颜色

默认的emacs会带有颜色主题,如果不需要,可以关闭之:

$ emacs -nw --color=no


作为守护进程

Emacs由于每次启动都需要加载大量自定义的配置文件,所以打开时候会有点慢。从Emacs23开始, Emacs可以以守护进程的形式运行,这样每个用户都可以链接到Emacs。以守护进程运行Emacs:

$ emacs --daemon

你可能在启动时打开一个守护进程,然后再将守护进程链接到窗口。另外,也可以将图形和终端客户端同时链接到守护进程上,这样启动图形界面速度就很快了。

如果你仅仅想链接到守护进程,用下面的命令(注意,在桌面环境下这个命令会打开一个图形客户端,而在像tty这种命令行下,它会打开一个命令行版的emacs):

$ emacsclient

如果你想在桌面环境下打开一个命令行版的emacs,使用下面的命令:

$ emacsclient -t

另外,你可以在后面加上 -a "" 参数. 现在,你第一次使用这个命令时,它会把emacs作为守护进程来启动,它会留在后台以加快以后的启动速度(也会记住缓冲区).

更聪明点,你可以在.bashrc中加上下面的别名:

alias em='emacsclient -t -a ""'  #在终端中开启emacs
alias emc='emacsclient -nc -a ""'  #启动emacs图形界面
EDITOR='emacsclient -a ""'

xfce桌面环境中,如果你想使用 emacsclient -c 来代替 emacs %f 打开一个新文件, 你可以修改你的 /usr/share/applications/emacs.desktop 文件,把下面这一行

$ Exec=emacs %f

修改为

$ Exec=emacsclient -c

使用这种方法,每次你打开一个文件时就只会启动客户端,因此速度非常快!

基本术语和约定

Emacs使用一些刚开始看起来很奇怪的术语和约定,我们会在合适的时候介绍。但是,对于部分术语,我们必须要在前面介绍,因为它们对于使用Emacs来说是非常基础的。

第一个要介绍的术语是缓冲区的概念。一个缓冲区就是Emacs中的数据的一种表示方式,比如,当使用Emacs打开一个文件时,这个文件从磁盘中被读出来,它的内容被存储在了缓冲区里面,它的内容可以在这个缓冲区里面被编辑并且可以重新写进磁盘中。缓冲区中的内容不仅仅可以是文本,也可以是图片和widget。现在,让缓冲区可以显示应用程序的工作正在进行!换个角度思考,在磁盘中数据是以文件形式保存的,而在Emacs中,数据是以缓冲区的形式存在的。

在Emacs中,对于按键组合的约定你可能很陌生。比如:

C-x 代表 Ctrl-x

M-x 代表 Meta-x

注意: 'Meta'一般代表Alt键,也可以用Esc键替代。

举个例子,退出Emacs使用下面的按键组合C-x C-c。这个可以读做,"按住Ctrl键再按'x',释放,再按住Ctrl键再按'c'。虽然Emacs提供了一个菜单栏,但是强烈建议学习使用按键组合。这个指南将参考Emacs的按键绑定的约定。

移动

光标移动和其它图形编辑器非常类似,鼠标和方向键可以用来改变光标(在Emacs中称为)的位置。在Emacs中,方向键代表的标准移动命令也有其它辅助的绑定。向前(forward)移动一个字符,使用 C-f,向后(back)移动一个字符,使用C-bC-nC-p 分别用于移动到下(next)一行和移动到上(previous)一行。再声明一下,强烈推荐使用组合键而不是使用方向键和鼠标。

可以想像,Emacs提供了更多的光标高级移动命令,包括移动一个单词和一个句子。 M-f 表示光标向前移动一个单词, M-b 表示向后移动一个单词。类似地,M-e 把光标移动到一个句子的末尾(end), M-a 移动到句子的开头。

直到现在,所有的移动命令都是和光标有关的。M-< 表示把光标移动到缓冲区的开头,和它相反的是 M->, 把光标移动到缓冲区的末尾。要把光标移动到某一特定行,使用M-g g. M-g g 会提示输入行号。同样,要移动到一行的开头或者结尾,分别使用C-aC-e

Note: 这些命令(实际上是全部命令)的绑定,在不同的模式(mode)中,稍微会有不同。然而,覆盖的命令提供不同的功能这种情况很少见。更多信息请看Modes

文件和缓冲区

Emacs 提供了一系列命令来对文件操作,其中最常用的会在这里详细说明。C-x C-f 用来打开一个文件(在Emacs中叫做'查找文件')。如果指定的文件不存在,Emacs会打开一个空的缓冲区。保存一个缓冲区会创建一个包含缓冲区内容的文件。C-x C-s 就是用来保存缓冲区的。要保存一个文件名不一样的缓冲区,使用C-x C-w (这其实是'write-file'这条命令的助记符), 它会在写入磁盘之前提示输入新文件名。也可以使用C-x s来保存所有的缓冲区, 如果某个缓冲区在上次保存之后被修改了,则会提示进行哪项操作。

Note: 如果指向某个文件的缓冲区还在打开的话,C-x C-f 是不会重新从磁盘中读取文件的。要从磁盘中重新读取文件,先使用C-x k关掉缓冲区,再使用C-x C-f打开文件,或者使用M-x revert-buffer.

很多互动的命令,比如"find-file" 或者 "write-file" 会在Emacs窗口的底部栏提示输入。这栏称为minibuffer。和很多*nix shell一样,minibuffer支持很多基本的操作和TAB补全。按两下<TAB>可以显示一个补全的选项列表,并且,如果你喜欢,可以用鼠标从列表中选择。minibuffer的补全在很多输入(包括命令和文件名)中都可以用。

minibuffer也提供一个记住历史的特性。通过Up Arrow 或者 C-p可以取得这条命令的上一个条目.

要在任意时刻退出minibuffer,使用C-g.

打开几个文件后,切换缓冲区是非常必要的。打开一个指向那个缓冲区的文件可以切换到那个缓冲区。但是这不是最高效的方法。Emacs提供C-x b来提示要显示的新缓冲区(这里可以使用TAB补全)。输入一个不存在的缓冲区,则会新建一个空的缓冲区。

Note: 要切换到上一个缓冲区,使用C-x b <RET>, 因为上一个缓冲区是默认的缓冲区。

使用C-x C-b可以显示所有打开缓冲区的列表。如果某个缓冲区不需要使用了,使用C-x k来关掉它。

编辑

Emacs 内建有很多编辑命令。可能最重要的还没有介绍的是'undo',它的快捷键为 C-_ 或者 C-/ .移动光标的命令通常都有对应的删除字符的命令。例如, M-<backspace> 可以用来删除一个光标后的词,M-d可以用来删除光标前面的一个词。删除光标至行尾或者句尾的字符可以分别用C-k 或者 M-k

通常我们都约定一行不能超过80个字符。这是为了代码的可读性,尤其是一行中的字符可能会接触到窗口边缘。在Emacs,自动地插入或者删除换行符称为filling。我们可以用 M-q 重整当前的段落(重新分配换行符,删除段落中多余的空格和tab键)

字符和单词可以分别通过 C-tM-t 进行交换。比如: Hello World!World! Hello

单词的大小写也可以调整。M-l 把光标处的单词变成小写 (HELLOhello); M-u 把光标处的单词变成大写(helloHELLO), M-c 把光标处的单词的第一个字母变成大写,并把后面的变成小写(hElLoHello).

移除,召回和区域

一个区域(region)是指在两个位置之间的一段字。其中一个位置被称为标记(mark),另一个是光标。C-<SPC>用来设置标记的位置,紧接着就可以通过移动光标来创造一个区域。在GNU Emacs 23.1及以后的版本中,这个区域默认是可见的。有许多命令是针对区域的,其中最常用的就是kill命令。

在Emacs中,剪切和粘贴分别对应的命令叫做killyank。许多删除多个字符的命令(包括上面提到的C-kM-d命令)实际上是把文字剪切下来,附加到一个叫kill-ring的地方。kill-ring 就是一个被删掉的文字的列表。在默认情况下,kill-ring会保存最多60次删除记录。连续的删除会连在一起。

C-wM-w 可以用来删除或复制一个区域。

要想把删掉的文字插入进来(也就是'yanking'),可以用C-y命令。C-y 可以连续用好多次来重复插入。按刚刚所讲,之前删除的文字会排成一个列表,但是C-y只能获取列表中的第一个,也就是最后删掉的一个。再之前删掉的可以通过M-y来获取。它会把C-y刚刚插入的文字用再早删除的文字替换掉。M-y 必须紧跟着C-y命令执行,可以执行很多次以遍历kill-ring。

查找和替换

在文本编辑中进行字符串查找是很常见的任务。按C-s可以正向搜索,按C-r‘’‘则可以反向搜索。这些命令提示要搜索的字符串。搜索是增量的,你再次按键时会显示下一个匹配的位置,并且可以使用C-s’‘’向前或者使用C-r‘’‘向后。找到匹配结果之后,可以按下<RET>结束搜索。另外,如果你想回到发起搜索的位置,可以使用C-g

一旦搜索完成(不是由C-g之类的指令引起),当前搜索的字符串将会作为下一次搜索的默认参数。使用’‘’C-s C-s或者C-r C-r可以达到这样的效果。

I-search 有一些有用的命令,使用M-e来编辑当且搜索区域,使用M-c来触发大小写敏感匹配。

正则表达式搜索除了指令不同之外,其他方面和以上都相同。使用C-M-s或者C-M-r来使用正则表达式搜索。相应地,用C-sC-r可以前后移动,和普通的搜索没什么两样。

除了搜索之外,进行字符串替换也是有必要的。原始文本和替换文本都会有提示,替换的时候也会有提示。尽管有很多选项提供(按?可以了解完整的选项),最常用的还是y(进行替换),n(跳过),以及!(替换当前及之后所有匹配)。

缩进和前缀参数

Indentation is usually performed with either <TAB>, to indent a single line, or with C-M-\, to indent a region.

Exactly how text is indented usually depends on the major-mode which is active. Major-modes often define indentation styles specialising in indenting a certain type of text. (See Modes for more information.)

In some cases, a suitable major-mode may not exist for a file type, in which case, manual indentation may be necessary. Create a region (see Killing, yanking and regions) then perform indentation with C-u <n> C-x <TAB> (where '<n>' is the number of columns which the text within the region should be indented). For example:

Increase the region's indentation by four columns:

C-u 4 C-x <TAB>

Decrease the region's indentation by two columns.

C-u -2 C-x <TAB>
Note: The trick behind this is C-u, which corresponds to the 'universal-argument' command. Providing a 'universal-argument' is a way to provide more information to a command (this information is referred to as a 'prefix argument'). In this case, we provided the amount of indentation desired to the command invoked by C-x <TAB>. Without providing an argument, C-x <TAB> will only increase indentation by 1 column.

窗口和外框架

Emacs的设计是可以同时方便地编辑多个文件。这是通过把Emacs的接口分成三个层次来实现的,即buffer(之前介绍过了),windowframe

window 是显示buffer的一个viewport(社区)。一个window一次只能显示一个buffer。但是一个buffer可以在多个window中显示。在窗口下面有一个mode-line,它用于显示当前buffer的信息。

frame 是Emacs的一个"窗口"(这是标准的术语。比如,'窗口'是现代桌面的称谓),它包含了标题栏,菜单栏,还有一个或多个'window'(这是Emacs的术语,比如上面提到的'window')。

从现在起,这些Emacs中存在的名词的定义就可以使用了。

要把window水平分隔和垂直分隔,分别使用C-x 2C-x 3。这种效果其实就是在当前frame中开户另外一个window。要在多个window中循环切换,使用C-x o

与分隔一个window相反的是删除window。要删掉当前window,使用C-x 0,要删掉除了当前window之外的其它window,使用C-x 1

和window一样,创建和删除frame也是可以的。C-x 5 2会创建一个frame,C-x 5 0会删除当前的frame,C-x 5 1会删除除了当前frame之外的其它frame。

注意: 这些命令不影响buffer。比如删除一个window并不会删除在window中显示的buffer。

获得帮助

Emacs在设计的时候就自文档化了。比如,要查看一个命令的名字或者它的键的绑定,Emacs提供了很多帮助信息。下面是列出来的最有用的一些帮助命令:

C-h t        启动Emacs官方教程

C-h b        列出来所有的有效键绑定

C-h k        查找一个键被绑定在了哪个命令上

C-h w        查找一个命令被绑定在了哪些键上

C-h a        查找一个匹配一段描述的命令

C-h m        显示当前激活的所有模式的信息

C-h f        显示给定函数的描述信息

模式

一个Emacs模式是用Emacs Lisp语言写的一个控制相关缓冲区行为的扩展。一般来说,它给编辑文本提供缩进,语法高亮和键绑定的功能。复杂的模式可以把Emacs变成一个完美的IDE (Integrated Development Environment). Emacs 一般会使用一个文件的扩展来确定应该加载那个模式。

一些编写shell脚本比较有用的模式有sh-mode, line-number-mode 和 column-number-mode。它们可以并行地通过下面的方式激活:

M-x sh-mode <RET>

M-x column-number-mode <RET>

line-number-mode 默认是激活的,它可以通过下面的命令来打开/关闭:

M-x line-number-mode <RET>

sh-mode 是一个 major-mode. Major-modes 调整Emacs,并且经常提供了一些特定的命令来编辑某种类型的文本。一个缓冲区只能激活一种major-mode。除了支持语法高亮和缩进,sh-mode还定义了几条命令来帮助快速开发shell脚本。下面是其中的几条:

C-c (	 插入一个函数定义

C-c C-f	 插入一个for循环

C-c TAB	 插入一条if语句

C-c C-w	 插入一个while循环

C-c C-l	 插入一个从1到n的有下标的循环

'line-number-mode' 和 'column-number-mode' 是 minor-modes. Minor-modes 可以用来扩充major-mode的功能,多个minor-mode可以同时激活。

提示和技巧

前面的部分给出了基本编辑命令的概述,没有给出Emacs的一个指示。这个部分讲述一些高级的技巧和功能。

TRAMP

TRAMP (Transparent Remote Access, Multiple Protocols) ,顾名思义,是一个可以通过很多协议透明访问远程文件的一个扩展。当提示输入一个文件名,输入特定的格式就可以使用TRAMPP。比如:

在打开/etc/hosts文件之前提示输入root的密码以获取root权限:

C-x C-f /su::/etc/hosts

要通过SSH使用'myuser'用户名登录'myhost'主机并打开文件~/example.txt

C-x C-f /ssh:myuser@myhost:~/example.txt

TRAMP的路径一般是这种格式'/[protocol]:[[user@]host]:<file>'。TRAMP支持的不只上面的两个简单例子。请查看Emacs里面的TRAMPP info手册了解更多的信息。

键盘宏和寄存器

这一部分会提供一个实用的指南来使用一些更强大的编辑特性。也就是“键盘宏”和“寄存器”。

我们的目标是产生一个字符列表和它们在这个列表中对应的位置。虽然这可以通过手工格式化来完成,但是这样会很慢而且容易出错。如果我们采用一些Emacs更高级的编辑功能却可以起到四两拨千斤的功效。在介绍这个方法之前,需要先了解一些技术背后的细节。

要介绍的第一个特性就是寄存器。寄存器的功能是用来保存和获取各种各样的数据。每个寄存器用一个字母来命名,这个字母就是用来调用这个寄存器的。

另一个要介绍的就是键盘宏。一个键盘宏存储了一个命令序列以便以后可以重复使用。下面就一步一步地讲解这个方法。

首先我们从一个包含如下字符的缓冲区开始:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

通过`number-to-register' 命令 (C-x r n)我们来准备一个寄存器,把数字'0'存储在寄存器'k'中:

C-x r n k

当光标在缓冲区开头的时候,开始录制键盘宏 (C-x () 然后开始对字符串进行格式化:

C-x ( C-f M-4 .

插入 (C-x r i) 然后将寄存器'k'加1 (C-x r +) 。开头的 (C-u) 命令是用来在插入文字之后让光标移到插入的文字后面:

C-u C-x r i k C-x r + k

最后我们来插入一个回车来结束格式化。Emacs可以重复以上过程,从我们定义键盘宏的位置开始,直到最后一个字符。C-x e命令停止宏的录制并开始执行这段宏。开头的 M-0 命令是用来让宏在出错的时候停下来,这样在它走到这行的结尾就会停下来。

<RET> M-0 C-x e

下面是结果:

 A....0
 B....1
 C....2
 [...]
 x....49
 y....50
 z....51

正则表达式

From the Emacs Manual: "A regular expression, or regexp for short, is a pattern that denotes a (possibly infinite) set of strings." This section will not go into any detail regarding regular expressions themselves (as there is simply too much to cover). It will however provide a quick demonstration of their power. See Regular Expressions section in the Emacs Manual for further reading.

Given the same scenario presented above: A list of characters which are to be formatted to represent their respective position in the list. (see Keyboard macros and registers). Again, starting with a buffer containing.

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

At the beginning of the buffer, use C-M-% (if the key-sequence is difficult to perform, it may be more comfortable to use M-x query-replace-regexp). At the prompt:

\(.\)

which simply matches one character. Then, when prompted for the replacement:

\1....\#^J
Note: '^J' represents where a newline should be placed, it should not be entered into the prompt. The newline must instead be inserted literally using C-q C-j.

The replacement expression reads: "Insert the matched text between the first set of parentheses (in this case, a single character), followed by 4 periods then insert an automatically incremented number followed by a newline.

Finally, press ! to apply this across the entire buffer. All of the formatting that was performed in the previous section was performed with a single regexp replacement.

定制

Emacs能通过~/.emacs或者M-x customize来定制。本段落将着眼于手动定制 ~/.emacs,并且提供了一些常用配置的样例。配置命令提供了一个适应的途径,通过它你能够渐渐熟悉Emacs。

这里的所有例子都能在Emacs中奏效。比如,在Emacs中计算表达式: C-M-x 同时指到任何需要求值的地方。

或者

C-x C-e 指到最后的“)”。

用'y'和'n'来代替频繁地输入'yes’和'no‘是一个好的方法:

(defalias 'yes-or-no-p 'y-or-n-p)

取消光标闪烁:

(blink-cursor-mode -1)

类似地,开启上一节提到的列号模式:

(column-number-mode 1)

它们两者之间的相似不是偶然:光标闪烁模式和列号模式都是'副参模式',按照规则,'副参模式’能通过正负值来设置。如果参数省略,'副参模式'将被开/关。

这里有其他一些'副参模式'的例子,下面的参数将会关闭滚动栏、菜单栏、工具栏。

(scroll-bar-mode -1)
(menu-bar-mode -1)
(tool-bar-mode -1)

变量'auto-mode-alist'修改之后能够改变默认的“主参模式”。下面的例子把'.tut'和'.req'修改成了'.text-mode'。

(setq auto-mode-alist
  (append
    '(("\\.tut$" . text-mode)
      ("\\.req$" . text-mode))
    auto-mode-alist))

Settings can also be applied on a per-mode basis. A common method for this is to add a function to a hook. For example, to force indentation to use spaces instead of tabs, but only in text-mode:

(add-hook 'text-mode-hook (lambda () (setq indent-tabs-mode nil)))

Similarly, to only use spaces for indentation everywhere:

(setq-default indent-tabs-mode nil)

Keybindings can be adjusted in two ways. The first of which is 'define-key'. 'define-key' creates a keybinding for a command but only in one mode. The example below will make F8 delete any whitespace from the end of each line of a 'text-mode' buffer:

(define-key text-mode-map (kbd "<f8>") 'delete-trailing-whitespace)

The other method is 'global-set-key'. This is used to bind a key to a command everywhere. To bind 'query-replace-regexp' (C-M-%) to '<f7>'.

(global-set-key (kbd "<f7>") 'query-replace-regexp)

Binding a command to an alternate key does not replace any existing bindings. Which is to say, 'query-replace-regexp' would be bound to both F7 and C-M-% after the above example.

Almost anything within Emacs can be configured. Browsing through the Emacs Wiki should give a solid place to start.


多种配置

你可以使用少量配置然后告诉Emacs来加载其他配置。

例如,我们来定义两个配置文件。

.emacs
(load "~/.emacs.d/main" nil t)
(load "~/.emacs.d/functions" nil t)
(load "~/.emacs.d/modes" nil t)
(load "~/.emacs.d/plugins" nil t)
(load "~/.emacs.d/theme" nil t)

这是我们在后台载入的完整配置。但是plugins文件太大导致载入太慢,如果我们要打开一个新的Emacs窗口,可能就不会使用plugins配置,每次加载它实在是太笨重了。

.emacs-light
(load "~/.emacs.d/main" nil t)
(load "~/.emacs.d/functions" nil t)
(load "~/.emacs.d/modes" nil t)
(load "~/.emacs.d/theme" nil t)

现在我们这样来加载Emacs:

emacs -q -l ~/.emacs-light

你可以为这个命令创建一个别名。

加载扩展程序

你能用require来加载扩展程序,例如这样:

(require 'mediawiki)

如果你试着在一个买有安装mediawiki的机器上使用相同的配置,Emacs会提示错误。并且,所有制定的扩展代码都会失效。

一个小的技巧是测试require的返回值。

(if (require 'mediawiki nil t)
    (progn
      (setq mediawiki-site-alist '(
             ("ArchLinux" "https://wiki.archlinux.org/" "UserName" "" "Main Page")
             )
           )
      (setq mediawiki-mode-hook
            (lambda ()
              (visual-line-mode 1)
              (turn-off-auto-fill)
              ))
 ))

Local and custom variables

You can define variables in your configuration file that can be later one modified locally for a file.

(defcustom my-compiler "gcc" "Some documentation")

Now in any file you can define local variables in two ways:

  • On the very first line, write
// -*- my-compiler:g++; mode:c++ -*-
  • If you cannot (or do not want to) write this on the first line, you can put it at the end:
// Local Variables:
// my-compiler: g++
// mode: c++
// End:

Note that the beginning characters need to be comments for the current language, that's why here we used two backslashes for C++. For Elisp you would use

;; -*- mode:emacs-lisp -*-

There is two functions that may help you in defining the variables: add-file-local-variable and add-file-local-variable-prop-line.

Finally, custom variable are considered insecure by default. If you try to open a file that contains local variable redefining insecure custom variables, Emacs will ask you for confirmation.

If you know what you are doing, you can declare the variable as secure, thus removing the Emacs prompt for confirmation. You need to specify a predicate that any new value has to verify so that it can be considered safe.

(defcustom my-compiler "gcc" "Some documentation" :safe 'stringp)

In the previous example, if you attempt to set anything else than a string, Emacs will consider it insecure.

Custom colors and theme

Colors can be easily customized using the face facility.

(set-face-background  'region                 "color-17")
(set-face-foreground  'region                 "white")
(set-face-bold-p      'font-lock-builtin-face t ) 

You can have let Emacs tell you the name of the face where the point is. Use the customize-face function for that. The facility will show you how to set colors, bold, underline, etc.

Emacs in console can handle 256 colors, but you will have to use an appropriate terminal for that. For instance URxvt has support for 256 colors. You can use the list-colors-display for a comprehensive list of supported colors. This is highly terminal-dependent.

SyncTeX support

Emacs is definitely one of the most powerful LaTeX editor. This is mostly due to the fact you can adapt or create a LaTeX mode to fit your needs best.

Still, there might be some challenges, like SyncTeX support. First you need to make sure your TeX distribution has it. If you installed TeX Live manually, you may need to install the synctex package.

# umask 022 && tlmgr install synctex

SyncTeX support is viewer-dependent. Here we will use Zathura as an example, so the code needs to be adapted if you want to use another PDF viewer.

(defcustom tex-my-viewer "zathura --fork -s -x \"emacsclient --eval '(progn (switch-to-buffer  (file-name-nondirectory \"'\"'\"%{input}\"'\"'\")) (goto-line %{line}))'\"" 
  "PDF Viewer for TeX documents. You may want to fork the viewer
so that it detects when the same document is launched twice, and
persists when Emacs gets closed.

Simple command:

  zathura --fork

We can use

  emacsclient --eval '(progn (switch-to-buffer  (file-name-nondirectory \"%{input}\")) (goto-line %{line}))'

to reverse-search a pdf using SyncTeX. Note that the quotes and double-quotes matter and must be escaped appropriately."
:safe 'stringp)

Here we define our custom variable. If you are using AucTeX or Emacs default LaTeX-mode, you will have to set the viewer accordingly.

Now open a LaTeX source file with Emacs, compile the document, and launch the viewer. Zathura will spawn. If you press Ctrl+Left click Emacs should place the point at the corresponding position.

Documentation

You may find yourself overwhelmed by the amount of Emacs features. You may find it difficult to know how to use Emacs Lisp to customize your favorite modes, or even to create your own modes / packages. Thankfully Emacs takes a strong point to auto-documenting everything: its internals, current configuration, bindings, etc. Almost everything is documented.

Contextual help

Emacs is self-documenting by design. As such, a great deal of information is available to determine the name of a specific command or its keybinding, for example. The following is a listing of some of the most helpful of these:

C-h a        Find a command matching a description.
C-h b        List all active keybindings.
C-h f        Describe the given function.
C-h k        Find which command a key is bound to.
C-h m        Display information regarding the currently active modes.
C-h t        Start the Emacs tutorial.
C-h v        Describe the given variable.
C-h w        Find which key(s) a command is bound to.

The manuals

If you really want to master Emacs, the most recommanded source of documentation remains the official manuals:

  • Emacs: the complete Emacs user manual.
  • Emacs FAQ.
  • Emacs Lisp Intro: if you never used any programming language before.
  • Elisp: if you are already familiar with a programming language.

You can access it as PDFs from GNU.org or directly from Emacs itself thanks to the embedded 'info' reader: C-h i. Press m to choose a book.

Some users prefer to read books using 'info' because of its convenient shortcuts, its paragraphs adapting to window width and the font adapted to current screen resolution. Some find it less irritating to the eyes. Finally you can easily copy content from the book to any Emacs buffer, and you can even execute Lisp code snippets directly from the examples.

You may want to read the Info book to know more about it: C-h i m info <RET>. Press ? while in info mode for a quick list of shortcuts.

拓展模块

虽然Emacs包含了成百上千种模式(mode),库和其它扩展,还有更多的扩展来增强Emacs。大多数扩展会详细说明安装它的时候要对~/.emacs作什么改动。这些说明一般会在一个elisp源文件开头的注释中,或者在一个README中(如果这个扩展包含了多个源文件)。

在'community'仓库中有很多流行的扩展,更多的扩展还放在了AUR。这些软件包的名字有一个'emacs-'前缀(比如emacs-lua-mode)。在很多情况下,在安装的过程会显示怎样修改~/.emacs以安装该扩展到Emacs中。

想知道怎样激活一个不在上面提到的地方的扩展,查看Emacs Wiki中的相应页面,一般会提供一个配置的例子。Emacs Wiki也是一个寻找扩展的优秀资源。

你也可以使用Emacs Lisp Package Archive (ELPA) 来自动安装软件包。打开那个网站看说明。ELPA已经包括在了Emacs24中;它已经作为Emacs生态系统中的一部分了。

疑难杂症

彩色输出的问题

Emacs默认使用原生的转义串来输出颜色。也就是说,它会在要显示颜色的地方显示奇怪的字符。

~/.emacs中加入下面的代码解决这个问题:

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

菜单显示为空

一些菜单显示为空,这是GNU Emacs 23.1的一个bug(使用GTK toolkit的时候)。好像在Emacs的CVS trunk中已经修复了。对应的Debian bug report 有一个应对措施。

X 窗口下的字符显示问题

当你使用X窗口启动emacs时,如果发现主窗口中的所有字符都是黑框白块(就像你没有安装正确的字体看到的字符一样),那么你需要安装 xorg-fonts-75dpi 或者 xorg-fonts-100dpi 并且重启X窗口。

启动速度慢

启动速度慢经常是由下面两种情况引起的。

要确定是哪种情况,这样打开Emacs:

$ emacs -q

如果Emacs还是启动很慢,则是错误的网络配置。如果不是,则可以确定是.emacs的问题

错误的网络配置

当启动Emacs的时候,一些错误,特别是在/etc/hosts中的,经常会导致5秒以上的延迟。在网络配置指南中查看'set the hostname' 了解更多内容。

初始化文件加载慢

一个很简单的方法查找原因是注释掉(比如在行开头使用';')你的~/.emacs(或者~/.emacs.d/init.el)里面可疑的地方,然后再启动Emacs,看速度是否有改善。记住,使用"require"和"load"会减慢启动速度,特别是用在很大的插件上。一般来说,他们应该用在当目标是Emacs启动的时候就需要或者提供仅仅是一个扩展的"autoloads"。否则,直接使用'autoload'函数。比如,不是这样:

(require 'anything)

你应该这样:

(autoload 'anything "anything" "Select anything" t)

不能打开配置文件: ...

这个错误最常见的原因是'load-path'变量没有包含某些插件的目录。要解决这个问题,在加载插件前,把需要加载的插件目录加入到要搜索的list中:

 (add-to-list 'load-path "/path/to/directory/")

当尝试使用一个插件的包,而这个包又被Emacs加上了非'/usr'的前缀时,load-path需要更新。把下面的代码放到使用这个插件的包的代码的前面:

 (add-to-list 'load-path "/usr/share/emacs/site-lisp")

如果手动编译Emacs,记住默认的前缀是'/usr/local'。

Dead-accent keys problem: '<dead-acute> is undefined'

Searching about this bug on Google, we find this link: http://lists.gnu.org/archive/html/help-gnu-emacs/2009-05/msg00167.html

Explaining the problem: in recent versions of b72

Emacs, the normal way to use accent keys doesn't work as expected. Trying to accent a word like 'fiancé' will produce the message above.

A way to solve it is just put the line above on your startup file, ~/.emacs:

  (require 'iso-transl)

And no, it isn't a bug, but a feature of new Emacs versions. Reading the subsequent messages about it on the mail list, we found it (http://lists.gnu.org/archive/html/help-gnu-emacs/2009-05/msg00179.html):

It seems that nothing is loaded automatically because there is a choice betwee iso-transl and iso-acc. Both seem to provide an input method with C-x 8 or Alt-<accent> prefix, but what you and I are doing is just pressing a dead key (^, ´, `, ~, ¨) for the accent and then another key to "compose" the accented character. And there is no Alt key used in this! And according to documentation it seems be appropriate for 8-bit encodings, so it should be pretty useless in UTF-8. I reported this bug when it was introduced, but the bug seems to be

a3b

classified as a feature ... Maybe it's just because the file is auto-loaded though pretty useless. 

C-M-% and some other bindings do not work in emacs nox

This is because terminals are more limited than Xorg. Some terminals may handle more bindings than other, though. Two solutions:

  • either use the graphical version,
  • or change the binding to a supported one.

Example:

.emacs
(global-set-key (kbd "C-M-y") 'query-replace-regexp)

Emacs client gets stuck when switching back to it

If you are using Emacs daemon, then you should know that input is blocking. If one Emacs instance is in the minibuffer (after an M-x for instance), then all other instance will wait for it to finish. Press C-g to cancel any input to make sure this Emacs session is not blocking.

Emacs-nox output gets messy

When working in a terminal, the color, indentation, or anything related to the output might become crazy. This is (probably?) because Emacs was sent a special character at some point which may conflict with the current terminal. There is not much to be done but restarting emacs. If someone has a workaround or a more detailed explanation on the issue, feel free to contribute.

Graphical Emacs does not suffer from this issue.

Shift + Arrow keys not working in emacs within tmux

First you must enable xterm-keys in your tmux config.

.tmux.conf
setw -g xterm-keys on

But, this will break other key combinations. To fix them, put the following in your emacs config.

.emacs
;; handle tmux's xterm-keys
;; put the following line in your ~/.tmux.conf:
;;   setw -g xterm-keys on
(if (getenv "TMUX")
    (progn
      (let ((x 2) (tkey ""))
	(while (<= x 8)
	  ;; shift
	  (if (= x 2)
	      (setq tkey "S-"))
	  ;; alt
	  (if (= x 3)
	      (setq tkey "M-"))
	  ;; alt + shift
	  (if (= x 4)
	      (setq tkey "M-S-"))
	  ;; ctrl
	  (if (= x 5)
	      (setq tkey "C-"))
	  ;; ctrl + shift
	  (if (= x 6)
	      (setq tkey "C-S-"))
	  ;; ctrl + alt
	  (if (= x 7)
	      (setq tkey "C-M-"))
	  ;; ctrl + alt + shift
	  (if (= x 8)
	      (setq tkey "C-M-S-"))

	  ;; arrows
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d A" x)) (kbd (format "%s<up>" tkey)))
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d B" x)) (kbd (format "%s<down>" tkey)))
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d C" x)) (kbd (format "%s<right>" tkey)))
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d D" x)) (kbd (format "%s<left>" tkey)))
	  ;; home
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d H" x)) (kbd (format "%s<home>" tkey)))
	  ;; end
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d F" x)) (kbd (format "%s<end>" tkey)))
	  ;; page up
	  (define-key key-translation-map (kbd (format "M-[ 5 ; %d ~" x)) (kbd (format "%s<prior>" tkey)))
	  ;; page down
	  (define-key key-translation-map (kbd (format "M-[ 6 ; %d ~" x)) (kbd (format "%s<next>" tkey)))
	  ;; insert
	  (define-key key-translation-map (kbd (format "M-[ 2 ; %d ~" x)) (kbd (format "%s<delete>" tkey)))
	  ;; delete
	  (define-key key-translation-map (kbd (format "M-[ 3 ; %d ~" x)) (kbd (format "%s<delete>" tkey)))
	  ;; f1
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d P" x)) (kbd (format "%s<f1>" tkey)))
	  ;; f2
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d Q" x)) (kbd (format "%s<f2>" tkey)))
	  ;; f3
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d R" x)) (kbd (format "%s<f3>" tkey)))
	  ;; f4
	  (define-key key-translation-map (kbd (format "M-[ 1 ; %d S" x)) (kbd (format "%s<f4>" tkey)))
	  ;; f5
	  (define-key key-translation-map (kbd (format "M-[ 15 ; %d ~" x)) (kbd (format "%s<f5>" tkey)))
	  ;; f6
	  (define-key key-translation-map (kbd (format "M-[ 17 ; %d ~" x)) (kbd (format "%s<f6>" tkey)))
	  ;; f7
	  (define-key key-translation-map (kbd (format "M-[ 18 ; %d ~" x)) (kbd (format "%s<f7>" tkey)))
	  ;; f8
	  (define-key key-translation-map (kbd (format "M-[ 19 ; %d ~" x)) (kbd (format "%s<f8>" tkey)))
	  ;; f9
	  (define-key key-translation-map (kbd (format "M-[ 20 ; %d ~" x)) (kbd (format "%s<f9>" tkey)))
	  ;; f10
	  (define-key key-translation-map (kbd (format "M-[ 21 ; %d ~" x)) (kbd (format "%s<f10>" tkey)))
	  ;; f11
	  (define-key key-translation-map (kbd (format "M-[ 23 ; %d ~" x)) (kbd (format "%s<f11>" tkey)))
	  ;; f12
	  (define-key key-translation-map (kbd (format "M-[ 24 ; %d ~" x)) (kbd (format "%s<f12>" tkey)))
	  ;; f13
	  (define-key key-translation-map (kbd (format "M-[ 25 ; %d ~" x)) (kbd (format "%s<f13>" tkey)))
	  ;; f14
	  (define-key key-translation-map (kbd (format "M-[ 26 ; %d ~" x)) (kbd (format "%s<f14>" tkey)))
	  ;; f15
	  (define-key key-translation-map (kbd (format "M-[ 28 ; %d ~" x)) (kbd (format "%s<f15>" tkey)))
	  ;; f16
	  (define-key key-translation-map (kbd (format "M-[ 29 ; %d ~" x)) (kbd (format "%s<f16>" tkey)))
	  ;; f17
	  (define-key key-translation-map (kbd (format "M-[ 31 ; %d ~" x)) (kbd (format "%s<f17>" tkey)))
	  ;; f18
	  (define-key key-translation-map (kbd (format "M-[ 32 ; %d ~" x)) (kbd (format "%s<f18>" tkey)))
	  ;; f19
	  (define-key key-translation-map (kbd (format "M-[ 33 ; %d ~" x)) (kbd (format "%s<f19>" tkey)))
	  ;; f20
	  (define-key key-translation-map (kbd (format "M-[ 34 ; %d ~" x)) (kbd (format "%s<f20>" tkey)))

	  (setq x (+ x 1))
	  ))
      )
  )

替代方案

有很多Emacs的实现。GNU/Emacs 可能是最受欢迎的了。
更轻量的兼容性较好的Emacs可以在Arch仓库或在AUR中找到。

mg

mg(原来叫MicroGnuEmacs),是用C语言实现的轻量级Emacs。

可以从{{ic|community}中安装mg

# pacman -S mg

或者从官方网站page中下载源代码。

zile

引用官方网站page的描述,"GNU Zile is a lightweight Emacs clone. Zile is short for Zile Is Lossy Emacs. Zile has been written to be as similar as possible to Emacs; every Emacs user should feel at home.",意思是'GNU Zile'是一个轻量级的Emacs的克隆。Zile是'Zile Is Lossy Emacs'的缩写。 Zile的实现与Emacs如此相似以至于每个Emacs用户使用Zile一定会有一种宾至如归的感觉。

zile 可以在extra中找到

# pacman -S zile

最新的tarball可以在GNU的官方源mirrors中找到。

uemacs

uemacs 是由Linus Torvalds定制的微型Emacs版本。 它可以在 AURuemacs中找到。

资源