Emacs

出自 Arch Linux 中文维基

本文內容或本節內容已經過期。

原因: 請提供模板的第一個位置參數以概括原因。 (在Talk:Emacs討論)

這篇文章或章節的翻譯不反映原文。

原因:Out of sync with the english pages since 2018(在 Talk:Emacs# 中討論)

Emacs是一個可擴展、可定製、自文檔化的實時顯示編輯器。Emacs的核心構建在Emacs Lisp解釋器之上,Emacs Lisp是大部分Emacs內建函數和拓展模塊的實現語言。在圖形界面系統下,Emacs使用GTK作為默認的X工具,在命令行界面下(CLI),Emacs也可以工作良好。在文本編輯能力上,Emacs常被拿來和vim比較。

安裝[編輯 | 編輯原始碼]

如果要安裝穩定版本的 GNU Emacs,請安裝 emacs 包,此外,還有以下發行版

  • emacs-gitAUR:GNU Emacs 的開發版本。自 2021 年 6 月起,它添加了對 feature/native-comp 的支持。
  • emacs-pgtk-native-comp-gitAUR 或者 emacs-native-comp-git-enhancedAUR:一個非官方的分支,結合了 GNU Emacs 的開發分支和新的渲染引擎PGTK((還有 feature/native-comp)。
  • emacs-nox:沒有 GTK+ 支持的 GNU Emacs,沒有聲音或其它花哨的東西,在終端下使用。文字模式的Emacs有一些缺點:它支持的顏色和字體設置功能都要更少(實時改變字體大小,單文檔多字體,等等)。而且 emacs-nox 存在一些高級功能上的缺陷,比如 Speedbar 和 GUD(調試環境),處理複雜的外觀(face,文本在 Emacs 中呈現的樣子)的時候速度也會變慢。

值得注意的是,archlinuxcn 源中也有一個名為 emacs-git 的包,它是 GNU Emacs 的開發版本,但是沒有對 feature/native-comp 的支持。原版 emacsemacs-native-comp 的主要區別是後者的性能會更好一些。

運行Emacs[編輯 | 編輯原始碼]

啟動Emacs之前,你應該知道怎樣關掉它(特別是你在終端裡運行時):使用 Ctrl+xCtrl+c 按鍵順序。

啟動Emacs:

$ emacs

或者以文字模式啟動:

$ emacs -nw

又或者,快速啟動(不解析.emacs文件)並以文字模式啟動:

$ emacs -Q -nw

如果你安裝的是nox版本,'emacs' 和 'emacs -nw' 效果是一樣的。

可以提供文件名直接打開文件:

$ emacs filename.txt

沒有顏色[編輯 | 編輯原始碼]

默認情況下,Emacs以顏色主題開始,顯示超連結為深藍色。

以文本模式,不使用任何顏色主題啟動Emacs:

$ emacs -nw --color=no

這將導致所有的文本採用終端的字體顏色 –– 通常是黑色背景之上的白色文本,或白色背景上的黑色文本。

作為守護進程[編輯 | 編輯原始碼]

如過不想讓Emacs每次啟動都讀取配置文件的話,可以以守護進程運行Emacs:

$ emacs --daemon

連接到守護進程:

$ emacsclient -nc

這個命令會創建一個新的frame -c(如果你更喜歡文字模式,使用 -t )並且不會獨佔終端 -n--no-wait)。 有的程序,如Mutt和Git,(為了提交信息)會等待編輯器完成編輯,所以不能使用 -n 參數。 如果你的默認編輯器是默認使用-n,你需要為那些程序指定一個替代編輯器(比如 emacsclient -a "" -t)。

作為systemd單元[編輯 | 編輯原始碼]

舊的systemd單元方法有一些需要注意的地方。 它給了一個限制shell調用的有限的shell環境,所以我們需要使用一個user單元,它往往比調用emacs --daemon好得多。 為Emacs創建一個systemd單元:

注意: 這樣一個單元文件將會包含在Emacs 26.1中, 參見 emacs bug 16507.
~/.config/systemd/user/emacs.service
[Unit]
Description=Emacs: the extensible, self-documenting text editor

[Service]
Type=forking
ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
Restart=always

[Install]
WantedBy=default.target

需要啟動並啟用該單元,以便其在每次電腦啟動時啟動(請注意 - 請勿「以root運行」 - 我們希望它以user,而不是根root user運行):

$ systemctl --user enable --now emacs

請注意,systemd user單元不會從登錄shell(例如~/.bash_profile)繼承環境變量,因此你可能需要在~/.pam_environment中設置變量。有關更多信息,請參見Systemd/User

如啟動emacs作為守護程序,你可能會想把VISUALEDITOR環境變量設置為emacsclient,以便啟動編輯器的程序使用emacsclient而不是一個完整的編輯器。使用外部編輯器的程序,包括電子郵件程序(用於編輯消息),Git(用於編輯提交消息)和less(用於編輯顯示文件的v命令)。 不要對emacsclient使用-n--nowait)選項,因為程序通常要求在編輯器退出時編輯完成。

建議將Emacs的任何GUI開始菜單條目(或等效條目)更改為指向emacsclient而不是emacs,以便使用emacs守護程序而不是啟動新的emacs進程。

獲得幫助[編輯 | 編輯原始碼]

儘管Emacs很複雜,但了解定製和可擴展性帶來的好處並不需要很長時間。此外,已有的各種擴展允許將其轉變為針對幾乎任何形式的強大文本編輯環境。

Emacs有一個很好的內置教程,可以通過點擊啟動畫面上的第一個連結進行訪問; 通過從菜單中選擇幫助-> Emacs 教程或按F1 + t,亦或者按 C-h t (C 默認是 Ctrl 鍵, - 符號代表同時按下,也就是説你需要按 Ctrl 鍵的同時按 h 鍵,然後放開再按 t 鍵)

Emacs設計為良好的自文檔化。 因此,你很容易知道一些特定命令和快捷鍵是什麼意思,比如命令 find-file ,而它的默認快捷鍵是 C-x C-f, f 鍵可聯想為file。Emacs 提供大量文檔給使用者,建議頻繁使用C-h C-h查看所有命令,快捷鍵和方法。

Emacs還包含一組參考卡,對初學者和專家都很有用,請參閱/usr/share/emacs/<version>/etc/refcards/(用您的emacs版本替換<version>)。

手冊[編輯 | 編輯原始碼]

如果你真的想要掌握Emacs,最推薦的文檔來源仍然是官方手冊:

  • Emacs:完整的Emacs用户手冊。
  • Emacs 常見問題。
  • Emacs Lisp簡介:如果你以前從未使用任何編程語言。
  • Elisp:如果你已經熟悉一門編程語言。

通過內置'info'閱讀器,你可以從官網的 PDF 文檔GNU.org 或直接從 Emacs 本身訪問:C-h i。 可以按 m 鍵快速選擇特定章節。

有些用户更喜歡使用'info'閱讀器來閱讀文本,因為它有很方便的默認快捷鍵來幫使用者瀏覽整本書。另外其段落會適應窗口寬度,字體會適應當前屏幕解像度。有些人覺得這樣不那麼刺激眼睛。最後,你可以輕鬆地將章節中的內容複製到任何Emacs緩衝區,甚至可以直接從文本的示例中執行Lisp代碼片段。

嘗試用 C-h i m info <RET> 快捷鍵獲取更多 info reader 的信息。 在info模式下按 ? 可以快速查看快捷鍵列表。

定製化[編輯 | 編輯原始碼]

Emacs 其中一個主要的特點就是擁有極高自由度的擴展性,而且配置起來很容易。Emacs 內置自定義頁面,你可以通過M-x customize快捷鍵查看自定義選項列表,亦或者用M-x customize-group查詢特定插件的自定義列表。更多詳細請查詢 Emacs manual C-h r 裡的 Easy Customization

配置文件[編輯 | 編輯原始碼]

當Emacs啟動時,通常會自動加載初始化配置文件。這些配置文件由 lisp 語言編寫(準確地來説是Emacs lisp),配置文件默認是在~/.emacs.el或者~/.emacs。你也可以把配置文件放在目錄裡方便管理,默認下是在~/.emacs/init.el。更多詳細請看 Emacs manual 裡的 Init File 章節。

提示和技巧[編輯 | 編輯原始碼]

TRAMP[編輯 | 編輯原始碼]

TRAMP (Transparent Remote Access, Multiple Protocols) ,顧名思義,是一個可以通過很多協議透明訪問遠程文件的一個擴展。當提示輸入文件名時,輸入特定的格式就可以使用TRAMPP。比如:

在打開/etc/hosts文件之前提示輸入root的密碼以獲取root權限:

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

要通過SSH使用'you'用户名登錄'remotehost'主機打開文件~/example.txt

C-x C-f /ssh:you@remotehost:~/example.txt

TRAMP的路徑一般是這種格式'/[protocol]:[[user@]host]:<file>'。

作為用户'you'連接至'myhost'並以sudo編輯/etc/hosts

/ssh:you@remotehost|sudo:remotehost:/etc/hosts

TRAMP支持的不只上面的兩個簡單例子。請查看Emacs裏面的TRAMPP info手冊了解更多的信息。

使用Emacs作為git mergetool[編輯 | 編輯原始碼]

默認情況下,Git支持使用Emacs的Emerge模式作為合併工具。但是你可能更喜歡Ediff模式。不幸的是,由於技術原因,這種模式不被git支持。通過在emacs調用時對一些elisp代碼賦值,仍然有一種方法可以使用Ediff。

.gitconfig
[mergetool.ediff]
    cmd = emacs --eval \" (progn (defun ediff-write-merge-buffer () (let ((file ediff-merge-store-file)) (set-buffer ediff-buffer-C) (write-region (point-min) (point-max) file) (message \\\"Merge buffer saved in: %s\\\" file) (set-buffer-modified-p nil) (sit-for 1))) (setq ediff-quit-hook 'kill-emacs ediff-quit-merge-hook 'ediff-write-merge-buffer) (ediff-merge-files-with-ancestor \\\"$LOCAL\\\" \\\"$REMOTE\\\" \\\"$BASE\\\" nil \\\"$MERGED\\\"))\" 

[merge]
	tool = ediff

請注意,該命令必須在同一行上。在上面的例子中,我們啟動了一個Emacs的新實例。你可能想要使用emacsclient更快地啟動; 不建議這樣做,因為Ediff調用不是很乾淨:它可能會打斷當前的Emacs會話。

如果你想要一個即時啟動,可以使用-q參數。如果想在保留至少一部分配置的情況下快速啟動Emacs,則可以這樣使用Emacs:

emacs -q -l ~/.emacs-light

light配置文件僅加載Ediff所需的內容。

關於這個技巧的細節和Ediff問題,參見kerneltrap.orgstackoverflow

使用大寫鎖定作為控制鍵[編輯 | 編輯原始碼]

一些用户喜歡這種個修改,以避免所謂的'emacs pinky'。如果你想在 X Keyboard 上試用它,運行

$ setxkbmap -option 'ctrl:nocaps'

或者,交換鍵值,運行

$ setxkbmap -option 'ctrl:swapcaps'

要永久更改它,請考慮將其添加到.xinitrc文件中。

如果要將一個區域變為大寫,只需使用默認的C-x C-u鍵綁定,調用upcase-region函數。

參見[1]了解另一種方法。

如果缺少Caps Lock功能,請同時將其映射為「Shift」。

$ setxkbmap -option "shift:both_capslock"

某些桌面環境包含圖形工具以簡化鍵盤重新映射。 例如,在Plasma 5中打開系統設置並點擊輸入設備。選擇鍵盤,然後在高級選項卡中會看到Caps Lock設置,可以在其中選擇Caps Lock也是一個Ctrl。

復用 emacs and emacsclient[編輯 | 編輯原始碼]

emacs-session以同樣的方式打開一個新文件需要使用emacsclient. 如果會話emacs存在,命令可以自己包裝以執行更智能的行為來打開文件。 要開始會話您需要start-server. 這將會在emacs的第一個會話中創建伺服器.將此添加到您的emacs 配置文件中.

.emacs or .emacs.d/init.el
(require 'server)
(unless (server-running-p)
  (server-start))

Shell 別名方法不適用於此,因為您還需要傳遞變量或啟動您自己的獨立會話. 將此添加到.bashrc或其它的shell rc文件中. 如果$ emacs傳遞了參數,這將使您的命令表現得像emacsclient.

function emacs {
    if [[ $# -eq 0 ]]; then
        /usr/bin/emacs # "emacs" is function, will cause recursion
        return
    fi
    args=($*)
    for ((i=0; i <= ${#args}; i++)); do
        local a=${args[i]}
        # NOTE: -c for creating new frame
        if [[ ${a:0:1} == '-' && ${a} != '-c' && ${a} != '--' ]]; then
            /usr/bin/emacs ${args[*]}
            return
        fi
    done
    setsid emacsclient -n -a /usr/bin/emacs ${args[*]}
} 

如果您想在新會話中運行它,只需執行emacs <file> -.

多種配置[編輯 | 編輯原始碼]

你可以使用少量配置然後吿訴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

你可以為這個命令創建一個別名。

本地化和自定義變量[編輯 | 編輯原始碼]

您可以在配置文件中定義變量,稍後可以在本地為文件修改這些變量.

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

現在在任何文件中,您都可以通過兩種方式定義局部變量:

  • 使用M-x add-file-local-variable-prop-line,它在開頭添加了一個類似於以下內容的註釋行:
// -*- my-compiler:g++; mode:c++ -*-
  • 或者您可以使用M-x add-file-local-variable在文件末尾附近添加行:
// Local Variables:
// my-compiler: g++
// mode: c++
// End:

請注意,要使這些值生效,您需要調用M-x revert-buffer.

默認情況下,自定義變量被認為是不安全的。如果您試圖打開一個包含重新定義自定義變量的局部變量的文件,Emacs將要求您確認.

您可以將變量聲明為安全的,從而刪除Emacs確認提示。您需要指定任何新值都必須驗證的謂詞,以便將其視為安全的.

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

在前一個示例中,如果試圖設置字符串以外的任何東西,Emacs會認為它不安全.

定製顏色和主題[編輯 | 編輯原始碼]

使用face功能可以輕鬆定製顏色.

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

您可以讓Emacs吿訴您點所在的面的名稱。為此,請使用customize-face功能。該設施將向您展示如何設置顏色、粗體、下劃線等.

控制台中的Emacs可以處理256色,但您必須為此使用適當的terminal。例如,URxvt支持256色。您可以使用list-colors-display查看受支持顏色的綜合列表。這是高度依賴terminal的.

SyncTeX支持[編輯 | 編輯原始碼]

Emacs是一個功能強大的LaTeX編輯器。這主要是因為您可以調整或創建一個LaTeX模式以最適合您的需要.

儘管如此,還是有一些挑戰,比如SyncTeX支持。首先,您需要確保您的TeX發行版擁有它。如果手動安裝TeX Live,則可能需要安裝synctex軟件包.

# umask 022 && tlmgr install synctex

SyncTeX支持依賴於查看器。這裏我們將以Zathura為例,因此如果您想使用另一個PDF查看器,則需要修改代碼.

(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)

這裏我們定義了自定義變量。如果您使用的是AucTeX或Emacs默認LaTeX模式,則必須相應地設置查看器.

現在用Emacs打開一個LaTeX源文件,編譯文檔,並啟動查看器。Zathura會觸發的。如果按{ic | Ctrl+左鍵單擊}},則Emacs應將點放置在相應位置.

systemd文件的語法高亮[編輯 | 編輯原始碼]

您可以用systemd-mode.

或者,您可以簡單地吿訴emacs為systemd文件(服務、計時器等)高亮,方法是將其添加到init文件中:

 (add-to-list 'auto-mode-alist '("\\.service\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.timer\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.target\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.mount\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.automount\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.slice\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.socket\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.path\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.netdev\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.network\\'" . conf-unix-mode))
 (add-to-list 'auto-mode-alist '("\\.link\\'" . conf-unix-mode))

emacs-nox的剪貼板支持[編輯 | 編輯原始碼]

要在emacs-nox中使用Xorg的剪貼板, 安裝 xclip並將以下函數添加到~/.emacs [2]

;; use xclip to copy/paste in emacs-nox
(unless window-system
  (when (getenv "DISPLAY")
    (defun xclip-cut-function (text &optional push)
      (with-temp-buffer
	(insert text)
	(call-process-region (point-min) (point-max) "xclip" nil 0 nil "-i" "-selection" "clipboard")))
    (defun xclip-paste-function()
      (let ((xclip-output (shell-command-to-string "xclip -o -selection clipboard")))
	(unless (string= (car kill-ring) xclip-output)
	  xclip-output )))
    (setq interprogram-cut-function 'xclip-cut-function)
    (setq interprogram-paste-function 'xclip-paste-function)
    ))
提示:您還可以通過添加:
;; xterm mouse support
(require 'mouse)
(xterm-mouse-mode t)
參考mwheel.el.

[編輯 | 編輯原始碼]

Emacs的功能可以通過第三方軟件包進行擴展。內置的包管理器package.el是被官方支持的方式, 儘管還有其他幾個包管理器是由Emacs社區的成員編寫的. package.el依賴於變量package-archives尋找包. 默認它包含Emacs Lisp Package Archive (ELPA). M-x list-packages將創建一個緩衝區,列出Emacs知道的所有包。手冊((info "(emacs) Packages"))包含更多信息.

可以添加第三方包存檔。其中應用最廣泛的是MELPA.

許多流行的擴展程序包在[community] repository, and more still, via the AUR. 這些包的名稱通常有一個'emacs-'前綴(例如, emacs-lua-mode), 雖然不總是這樣 (例如, auctexAUR).

提示:ArchLinuxWiki貢獻者可能對Emacs Mediawiki感興趣.

某些軟件包可能需要您對配置文件進行更改,以便激活它們,從而使其功能在Emacs會話期間可用。例如,如果您安裝auctexAUR,您將需要添加

(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)

到您的配置文件. 其他軟件包應該讓您知道如何在原始碼的註釋部分或README中激活它們.

疑難雜症[編輯 | 編輯原始碼]

彩色輸出的問題[編輯 | 編輯原始碼]

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: https://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 does not 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 is not a bug, but a feature of new Emacs versions. Reading the subsequent messages about it on the mail list, we found it (https://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 + 方向鍵在tmux中使用Emacs時不工作[編輯 | 編輯原始碼]

首先,你需要在tmux配置中啟用xterm-keys。

.tmux.conf
setw -g xterm-keys on

但這樣的話,會破壞其他的組合鍵。在Emacs配置中寫入以下內容來修復這個問題。

.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的工具。 你也可以嘗試在在Arch倉庫或在AUR中找相似 Emacs 的工具。

  • mg — C 語言編寫實現輕量級 emacs。注意這軟件不支持 UTF-8。
https://homepage.boetes.org/software/mg/ || mg
  • Zile — "Zile Is Lossy Emacs", 一個輕量級的克隆版本,編寫得儘可能類似 Emacs 。
https://www.gnu.org/software/zile/ || zile
  • uemacs — 由 Linus Torvalds定製的「 Micro-emacs」版本。
https://git.kernel.org/pub/scm/editors/uemacs/uemacs.git || uemacs-gitAUR
  • remacs — Rust 主打的 emacs。
https://github.com/remacs/remacs || remacsAUR[損壞的連結:package not found]

資源[編輯 | 編輯原始碼]