「Emacs」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(Kusakata がページ「Emacs (日本語)」を「Emacs」に移動しました)
(一部を翻訳)
(4人の利用者による、間の20版が非表示)
1行目: 1行目:
[[Category:Development (日本語)]]
+
[[Category:テキストエディタ]]
[[Category:Text editors (日本語)]]
+
[[Category:コンソールアプリケーション]]
  +
[[Category:GNU]]
 
[[de:Emacs]]
 
[[de:Emacs]]
 
[[en:Emacs]]
 
[[en:Emacs]]
  +
[[es:Emacs]]
 
[[fr:Emacs]]
 
[[fr:Emacs]]
 
[[sr:Emacs]]
 
[[sr:Emacs]]
[[zh-CN:Emacs]]
+
[[zh-hans:Emacs]]
[[Wikipedia:ja:Emacs|Emacs]] は拡張性があり、カスタマイズ可能な、セルフドキュメント方式のリアルタイム表示エディタです。Emacs のコアには、多数の Emacs の内蔵機能や拡張を実装するのに使われている言語である、[[Wikipedia:ja:Emacs Lisp|Emacs Lisp]] のインタプリタが存在します。GNU Emacs 22 から GTK がデフォルトの X ツールキットとして使われていますが、CLI 環境でも負けず劣らずに機能します。Emacs のテキスト編集機能はしばしば [[Vim (日本語)|vim]] と比較されることがあります。
+
[[Wikipedia:ja:Emacs|Emacs]] は拡張性があり、カスタマイズ可能な、セルフドキュメント方式のリアルタイム表示エディタです。Emacs のコアには、多数の Emacs の内蔵機能や拡張を実装するのに使われている言語である、[[Wikipedia:ja:Emacs Lisp|Emacs Lisp]] のインタプリタが存在します。GNU Emacs 22 から GTK がデフォルトの X ツールキットとして使われていますが、CLI 環境でも負けず劣らずに機能します。Emacs のテキスト編集機能はしばしば [[vim]] と比較されることがあります。
   
 
== インストール ==
 
== インストール ==
   
Emacs には複数の種類があります (時々 ''emacsen'' と呼ばれます)。その中で最も一般的なのは [http://www.gnu.org/software/emacs/ GNU Emacs] です。
+
Emacs には複数の種類があります (時々 ''emacsen'' と呼ばれます)。その中で最も一般的なのは [https://www.gnu.org/software/emacs/ GNU Emacs] です。
   
[[公式リポジトリ]]の {{Pkg|emacs}} を[[pacman (日本語)|インストール]]してください。いつもターミナルで作業をする場合は、GTK+ がいらない {{Pkg|emacs-nox}} の方が良いでしょう (サウンドなどの装飾的な機能もありません)。
+
{{Pkg|emacs}} パッケージを[[インストール]]してください。いつもターミナルで作業をする場合は、GTK+ がいらない {{Pkg|emacs-nox}} の方が良いでしょう (サウンドなどの装飾的な機能もありません)。
 
ただしテキストバージョンにはいくつか欠点が存在します: サポートしている色やフォント処理の機能 (実行中にサイズを変更したり、単一の文章で複数のサイズを使うなど) が少なくなっています。その上、emacs-nox には Speedbar や GUD (デバッグ環境) などの高度な機能について制限があり、複雑なフェイスを使おうとするとやや遅くなります ("フェイス"とは Emacs におけるテキストの外観のことです)。
 
ただしテキストバージョンにはいくつか欠点が存在します: サポートしている色やフォント処理の機能 (実行中にサイズを変更したり、単一の文章で複数のサイズを使うなど) が少なくなっています。その上、emacs-nox には Speedbar や GUD (デバッグ環境) などの高度な機能について制限があり、複雑なフェイスを使おうとするとやや遅くなります ("フェイス"とは Emacs におけるテキストの外観のことです)。
   
24行目: 26行目:
 
</nowiki>}}
 
</nowiki>}}
   
他に {{Pkg|xemacs}} もよく使われています。
+
他に {{AUR|xemacs}}{{Broken package link|パッケージが存在しません}} もよく使われています。
   
 
== Emacs の実行 ==
 
== Emacs の実行 ==
39行目: 41行目:
   
 
$ emacs -nw
 
$ emacs -nw
  +
  +
(.emacs を使わずに) 高速でロードして CLI で編集するには:
  +
  +
$ emacs -Q -nw
   
 
nox バージョンをインストールしている場合、'emacs' と 'emacs -nw' は同じになります。
 
nox バージョンをインストールしている場合、'emacs' と 'emacs -nw' は同じになります。
52行目: 58行目:
 
$ emacs -nw --color=no
 
$ emacs -nw --color=no
   
このコマンドでは全てのテキストが白一色だけで表示されます。
+
このコマンドでは全てのテキストがターミナルの前景色だけで表示されます。通常は黒背景に白字か、白背景に黒字となります。
   
 
=== デーモンとして ===
 
=== デーモンとして ===
81行目: 87行目:
   
 
{{bc|1=
 
{{bc|1=
alias em='emacsclient -nc -a ""'
+
alias emt='emacsclient -nc -a ""'
 
alias emc='emacsclient -t -a ""'
 
alias emc='emacsclient -t -a ""'
 
EDITOR='emacsclient -a ""'
 
EDITOR='emacsclient -a ""'
88行目: 94行目:
   
 
スクリプトを書くほうがもっと使い勝手が良く信頼性があります:
 
スクリプトを書くほうがもっと使い勝手が良く信頼性があります:
{{hc|/usr/local/bin/em|<nowiki>
+
{{hc|/usr/local/bin/emc|<nowiki>
 
#!/bin/sh
 
#!/bin/sh
 
if [ -z "$DISPLAY" ]; then
 
if [ -z "$DISPLAY" ]; then
103行目: 109行目:
 
</nowiki>}}
 
</nowiki>}}
 
スクリプトは実行可能属性を付けて下さい:
 
スクリプトは実行可能属性を付けて下さい:
# chmod 755 /usr/local/bin/em
+
# chmod 755 /usr/local/bin/emc
   
これで 'em' が期待通りに動作するようになります。このクライアントをデフォルトのエディタにしたいときは上記のスクリプトで {{ic|EDITOR}} 環境変数を設定すれば OK です。
+
これで 'emc' が期待通りに動作するようになります。このクライアントをデフォルトのエディタにしたいときは上記のスクリプトで {{ic|EDITOR}} 環境変数を設定すれば OK です。
   
  +
=== systemd ユニットとして ===
You may find that typing "em filename.c" could be easily mistyped as "rm filename.c", so you may prefer to use "emc" (read "emacs client") instead of "em". Non-X Emacs should be used when emc is called from terminal, whether you are in X or not. Another thing, Emacs daemon also complains about the "long standing Gtk bug". That bug can be avoid by telling the Emacs daemon that there is no DISPLAY. So, here is another alternative:
 
   
  +
Emacs 26.1 から systemd ユニットが含まれています。Emacs のインストール後に以下のように有効化してください:
{{hc|/usr/local/bin/emc|<nowiki>
 
#!/bin/sh
 
ps aux|grep 'emacs *--daemon' || DISPLAY='' emacs --daemon -nw --no-splash
 
   
  +
$ systemctl --user enable --now emacs
if [ "x$1" = "x-nw" ]; then
 
exec emacsclient "$@"
 
else
 
exec emacsclient -nc "$@"
 
fi
 
</nowiki>}}
 
   
  +
Emacs サーバーを実行したいユーザーで上記のコマンドを実行してください。サービスの起動後、Emacs の準備ができます。ユニットファイルは {{ic|/usr/lib/systemd/user/emacs.service}} に存在します。ユニットファイルの中身は以下のようになっています:
And create an alias in the interactive shell script
 
   
{{hc|$HOME/.zshrc or $HOME/.bashrc|<nowiki>
+
{{hc|/usr/lib/systemd/user/emacs.service|<nowiki>
[ -f $HOME/.aliases ] && . ~/.aliases
 
</nowiki>}}
 
 
{{hc|$HOME/.aliases|<nowiki>
 
alias emc='emc -nw'
 
</nowiki>}}
 
 
([[GNOME (日本語)|GNOME]], [[KDE (日本語)|KDE]], [[Xfce (日本語)|Xfce]] などの) デスクトップ環境で、新しいファイルを開いた時に {{ic|emacs %f}} の代わりに {{ic|emacsclient -c}} を使うようにしたい場合は、emacs.desktop を以下のように修正してください:
 
{{hc|/usr/share/applications/emacs.desktop|
 
## This line has been commented out:
 
#Exec&#61;emacs %f
 
 
## For this line
 
Exec&#61;emacsclient -c
 
 
## or, if using the preceeding script, uncomment this line instead
 
#Exec&#61;em
 
}}
 
 
これで、ファイルを開く度にクライアントが呼び出されるようになり起動がとても速くなります。
 
 
=== systemd ユニットとして ===
 
 
旧システムユニットの方法には注意事項がありました。シェルの呼び出しが制限される限定的なシェル環境になるため、ユーザーユニットを使用します、単純に ''emacs --daemon'' を呼び出すよりもずっと良い感じで動作するはずです。
 
 
emacs の systemd ユニットを作成:
 
{{hc|~/.config/systemd/user/emacs.service|<nowiki>
 
 
[Unit]
 
[Unit]
Description=Emacs: the extensible, self-documenting text editor
+
Description=Emacs text editor
  +
Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/
   
 
[Service]
 
[Service]
Type=forking
+
Type=simple
ExecStart=/usr/bin/emacs --daemon
+
ExecStart=/usr/bin/emacs --fg-daemon
 
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
 
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
  +
Environment=SSH_AUTH_SOCK=%t/keyring/ssh
Restart=always
 
  +
Restart=on-failure
   
 
[Install]
 
[Install]
163行目: 137行目:
 
</nowiki>}}
 
</nowiki>}}
   
  +
systemd のユーザーユニットはログインシェル ({{ic|~/.bash_profile}} など) から環境変数を継承しないため、{{ic|~/.pam_environment}} に変数を設定すると良いでしょう。詳しくは [[Systemd/ユーザー]]を参照。
起動時に実行されるようにユニットを有効にする必要があります (root でコマンドを実行''しないでください''):
 
 
$ systemctl --user enable emacs
 
   
  +
emacs をデーモンとして起動する場合、{{ic|VISUAL}} と {{ic|EDITOR}} 環境変数を {{ic|emacsclient}} に設定することでプログラムからエディタを起動するときにエディタの新しいインスタンスを起動するかわりに emacsclient が使われるようになります。外部エディタを使用するプログラムとしてメールプログラム (メッセージを編集するため) や Git (コミットメッセージを編集するため)、less ({{ic|v}} コマンドで表示されたファイルを編集するため) などが存在します。emacsclient の {{ic|-n}} ({{ic|--nowait}}) オプションは使用しないでください。基本的にプログラムはエディタが終了したときに編集が完了したと認識するためです。
実際にユニットを使うには、再起動するかユニットを手動で起動します:
 
   
  +
GUI の Emacs のスタートメニューエントリなども emacs ではなく emacsclient が使われるように変更することを推奨します。
$ systemctl --user start emacs
 
   
 
== クイックスタート ==
 
== クイックスタート ==
183行目: 155行目:
 
始めは慣れないと思うかもしれませんが Emacs にはいくつか特別な用語法と約束事があり、必要に応じて紹介していきます。ただ、Emacs を使うにあたって当然知ってるべきとされるような、基本的な用語についてはあらかじめ説明しておいたほうが良いでしょう。
 
始めは慣れないと思うかもしれませんが Emacs にはいくつか特別な用語法と約束事があり、必要に応じて紹介していきます。ただ、Emacs を使うにあたって当然知ってるべきとされるような、基本的な用語についてはあらかじめ説明しておいたほうが良いでしょう。
   
  +
真っ先に知るべきは''バッファ''の概念です。バッファは Emacs の中のデータを表します。例えば、Emacs でファイルを開いたとき、ディスクからファイルから読み込まれてバッファに中身が保存され、バッファを編集して後でディスクに上書き保存することができます。バッファはテキストに限られず、画像やウィジェットなども含めます。作業中でもバッファでアプリケーションを表示することすらできます。こういう考え方もできます: ディスク上のデータは'ファイル'であり、一方、Emacs 上のデータは'バッファ'である。
The one piece of terminology which must be introduced early is the concept of ''buffers''. A buffer is a representation of data within Emacs. For example, when a file is opened in Emacs, that file is read from disk and its contents stored in a buffer, which allows it to be edited and saved back to disk later. Buffers are not limited to text, and can also contain images and widgets. Work is in progress to allow buffers to even display applications! Another way to think of it: data available on disk is referred to as a 'file', whereas data available in Emacs is referred to as a 'buffer'.
 
   
 
Emacs におけるキーシーケンスの決まり事も見慣れないかもしれません。即ち:
 
Emacs におけるキーシーケンスの決まり事も見慣れないかもしれません。即ち:
193行目: 165行目:
 
{{Note|'Meta' は多くの場合 Alt キーに相当します。また、Esc キーを使うことも可能です。}}
 
{{Note|'Meta' は多くの場合 Alt キーに相当します。また、Esc キーを使うことも可能です。}}
   
  +
例えば、Emacs を終了するには次のキーシーケンスを使います: '''C-x C-c'''。これは "Control を押しながら 'x' を押す。手を離す。Control を押しながら 'c' を押す" と読み替えることができます。Emacs にはメニューバーが存在しますが、キーシーケンスを使いこなせるように練習することを推奨します。以下から、Emacs で使われるキーバインディングと慣習について説明します。
For example, to exit Emacs use the following key sequence: '''C-x C-c'''. This can be read as "Hold Control and press 'x'. Release. Hold Control and press 'c'." Although Emacs provides a menu bar, it is recommended practise to focus on learning the key sequences. This guide will refer to keybindings with the convention used in Emacs from now on.
 
   
 
=== 移動 ===
 
=== 移動 ===
   
  +
カーソルの移動は他のグラフィカルエディタとよく似ています。マウスや方向キーを使ってカーソルの位置 (Emacs では''ポイント''と呼ばれます) を変更することができます。一般的な移動コマンドは方向キーで出来ますが、Emacs には他にも便利なバインディングが存在します。1文字先に移動するには '''C-f''' を使用して、1文字前に移動するには '''C-b''' を使用します。'''C-n''' と '''C-p''' でそれぞれ次の行と前の行に移動できます。基本的に、マウスや方向キーを使うよりもこれらのキーシーケンスを使うことが推奨されています。
Cursor movement is very similar to other graphical editors. The mouse and arrow keys can be used to change the position of the cursor (referred to as ''point'' in Emacs). The standard movement commands performed by the arrow keys also have more accessible bindings in Emacs. To move forward one character, use '''C-f''' and to move one character backward, '''C-b'''. '''C-n''' and '''C-p''' can be used to move to the next and previous lines, respectively. Again, it is generally recommended to use these key-sequences in preference to the mouse and/or arrow keys.
 
   
  +
当然、Emacs には、単語や文単位で移動できる、もっと高度な移動コマンドが用意されています。'''M-f''' は1単語先に移動して '''M-b''' は1単語前にポイント移動します。同じく、'''M-e''' は1文先にポイント移動して '''M-a''' は前に戻ります。
As might be expected, Emacs provides more advanced movement commands, including moving by word and sentence. '''M-f''' moves forward one word and '''M-b''' will move point one word backward. Similarly, '''M-e''' moves point one sentence forward and '''M-a''' one sentence backward.
 
   
  +
ここまでは、紹介した全ての移動コマンドはポイントに関連しています。'''M-<''' を使うことでバッファの最初にポイントを移動することができ、逆に、'''M->''' はバッファの最後に移動します。特定の行番号にポイントを移動するには、'''M-g g''' を使います。'''M-g g''' は移動したい行番号を尋ねます。また、現在の行の先頭と末尾に移動するには、'''C-a''' あるいは '''C-e''' を使って下さい。
Until now, all of the movement commands introduced have been relative to point. '''M-<''' can be used to move point to the beginning of the buffer, with its counterpart, '''M->''', moving to the end of the buffer. To move point to a specific line number, use '''M-g g'''. '''M-g g''' will prompt for the desired line number. Also, to move to the start or end of the current line, use '''C-a''' or '''C-e''', respectively.
 
   
  +
{{Note|上記のコマンドを含む全てのコマンドのキーバインドは、現在有効になっているモードによって''多少''ながら変わってくる可能性があります。しかしながら、同一の機能を実行するのにコマンドを置き換えることはあまりありません。詳しくは[[#モード|モード]]を見て下さい。}}
{{Note|Keybindings for these commands, or indeed any command, may differ ''slightly'' depending on which modes are currently active. However, it is unusual for the replacement command not to provide equivalent functionality. See [[#モード|Modes]] for more information.}}
 
   
 
=== ファイルとバッファ ===
 
=== ファイルとバッファ ===
   
  +
Emacs にはファイルを操作するためのコマンドが用意されています。このセクションでは最もよく使われるコマンドを紹介します。ファイルを開くには '''C-x C-f''' を使います (Emacs ではこのコマンドを 'find-file' と呼びます)。指定されたファイルが存在しなかった場合、Emacs は空のバッファを開きます。バッファを保存するとバッファの中身が入ったファイルが作成されます。'''C-x C-s''' を使うことでバッファを保存できます。別のファイル名でバッファを保存するには、'''C-x C-w''' を使って下さい (this is a mnemonic for the command 'write-file')。新しいファイル名が尋ねられ、ディスクにファイルが書き込まれます。'''C-x s''' で全てのバッファを保存することも可能です。最後に保存されていた状態からバッファに変更が加えられている場合、保存を実行するかどうか訊くプロンプトが表示されます。
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.
 
   
{{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, (1) kill the buffer ('''C-x k''') first, then open the file ('''C-x C-f'''); or (2) use '''M-x revert-buffer''' to revert to data on disk; or (3) use '''C-x C-v''' to load that file to the current buffer; or (4) use '''C-x RET r RET''' to reload the file with the same coding.}}
+
{{Note|ファイルに対応するバッファが既に開かれている場合 '''C-x C-f''' はディスクからファイルを読み込みません。ディスクからファイルを再度読み込むには、(1) まずバッファを閉じて ('''C-x k''')、それからファイルを開くか ('''C-x C-f''') あるいは (2) '''M-x revert-buffer''' を使ってディスク上のデータに戻して下さい。それか (3) '''C-x C-v''' でファイルを現在のバッファにロードするか (4) '''C-x RET r RET''' で同一コードでファイルを再読み込みしてください。}}
   
  +
"find-file" や "write-file" などのインタラクティブなコマンドでは Emacs ウィンドウの一番下の行に入力が求められます。この行のことは''ミニバッファ''と呼びます。ミニバッファでは基本的な編集コマンドを使うことができ、多くの *nix シェルで利用できるのと同じようなタブ補完機能があります。'''<TAB>''' を2回連続で押すと補完リストが表示され、マウスを使ってリストから補完候補を選択することも可能です。ミニバッファでの補完はコマンドやファイル名など様々な形で使えます。
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.
 
   
  +
ミニバッファには履歴機能も存在します。前に入力したコマンドは '''Up Arrow''' または '''M-p''' を使うことで再呼び出しできます。
The minibuffer also provides a history feature. The previous items entered for a command can be recalled using the '''Up Arrow''' or '''M-p'''.
 
   
  +
ミニバッファは '''C-g''' を押すことでいつでも終了できます。
To exit the minibuffer at any time, press '''C-g'''.
 
   
  +
ファイルを複数開いたら、ファイルを切り替える手段が必要です。Emacs 内に既に存在するバッファにファイルを開くと、Emacs はそのバッファに切り替えます。しかしながらこれは効率的とは言えません。Emacs には表示するバッファを選択する '''C-x b''' が存在します (タブ補完が使えます)。存在しないバッファの名前を入力すると、その名前で新しいバッファが作成されます。
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.
 
   
  +
{{Note|前のバッファに切り替えるには '''C-x b <RET>''' を使います。前のバッファがデフォルトだからです。}}
{{Note|To switch to the previous buffer use '''C-x b <RET>''', as the previous buffer is the default.}}
 
   
  +
全てのバッファのリストを '''C-x C-b''' で表示することができます。バッファが必要なくなったときは '''C-x k''' で削除することが可能です。
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'''.
 
   
 
=== 編集 ===
 
=== 編集 ===
   
  +
Emacs には多数の編集コマンドが存在します。まだ紹介していない最も重要なコマンドはおそらく 'undo' でしょう。'''C-_''' または '''C-/''' で実行することができます。基本的に移動コマンドには対応する削除コマンドが存在します。例えば '''M-<backspace>''' で後方の単語を削除することができ、{{ic|M-d}} で前方の単語を削除できます。行末または文章の終わりまで削除するにはそれぞれ {{ic|C-k}} または {{ic|M-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 {{ic|M-d}} to delete a word forwards. To delete to the end of the line, or the end of the sentence, use {{ic|C-k}} or {{ic|M-k}}, respectively.
 
   
  +
1行は80文字を超えていはいけないという経験則が存在します。1行の長さを長くしすぎないことで、特にウィンドウの縁で改行してしまうような場合に、可読性を維持することができます。改行の自動挿入 (または削除) は Emacs では詰め込み (''filling'') と呼ばれます。{{ic|M-q}} を使うことでパラグラフを折り返させることができます。
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 {{ic|M-q}}.
 
   
Characters and words can be transposed using {{ic|C-t}} and {{ic|M-t}}, respectively. For example: {{ic|Hello World!}} to {{ic|World! Hello}}.
+
文字列と単語はそれぞれ {{ic|C-t}} {{ic|M-t}} で置き換えることが可能です。例えば {{ic|Hello World!}} {{ic|World! Hello}} と置き換えられます。
   
The case of words is also readily adjustable. {{ic|M-l}} downcases a word from point ({{ic|HELLO}} to {{ic|hello}}); {{ic|M-u}} upcases a word from point ({{ic|hello}} to {{ic|HELLO}}) and {{ic|M-c}} capitalizes the first character of a word from point while downcasing the remainder ({{ic|hElLo}} to {{ic|Hello}}).
+
単語の大文字小文字はすぐに修正できます。{{ic|M-l}} は単語を小文字に変換し ({{ic|HELLO}} {{ic|hello}}){{ic|M-u}} は単語を大文字に変換します ({{ic|hello}} {{ic|HELLO}})。そして {{ic|M-c}} は単語の最初の文字を大文字にして他の文字は小文字にします ({{ic|hElLo}} {{ic|Hello}})
   
 
=== kill と yank とリージョン ===
 
=== kill と yank とリージョン ===
245行目: 217行目:
 
=== 検索と置換 ===
 
=== 検索と置換 ===
   
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'''.
+
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. 次の検索結果に行くにはC-sを、前の検索結果に行くにはC-rをおします。 もし探しているものが見つかったのであれば、'''<RET>''' を押すことで検索を終了できます。 Alternatively, should you wish to return to the location you initiated the search, use '''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.
 
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.
292行目: 264行目:
 
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 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''.
   
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.
+
'''ウィンドウ'''とは、Emacsでバッファを表示している場所のことです。 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.
   
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').
+
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').
   
 
From now on the definition of these terms as they exist in Emacs will be used.
 
From now on the definition of these terms as they exist in Emacs will be used.
308行目: 280行目:
 
=== モード ===
 
=== モード ===
   
  +
Emacs モードは Emacs Lisp で書かれた拡張であり、接続されているバッファの挙動を制御します。大抵の場合、インデントやシンタックスハイライト、テキストを編集するときのキーバインドを提供します。洗練されたモードは Emacs を完全な IDE (統合開発環境) に変貌させます。Emacs は基本的にファイルの拡張子でモードをロードすべきか判断します。
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.
 
   
  +
シェルスクリプトを編集するのに役立つモードとして sh-mode, line-number-mode, column-number-mode が存在します。同時に使用することができ、以下のコマンドで呼び出します:
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:
 
   
 
'''M-x sh-mode <RET>'''
 
'''M-x sh-mode <RET>'''
316行目: 288行目:
 
'''M-x column-number-mode <RET>'''
 
'''M-x column-number-mode <RET>'''
   
  +
line-number-mode はデフォルトで有効になっていますが、コマンドを再度実行することでオンとオフを切り替えられます:
line-number-mode is enabled by default, though, it can be toggled on/off by issuing the command again:
 
   
 
'''M-x line-number-mode <RET>'''
 
'''M-x line-number-mode <RET>'''
332行目: 304行目:
 
'''C-c C-l''' Insert an indexed loop from 1 to n
 
'''C-c C-l''' Insert an indexed loop from 1 to n
   
  +
'line-number-mode' と 'column-number-mode' は''マイナーモード''です。マイナーモードはメジャーモードを拡張することができ、同時に任意の数のマイナーモードを有効にできます。
'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.
 
   
== Tips and tricks ==
+
=== マニュアル ===
  +
  +
Emacs を本当にマスターしたいのであれば、一番よく読むべき文章はやはり公式マニュアルです:
  +
* 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 them 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 の可能性はわかりません。このセクションではより高度なテクニックや機能を紹介します。
 
前のセクションでは基本的な編集コマンドを説明していますが、それだけでは Emacs の可能性はわかりません。このセクションではより高度なテクニックや機能を紹介します。
344行目: 331行目:
 
=== 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) は多数のプロトコルで、リモートファイルに透過的にアクセスすることができる拡張です。ファイル名を入力するときに、特定の形式を入力すると TRAMP が呼びだされます。例:
   
  +
{{ic|/etc/hosts}} を root 権限で開く前に root のパスワードを要求するには:
To prompt for the root password before opening /etc/hosts with root permissions:
 
   
C-x C-f /su::/etc/hosts
+
C-x C-f /sudo::/etc/hosts
   
To connect to 'myhost' as 'myuser' via SSH and open the file ~/example.txt:
+
SSH 'myhost' 'myuser' として接続して {{ic|~/example.txt}} ファイルを開くには:
   
 
C-x C-f /ssh:myuser@myhost:~/example.txt
 
C-x C-f /ssh:myuser@myhost:~/example.txt
   
  +
TRAMP のパスは '/[protocol]:[[user@]host]:<file>' という形式で指定します。TRAMP がサポートしているのは上記の例に限りません。詳しくは TRAMP の info マニュアルを参照してください。マニュアルは Emacs に付属しています。
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.
 
   
 
=== キーボードマクロとレジスタ ===
 
=== キーボードマクロとレジスタ ===
415行目: 402行目:
 
=== 正規表現 ===
 
=== 正規表現 ===
   
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 [http://www.gnu.org/software/emacs/manual/html_node/elisp/Regular-Expressions.html#Regular-Expressions Regular Expressions] section in the Emacs Manual for further reading.
+
Emacs Manual より: "正規表現 (''regexp'' と略す) とは、一致する可能性がある一連の (無限個でもよい) 文字列を表現するパターンである"。このセクションでは正規表現自体には深くは立ち入りません (単純に説明しきれない)。そのかわり正規表現のパワーを簡単にデモンストレーションします。詳しい説明は Emacs Manual [https://www.gnu.org/software/emacs/manual/html_node/elisp/Regular-Expressions.html#Regular-Expressions Regular Expressions] セクションを見て下さい。
   
 
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.
 
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.
432行目: 419行目:
 
=== 矩形選択 ===
 
=== 矩形選択 ===
   
  +
先進的なテキストエディタが当然備えているべき強力な機能として、テキストを矩形で選択・編集できるというのがあるでしょう。
A very powerful feature you may expect from advanced text editors is the possibility to select and edit text in a rectangular area.
 
   
  +
Emacs でも矩形選択は可能です。'''C-SPC''' で普通にテキストを選択してから、複数の矩形コマンドを使うことができます。
This also works in Emacs. Select your text as usual with '''C-SPC'''. Now you can use several rectangular command.
 
   
 
'''C-x r t''' Replace rectangle with text.
 
'''C-x r t''' Replace rectangle with text.
441行目: 428行目:
 
'''C-x r o''' Blank out rectangle.
 
'''C-x r o''' Blank out rectangle.
   
  +
たとえハイライトされていても、上記のコマンドが矩形の外のテキストに影響を与えることはありません。
Note that the command will not affect text outside the rectangle, even though it is highlighted.
 
   
 
=== ブックマーク ===
 
=== ブックマーク ===
   
  +
Emacs は開いたファイルのリストを記録できます。
Emacs can remember a list of visited files.
 
   
'''C-x r m''' Add current buffer to bookmarks.
+
'''C-x r m''' カレントバッファをブックマークに追加。
'''C-x r b''' Open a buffer from bookmarks.
+
'''C-x r b''' ブックマークからバッファを開く。
'''C-x r l''' List bookmarks.
+
'''C-x r l''' ブックマークの確認。
   
 
=== Elisp インタプリタ ===
 
=== Elisp インタプリタ ===
459行目: 446行目:
 
=== スマートなウィンドウ切り替え ===
 
=== スマートなウィンドウ切り替え ===
   
  +
長い間使っていると '''C-x o''' による伝統的なウィンドウ切り替えは面倒かもしれません。'''windmove''' コマンドはもっと便利にウィンドウを切り替えることができます。{{ic|Shift}} を押しながら方向キーでウィンドウを指定するだけです。
The traditional window switch with '''C-x o''' can be cumbersome to use in the long run.
 
The '''windmove''' commands provide a more convenient way to do this. All you have to do is to hold down {{ic|Shift}} while pointing at a window with the arrow keys.
 
   
  +
windmove キーを有効にするには、設定ファイルに以下を記述してください:
To activate the windmove keys, use the following in your configuration file:
 
   
 
{{hc|~/.emacs|
 
{{hc|~/.emacs|
488行目: 474行目:
 
=== 対応する括弧のハイライト ===
 
=== 対応する括弧のハイライト ===
   
  +
'''show-paren''' モードを使うことができます。デフォルトでは、対応する括弧が表示されるまで少し遅延がありますが、'''show-paren-delay''' を 0 に設定することでなくすことができます。
You can use the '''show-paren''' mode.
 
By default, there’s a small delay before showing a matching parenthesis. Set the '''show-paren-delay''' to 0 to deactivate it.
 
   
 
{{hc|~/.emacs|
 
{{hc|~/.emacs|
(show-paren-mode 1)
 
 
(setq show-paren-delay 0)
 
(setq show-paren-delay 0)
  +
(show-paren-mode 1)
 
}}
 
}}
   
 
=== スペルチェック ===
 
=== スペルチェック ===
   
  +
辞書を選択するには:
You can choose the dictionary with
 
 
'''M-x ispell-change-dictionary'''
 
'''M-x ispell-change-dictionary'''
   
548行目: 533行目:
 
\end{tabular}
 
\end{tabular}
   
Using the table mode you can also do some spreadsheet work like sum on rows and columns, but you will quickly find it limited. Besides it is quite slow. Have a look at the [[#Agenda, spreadsheet and document authoring|org-mode]] for much more powerful possibilities.
+
Using the table mode you can also do some spreadsheet work like sum on rows and columns, but you will quickly find it limited. Besides it is quite slow. Have a look at the [[#予定表、スプレッドシート、文章作成|org-mode]] for much more powerful possibilities.
   
 
=== 予定表、スプレッドシート、文章作成 ===
 
=== 予定表、スプレッドシート、文章作成 ===
606行目: 591行目:
 
=== リファクタリングとスマート補完 ===
 
=== リファクタリングとスマート補完 ===
   
If you are looking for popular programming features found in most IDE (such as Eclipse), the [http://cedet.sourceforge.net/semantic.shtml Semantic] tool will do the job. It is part of Emacs standard distribution. Currently C, C++, Scheme, Javascript, Java, HTML, and Make are well supported. You can find a table on the current support state on the [http://cedet.sourceforge.net/languagesupport.shtml CEDET page].
+
(Eclipse などの) IDE が大抵備えているプログラミング機能が必要な場合、[http://cedet.sourceforge.net/semantic.shtml Semantic] ツールが役に立ちます。Semantic Emacs の標準ディストリビューションに含まれています。現在 C, C++, Scheme, Javascript, Java, HTML, Make がサポートされています。現在のサポート状況は [http://cedet.sourceforge.net/languagesupport.shtml CEDET のページ] で確認できます。
   
 
Open a file in your favorite programming language supported by Semantic and turn on the Semantic minor mode with
 
Open a file in your favorite programming language supported by Semantic and turn on the Semantic minor mode with
671行目: 656行目:
 
where the light configuration file loads only what you need for Ediff.
 
where the light configuration file loads only what you need for Ediff.
   
See [http://kerneltrap.org/mailarchive/git/2007/7/1/250424 kerneltrap.org] and [http://stackoverflow.com/questions/1817370/using-ediff-as-git-mergetool stackoverflow] for more details on this trick and the Ediff issue.
+
See [http://kerneltrap.org/mailarchive/git/2007/7/1/250424 kerneltrap.org] and [https://stackoverflow.com/questions/1817370/using-ediff-as-git-mergetool stackoverflow] for more details on this trick and the Ediff issue.
   
 
=== Caps Lock キーを Control キーとして使う ===
 
=== Caps Lock キーを Control キーとして使う ===
683行目: 668行目:
   
 
リージョンを小文字から大文字にする必要がある場合は、デフォルトの {{ic|C-x C-u}} キーバインディングを使って下さい、{{ic|upcase-region}} 関数が呼び出されます。
 
リージョンを小文字から大文字にする必要がある場合は、デフォルトの {{ic|C-x C-u}} キーバインディングを使って下さい、{{ic|upcase-region}} 関数が呼び出されます。
  +
  +
Caps Lock がない場合は、両方の "Shift" にマッピングしてください:
  +
$ setxkbmap -option "shift:both_capslock"
  +
  +
一部のデスクトップ環境にはキーボードのマッピングを変更するためのグラフィカルなツールが含まれています。例えば [[Plasma|Plasma 5]] ならシステム設定を開いて入力デバイスをクリックしてください。キーボードを選択して高度な設定タブから Caps Lock を Ctrl として使用するように設定できます。
   
 
== カスタマイズ ==
 
== カスタマイズ ==
758行目: 748行目:
 
}}
 
}}
   
This is the full configuration we load for the daemon. But the ''plugins'' file is huge and slow to load. If we want to spaqn a new Emacs instance that does not need the ''plutings'' features, it can be cumbersome to load it everytime in the long time.
+
This is the full configuration we load for the daemon. But the ''plugins'' file is huge and slow to load. If we want to spawn a new Emacs instance that does not need the ''plugins'' features, it can be cumbersome to load it everytime in the long run.
   
 
{{hc|.emacs-light|
 
{{hc|.emacs-light|
770行目: 760行目:
 
emacs -q -l ~/.emacs-light
 
emacs -q -l ~/.emacs-light
 
You can create an alias to ease the call.
 
You can create an alias to ease the call.
 
=== 拡張のロード ===
 
 
''require'' 関数を使うことで拡張をロードできます。例えば:
 
(require 'mediawiki)
 
 
If you try using the same configuration file on a machine where mediawiki is not installed, Emacs will primpt for an error. Besides, all extension-specific code would be parsed for nothing.
 
 
The trick is to test the return value of ''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)
 
))
 
))
 
   
 
=== ローカル・カスタム変数 ===
 
=== ローカル・カスタム変数 ===
799行目: 767行目:
 
(defcustom my-compiler "gcc" "Some documentation")
 
(defcustom my-compiler "gcc" "Some documentation")
   
Now in any file you can define local variables in two ways:
+
Now in any file you can define local variables in two ways, see [https://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html the manual for complete details ]
  +
* On the very first line, write
 
  +
* Using {{ic| M-x add-file-local-variable-prop-line}}, which adds a commented line at the beginning similar to:
 
// -*- my-compiler:g++; mode:c++ -*-
 
// -*- 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:
+
* Or you can use {{ic| M-x add-file-local-variable}} to add lines near the end of the file:
 
// Local Variables:
 
// Local Variables:
 
// my-compiler: g++
 
// my-compiler: g++
808行目: 777行目:
 
// End:
 
// End:
   
  +
Note that for the values to take effect, you will need to call {{ic|M-x revert-buffer}}.
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 -*-
 
   
  +
Custom variables are considered unsafe by default. If you try to open a file that contains local variable redefining custom variables, Emacs will ask you for confirmation.
There is two functions that may help you in defining the variables: ''add-file-local-variable'' and ''add-file-local-variable-prop-line''.
 
   
  +
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.
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)
 
(defcustom my-compiler "gcc" "Some documentation" :safe 'stringp)
823行目: 789行目:
 
=== カスタムカラーとテーマ ===
 
=== カスタムカラーとテーマ ===
   
  +
''face'' を使うことで簡単に色をカスタマイズすることができます:
Colors can be easily customized using the ''face'' facility.
 
 
(set-face-background 'region "color-17")
 
(set-face-background 'region "color-17")
 
(set-face-foreground 'region "white")
 
(set-face-foreground 'region "white")
834行目: 800行目:
 
=== SyncTeX サポート ===
 
=== SyncTeX サポート ===
   
  +
Emacs は強力な LaTeX エディタです。LaTeX モードを必要に応じて自由に編集・作成することができます。
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.
 
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.
861行目: 827行目:
 
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.
 
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.
   
  +
=== Systemd ファイルのシンタックスハイライト ===
== ドキュメント ==
 
   
  +
[https://github.com/holomorph/systemd-mode systemd-mode] を使うことができます。
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.
 
   
  +
もしくは init ファイルに以下を追加することで、簡単に emacs で systemd のファイル (サービスやタイマーなど) をカラー表示できます:
=== コンテキストヘルプ ===
 
   
  +
(add-to-list 'auto-mode-alist '("\\.service\\'" . conf-unix-mode))
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:
 
  +
(add-to-list 'auto-mode-alist '("\\.timer\\'" . conf-unix-mode))
 
  +
(add-to-list 'auto-mode-alist '("\\.target\\'" . conf-unix-mode))
'''C-h a''' Find a command matching a description.
 
  +
(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 のクリップボードサポート ===
'''C-h b''' List all active keybindings.
 
   
  +
emacs-nox で [[Xorg]] クリップボードを使うには、{{Pkg|xclip}} を[[インストール]]して以下の関数を {{ic|~/.emacs}} に追加してください [https://lists.gnu.org/archive/html/help-gnu-emacs/2014-08/msg00189.html]:
'''C-h f''' Describe the given function.
 
   
  +
{{bc|1=
'''C-h k''' Find which command a key is bound to.
 
  +
;; 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)
  +
))
  +
}}
   
  +
{{Tip|以下を追加することでターミナルマウスサポートも有効にできます:
'''C-h m''' Display information regarding the currently active modes.
 
  +
{{bc|1=
  +
;; xterm mouse support
  +
(require 'mouse)
  +
(xterm-mouse-mode t)
  +
}}
  +
[http://www.opensource.apple.com/source/emacs/emacs-51/emacs/lisp/mwheel.el mwheel.el] を参照。}}
   
  +
== パッケージ ==
'''C-h t''' Start the Emacs tutorial.
 
   
  +
Emacs の機能はサードパーティ製のパッケージで拡張できます。公式にサポートされているパッケージマネージャとして {{ic|package.el}} が組み込まれていますが、Emacs のコミュニティメンバーによって書かれたパッケージマネージャが他にも存在します。{{ic|package.el}} は {{ic|package-archives}} 変数を使ってパッケージを検索します。デフォルトでは [https://elpa.gnu.org/ Emacs Lisp Package Archive (ELPA)] が含まれています。{{ic|M-x list-packages}} で Emacs が認識している全てのパッケージが列挙されたバッファが作成されます。マニュアル ({{ic|[https://www.gnu.org/software/emacs/manual/html_node/emacs/Packages.html (info "(emacs) Packages")]}}) にはさらに詳しい情報が載っています。
'''C-h v''' Describe the given variable.
 
   
  +
サードパーティ製のパッケージのアーカイブを追加することもできます。[https://melpa.org/ MELPA] が広く使われています。
'''C-h w''' Find which key(s) a command is bound to.
 
   
  +
人気の拡張の多くは {{ic|[community]}} リポジトリのパッケージや [[AUR]] からインストールできます。大抵の場合、パッケージの名前の前に 'emacs-' が付きますが (例: {{Pkg|emacs-lua-mode}})、常にそうなっているわけではありません (例: {{AUR|auctex}})。
=== マニュアル ===
 
   
  +
{{Tip|Arch Linux の Wiki を編集している場合は [[Emacs Mediawiki]] パッケージが役に立ちます。}}
Emacs を本当にマスターしないのならば、一番よく読むべき文章はやはり公式マニュアルです:
 
* 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.
 
   
  +
パッケージによっては設定ファイルに変更を加えないと機能が有効にならない場合があります。例えば、{{AUR|auctex}} をインストールしたときは、設定ファイルに以下を追加:
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.
 
   
  +
{{bc|
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.
 
  +
(load "auctex.el" nil t t)
  +
(load "preview-latex.el" nil t t)}}
   
  +
パッケージのソースコードや README で有効化する方法が書かれているはずです。
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 が 'Undefined color: "WINDOW_FOREGROUND"' というエラーメッセージで起動しない ===
Emacs includes hundreds of modes, libraries and other extensions, with many more available to further Emacs' capabilities. Most of these come with instructions detailing any changes needed to be made in {{ic|~/.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.
 
   
  +
{{Pkg|mcpp}} パッケージまたは {{Pkg|gcc}} パッケージをインストールする必要があります。''xrdb'' は [[X resources]] を前処理するために C のプリプロセッサ ''cpp'' を使用します。C のプリプロセッサがインストールされていない場合、''xrdb'' は C のプリプロセッサを実行しないため WINDOW_FOREGROUND が16進数のカラーコードに展開されません。
A number of popular extensions are available as packages in the 'community' repository, and more still, via [[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 in {{ic|~/.emacs}} are shown during the installation of the package.
 
 
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 packages. See the website for instructions. ELPA is included with Emacs 24 (the newest version of Emacs); it is an accepted part of the Emacs ecosystem. Also, check out the [http://marmalade-repo.org/ Marmalade] and [http://melpa.milkbox.net/ MELPA] repos.
 
 
{{Tip|Use {{ic|M-x list-packages}} to get a list of available packages for installation.}}
 
 
=== Emacs MediaWiki ===
 
Since we are at it, you may be a contributor to Arch Linux Wiki, or any Mediawiki-based website. Then emacs will become your best friend thanks to the [[Emacs Mediawiki]] extension. Check the dedicated page for more details.
 
 
== トラブルシューティング ==
 
   
 
=== カラー出力の問題 ===
 
=== カラー出力の問題 ===
923行目: 904行目:
 
この問題を解決するには以下を {{ic|~/.emacs}} に記述してください:
 
この問題を解決するには以下を {{ic|~/.emacs}} に記述してください:
 
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
 
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
 
=== メニューの表示が空 ===
 
 
(GTK ツールキットを使用する) GNU Emacs 23.1 にはバグが存在しメニューが空になることがあります。この問題は Emacs の CVS トランクでは修正されています。[http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=550541 Debian のバグレポート] に対応策が載っています。
 
   
 
=== X ウィンドウでの文字の表示問題 ===
 
=== X ウィンドウでの文字の表示問題 ===
933行目: 910行目:
   
 
=== 起動が遅い ===
 
=== 起動が遅い ===
  +
  +
{{Tip|[[#systemd ユニットとして|systemd ユニットとして]] Emacs を起動する方法でも起動時間を短縮できます。}}
   
 
しばしば起動時間が遅くなる原因は2つあります。
 
しばしば起動時間が遅くなる原因は2つあります。
942行目: 921行目:
 
このコマンドでも Emacs の起動が遅い場合、[[#不適切なネットワーク設定|不適切なネットワーク設定]]を参照してください。そうでない場合は、ほぼ確実に .emacs に問題があります。
 
このコマンドでも Emacs の起動が遅い場合、[[#不適切なネットワーク設定|不適切なネットワーク設定]]を参照してください。そうでない場合は、ほぼ確実に .emacs に問題があります。
   
  +
* {{ic|/etc/hosts}} の設定が間違っていると、Emacs の起動が5秒以上遅くなることになります。詳しくはネットワーク設定ガイドの '[[ネットワーク設定#ホストネームの設定|ホストネームの設定]]' を見てください。
* 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.
 
   
 
* A simple way to search for the cause is to comment-out (i.e., prefix lines with ';') suspect sections of your {{ic|~/.emacs}} (or {{ic|~/.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:
 
* A simple way to search for the cause is to comment-out (i.e., prefix lines with ';') suspect sections of your {{ic|~/.emacs}} (or {{ic|~/.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:
951行目: 930行目:
   
 
(autoload 'anything "anything" "Select anything" t)
 
(autoload 'anything "anything" "Select anything" t)
 
=== 不適切なネットワーク設定 ===
 
 
$ emacs -q
 
 
で起動が遅い場合、おそらくネットワーク設定に問題があります。何が問題があるのか確認するために、コンピュータから送信されたネットワークパケットを見て何かおかしな挙動がないかチェックする必要があります (Wireshark などのプログラムを使う)。例えば、DNS エラーの場合、{{ic|/etc/hosts}} に以下を追加してください:
 
 
127.0.0.1 localhost HOSTNAME
 
 
HOSTNAME は次のコマンドで表示されるホストネームに置き換えて下さい:
 
 
$ hostname
 
   
 
=== Cannot open load file: ... ===
 
=== Cannot open load file: ... ===
978行目: 945行目:
 
=== デッドアクセントキーの問題: '<dead-acute> is undefined' ===
 
=== デッドアクセントキーの問題: '<dead-acute> is undefined' ===
   
  +
このバグについて Google で検索すると、次のリンクが見つかります:
Searching about this bug on Google, we find this link:
 
http://lists.gnu.org/archive/html/help-gnu-emacs/2009-05/msg00167.html
+
https://lists.gnu.org/archive/html/help-gnu-emacs/2009-05/msg00167.html
   
 
Explaining the problem: in recent versions of
 
Explaining the problem: in recent versions of
989行目: 956行目:
 
(require 'iso-transl)
 
(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):
+
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 (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
+
:''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 classified as a feature ... Maybe it's just because the file is auto-loaded though pretty useless. ''
a3b
 
classified as a feature ... Maybe it's just because the file is auto-loaded though pretty useless. ''
 
   
 
=== C-M-% やその他のバインディングが emacs nox で動作しない ===
 
=== C-M-% やその他のバインディングが emacs nox で動作しない ===
   
  +
これはターミナルが Xorg よりも制限的であることが原因です。ただし、ターミナルによっては多くのバインディングを扱うことができることがあります。2つの解決法が考えられます:
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|
 
{{hc|.emacs|
 
(global-set-key (kbd "C-M-y") 'query-replace-regexp)
 
(global-set-key (kbd "C-M-y") 'query-replace-regexp)
 
}}
 
}}
   
=== Emacs クライアントを切り替えたときにフリーズする ===
+
=== Emacs フリーズする ===
   
  +
シングルスレッドであるがゆえに、Emacs は様々な操作でブロックします。フリーズが発生する状況は様々です。例えば、Emacs はあなたからの入力を待っているのかもしれません (例えばあるフレームでミニバッファを開いているのに他のフレームで作業しようとしている場合など)。あるいは、Emacs は完了するまで時間のかかるコードを実行中なのかもしれません。あるいはバグを踏んだという可能性もあります。Emacs プロセスを殺さずに Emacs のブロックを解除する方法は複数存在します。
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.
 
  +
  +
* {{ic|C-g}} を押してみる。Emacs が行っていることによっては、複数回押す必要があります。
  +
* {{ic|ESC ESC ESC}} を押してみる。
  +
* 他のターミナルで {{ic|killall -SIGUSR2 emacs}} を実行する。
   
 
=== Emacs-nox の出力が汚い ===
 
=== Emacs-nox の出力が汚い ===
   
 
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.
 
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.
  +
If this happens you can do {{ic|M-x redraw-display}}, which will redraw the terminal's display. If this problem happens frequently, you might want to bind the command to a key, e.g. by putting something like
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.
 
  +
(global-set-key (kbd "<f7>") 'redraw-display)
  +
in your .emacs file.
   
 
Graphical Emacs does not suffer from this issue.
 
Graphical Emacs does not suffer from this issue.
  +
  +
=== emacs ターミナルにおかしなエスケープ数字 (utf-8) が表示される ===
  +
  +
{{ic|.bashrc}} または {{ic|.zshrc}} で以下の変数をエクスポートしてください:
  +
  +
{{hc|1=~/.bashrc|2=
  +
export LANG="en_US.UTF-8"
  +
export LC_ALL="en_US.UTF-8"
  +
export TERM=xterm-256color
  +
}}
  +
  +
小文字の utf を使っていることがエラーの原因です (例: en_US.utf-8)。
  +
  +
利用可能なロケールは {{ic|locale -a}} で確認できます。
   
 
=== tmux で emacs を使っているときに Shift + 矢印キーが動作しない ===
 
=== tmux で emacs を使っているときに Shift + 矢印キーが動作しない ===
1,025行目: 1,010行目:
   
 
しかしながら、この設定は他のキーコンビネーションを破壊します。それを修正するために、以下を emacs の設定に記述します:
 
しかしながら、この設定は他のキーコンビネーションを破壊します。それを修正するために、以下を emacs の設定に記述します:
{{hc|.emacs|
+
{{hc|~/.emacs|
  +
(defadvice terminal-init-screen
;; handle tmux's xterm-keys
 
  +
;; The advice is named `tmux', and is run before `terminal-init-screen' runs.
;; put the following line in your ~/.tmux.conf:
 
  +
(before tmux activate)
;; setw -g xterm-keys on
 
  +
;; Docstring. This describes the advice and is made available inside emacs;
(if (getenv "TMUX")
 
  +
;; for example when doing C-h f terminal-init-screen RET
(progn
 
  +
"Apply xterm keymap, allowing use of keys passed through tmux."
(let ((x 2) (tkey ""))
 
  +
;; This is the elisp code that is run before `terminal-init-screen'.
(while (<&#61; x 8)
 
  +
(if (getenv "TMUX")
;; shift
 
  +
(let ((map (copy-keymap xterm-function-map)))
(if (&#61; x 2)
 
  +
(set-keymap-parent map (keymap-parent input-decode-map))
(setq tkey "S-"))
 
  +
(set-keymap-parent input-decode-map map))))
;; 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))
 
))
 
)
 
)
 
 
}}
 
}}
  +
  +
詳しくは [https://github.com/tmux/tmux/blob/master/FAQ#L242 tmux FAQ] を参照。
   
 
=== KDE でウィンドウのリサイズがおかしい ===
 
=== KDE でウィンドウのリサイズがおかしい ===
 
KDE ユーザーには Emacs のウィンドウが正しくリサイズされないという現象がおこることがあります。リサイズした部分が透過されマウスでクリックしても下のウィンドウがクリックされます。この挙動を修正するには、KDE の GTK3 テーマを oxygen-gtk 以外のテーマに変更してください。例えば、{{Pkg|gtk3}} に含まれている Emacs テーマを使って下さい。
 
KDE ユーザーには Emacs のウィンドウが正しくリサイズされないという現象がおこることがあります。リサイズした部分が透過されマウスでクリックしても下のウィンドウがクリックされます。この挙動を修正するには、KDE の GTK3 テーマを oxygen-gtk 以外のテーマに変更してください。例えば、{{Pkg|gtk3}} に含まれている Emacs テーマを使って下さい。
  +
  +
KDE で Emacs を完全に最大化するには、タイトルバーの Emacs アイコンをクリックして、More Actions > Special Window Settings を選択してください。そして "Size & Position" タブで "Obey geometry restrictions" を選択して、ドロップダウンメニューで "Force" を選んで右のラジオボタンで "No" を選んで下さい。
   
 
== 派生 ==
 
== 派生 ==
1,126行目: 1,035行目:
 
Emacs の実装は多数存在します。おそらく GNU/Emacs が一番ポピュラーです。
 
Emacs の実装は多数存在します。おそらく GNU/Emacs が一番ポピュラーです。
   
軽量な Emacs 互換のテキストエディタは Arch のリポジトリや [[AUR (日本語)|AUR]] にあります。
+
軽量な Emacs 互換のテキストエディタは Arch のリポジトリや [[AUR]] にあります。
   
 
=== mg ===
 
=== mg ===
1,132行目: 1,041行目:
 
mg (旧名 MicroGnuEmacs) は C で書かれた軽量な Emacs の実装です。
 
mg (旧名 MicroGnuEmacs) は C で書かれた軽量な Emacs の実装です。
   
{{Pkg|mg}} は[[公式リポトリ]]から入手できますが、上流の [http://homepage.boetes.org/software/mg/ ページ] からソースをダウンロードすることも可能です。
+
{{Pkg|mg}} パッケージから入手できますが、上流の [http://homepage.boetes.org/software/mg/ ページ] からソースをダウンロードすることも可能です。mg は UTF-8 をサポートしていません
   
 
=== zile ===
 
=== zile ===
   
公式ウェブ[https://www.gnu.org/software/zile/ ページ]によると "GNU Zile は軽量な Emacs クローンです。Zile は Zile Is Lossy Emacs の略となっています。Zile は出来る限り Emacs に近づくように書かれており、きっと全ての Emacs ユーザーの手に馴染むでしょう"。
+
公式ウェブ[https://www.gnu.org/software/zile/ ページ]によると "GNU Zile は軽量な Emacs クローンです。Zile は Zile Is Lossy Emacs の略となっています。Zile は出来る限り Emacs に近づくように書かれており、きっと全ての Emacs ユーザーの手に馴染むでしょう"。Zile は UTF-8 をサポートしていません
   
{{Pkg|zile}} は公式リポトリからインストールできます。
+
{{Pkg|zile}} パッケージからインストールできます。
   
 
最新の tarball は公式の GNU [http://ftp.sh.cvut.cz/MIRRORS/gnu/pub/gnu/zile/ ミラー] から落とせます。
 
最新の tarball は公式の GNU [http://ftp.sh.cvut.cz/MIRRORS/gnu/pub/gnu/zile/ ミラー] から落とせます。
1,144行目: 1,053行目:
 
=== uemacs ===
 
=== uemacs ===
   
Linus Torvalds によるカスタム版 Micro-emacs。[[AUR (日本語)|AUR]] の {{AUR|uemacs-git}} でインストール可能。
+
Linus Torvalds によるカスタム版 Micro-emacs。{{AUR|uemacs-git}} でインストール可能。
  +
  +
最新 (2005) の tarball は [ftp://ftp.cs.helsinki.fi/pub/Software/Local/uEmacs-PK/ こちら] にあります。
  +
  +
=== remacs ===
  +
  +
'''remacs''' はコミュニティによる Emacs の Rust 移植です。{{AUR|remacs-git}} パッケージでインストールできます。
   
 
== 参照 ==
 
== 参照 ==
   
* [http://www.gnu.org/software/emacs/ GNU Emacs ホームページ]
+
* [https://www.gnu.org/software/emacs/ GNU Emacs ホームページ]
* [http://www.gnu.org/software/emacs/manual/emacs.html GNU Emacs マニュアル]
+
* [https://www.gnu.org/software/emacs/manual/emacs.html GNU Emacs マニュアル]
 
* [http://www.emacswiki.org/cgi-bin/wiki/ Emacs Wiki]
 
* [http://www.emacswiki.org/cgi-bin/wiki/ Emacs Wiki]
* [http://wikemacs.org WikEmacs - a more readable, but less complete Emacs wiki]
+
* [http://wikemacs.org WikEmacs - 読みやすいが不完全な Emacs wiki]
* [http://www2.lib.uchicago.edu/keith/tcl-course/emacs-tutorial.html Useful introduction to Emacs and its shortcuts]
+
* [http://www2.lib.uchicago.edu/keith/tcl-course/emacs-tutorial.html Emacs とそのショートカットの有用な紹介]
* [https://d0edfcdc0ccc1cd13cdab5eb986fb92e8660dbef.googledrive.com/host/0B6LMD0u8OhYYZEotN2QyR1hwR1k/ The Church of Emacs (via Google drive)]
+
* [https://d0edfcdc0ccc1cd13cdab5eb986fb92e8660dbef.googledrive.com/host/0B6LMD0u8OhYYZEotN2QyR1hwR1k/ Emacs 教会 (Google ドライブ)]
* [http://www.gnu.org/software/emacs/refcards/pdf/refcard.pdf 公式リファレンスカード]
+
* [https://www.gnu.org/software/emacs/refcards/pdf/refcard.pdf 公式リファレンスカード]
  +
* [[EXWM]] - Emacs X Window Manager

2020年5月23日 (土) 12:16時点における版

Emacs は拡張性があり、カスタマイズ可能な、セルフドキュメント方式のリアルタイム表示エディタです。Emacs のコアには、多数の Emacs の内蔵機能や拡張を実装するのに使われている言語である、Emacs Lisp のインタプリタが存在します。GNU Emacs 22 から GTK がデフォルトの X ツールキットとして使われていますが、CLI 環境でも負けず劣らずに機能します。Emacs のテキスト編集機能はしばしば vim と比較されることがあります。

目次

インストール

Emacs には複数の種類があります (時々 emacsen と呼ばれます)。その中で最も一般的なのは GNU Emacs です。

emacs パッケージをインストールしてください。いつもターミナルで作業をする場合は、GTK+ がいらない emacs-nox の方が良いでしょう (サウンドなどの装飾的な機能もありません)。 ただしテキストバージョンにはいくつか欠点が存在します: サポートしている色やフォント処理の機能 (実行中にサイズを変更したり、単一の文章で複数のサイズを使うなど) が少なくなっています。その上、emacs-nox には Speedbar や GUD (デバッグ環境) などの高度な機能について制限があり、複雑なフェイスを使おうとするとやや遅くなります ("フェイス"とは Emacs におけるテキストの外観のことです)。

極めて重い依存パッケージをインストールすることなく Emacs の拡張機能を全て完全に使いたい場合、必要に応じて PKGBUILD をカスタマイズすることができます。gtk3 以外を使えば 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
# ...

他に xemacsAUR[リンク切れ: パッケージが存在しません] もよく使われています。

Emacs の実行

emacs を起動する前に、終了の方法を知っておきましょう (特にターミナルで実行する場合): Ctrl+xCtrl+c キーシーケンスを使って下さい。

通常の方法

Emacs を起動するには次を実行:

$ emacs

また、コンソールから使うには:

$ emacs -nw

(.emacs を使わずに) 高速でロードして CLI で編集するには:

$ emacs -Q -nw

nox バージョンをインストールしている場合、'emacs' と 'emacs -nw' は同じになります。

ファイル名を指定することですぐにファイルを開くこともできます:

$ emacs filename.txt

色無し

デフォルトでは、Emacs はハイパーリンクが濃青で表示されるカラーテーマで起動します。Emacs をカラーテーマやスキームを使わずに起動するには:

$ emacs -nw --color=no

このコマンドでは全てのテキストがターミナルの前景色だけで表示されます。通常は黒背景に白字か、白背景に黒字となります。

デーモンとして

毎回 .emacs ファイルをロードするため Emacs を起動するときには時間が多少かかります。また、別のインスタンスから同じファイルにアクセスしたいと思うときもあるかもしれません。バージョン23から、Emacs はユーザーが接続できるデーモンとして起動することが可能になっています。Emacs をデーモンとして実行するには:

$ emacs --daemon

起動時にデーモンを実行してウィンドウをデーモンに接続すると良いでしょう。さらに、グラフィカル・コンソールクライアント両方を同意時にデーモンに接続して GUI を素早く起動することも可能です。

デーモンに接続したいときは場合は次のコマンドを使って下さい (グラフィカル環境から実行したときはグラフィカルクライアントが、tty などのコンソールから実行したときはコンソールクライアントが起動します):

$ emacsclient

グラフィカル環境にいるときもコンソールクライアントを使いたい場合は次のコマンドを使って下さい:

$ emacsclient -t

さらに、-a "" パラメータを追加することもできます。 これで、最初にコマンドを実行したときは、デーモンとして emacs が起動します。バックグラウンドで動き続けるので後で呼び出した時に起動時間が早くなります (バッファも残り続けます)。

ターミナルや他のプログラムからクライアントを起動した場合、呼び出すプログラムを使うことで Emacs クライアントを閉じなくてもクライアントを使い続けることができます。そのためには、 -n (--no-wait) パラメータを使ってクライアントを起動します:

$ emacsclient -nc

Mutt や Git などのプログラムは (コミットメッセージを読むための) エディタが終了するまで待機するため、-n パラメータを使うことはできません。デフォルトエディタが設定されている場合、他のエディタを指定する必要があるかもしれません (例: emacsclient -a "" -t)。

以下のシェル設定が利用できます:

alias emt='emacsclient -nc -a ""'
alias emc='emacsclient -t -a ""'
EDITOR='emacsclient -a ""'

ただしこれには注意事項があります: コマンドのスペースの関係で多数のプログラムが外部エディタをロードすることはできません。

スクリプトを書くほうがもっと使い勝手が良く信頼性があります:

/usr/local/bin/emc
#!/bin/sh
if [ -z "$DISPLAY" ]; then
    IS_GRAPHICAL=true
else
    IS_GRAPHICAL=$(emacs --batch -Q --eval='(if (fboundp '"'"'tool-bar-mode) (message "true") (message "false"))' 2>&1)
fi

if $IS_GRAPHICAL; then
    emacsclient -a "" -nc "$@"
else
    emacsclient -a "" -t "$@"
fi

スクリプトは実行可能属性を付けて下さい:

# chmod 755 /usr/local/bin/emc

これで 'emc' が期待通りに動作するようになります。このクライアントをデフォルトのエディタにしたいときは上記のスクリプトで EDITOR 環境変数を設定すれば OK です。

systemd ユニットとして

Emacs 26.1 から systemd ユニットが含まれています。Emacs のインストール後に以下のように有効化してください:

 $ systemctl --user enable --now emacs

Emacs サーバーを実行したいユーザーで上記のコマンドを実行してください。サービスの起動後、Emacs の準備ができます。ユニットファイルは /usr/lib/systemd/user/emacs.service に存在します。ユニットファイルの中身は以下のようになっています:

/usr/lib/systemd/user/emacs.service
[Unit]
Description=Emacs text editor
Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/

[Service]
Type=simple
ExecStart=/usr/bin/emacs --fg-daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
Environment=SSH_AUTH_SOCK=%t/keyring/ssh
Restart=on-failure

[Install]
WantedBy=default.target

systemd のユーザーユニットはログインシェル (~/.bash_profile など) から環境変数を継承しないため、~/.pam_environment に変数を設定すると良いでしょう。詳しくは Systemd/ユーザーを参照。

emacs をデーモンとして起動する場合、VISUALEDITOR 環境変数を emacsclient に設定することでプログラムからエディタを起動するときにエディタの新しいインスタンスを起動するかわりに emacsclient が使われるようになります。外部エディタを使用するプログラムとしてメールプログラム (メッセージを編集するため) や Git (コミットメッセージを編集するため)、less (v コマンドで表示されたファイルを編集するため) などが存在します。emacsclient の -n (--nowait) オプションは使用しないでください。基本的にプログラムはエディタが終了したときに編集が完了したと認識するためです。

GUI の Emacs のスタートメニューエントリなども emacs ではなく emacsclient が使われるように変更することを推奨します。

クイックスタート

Emacs は複雑ですが、そのカスタマイズと拡張性のレベルによってもたらされる利益を理解し始めるのにそう時間はかからないでしょう。さらに、幅広い拡張を利用することで Emacs はあらゆる種類の文章編集のための強力な環境に姿を変えることができます。

Emacs には素晴らしいチュートリアルが内蔵されており、スプラッシュ画面の最初のリンクをクリックしてアクセスできます。メニューから Help->Emacs Tutorial を選択するか 'F1' の後に 't' を押して下さい。このページは Emacs を始めるのに追加的なリソースになるように書かれています。

初心者にもエキスパートにも役立つ、リファレンスカードのセットも Emacs に含まれています、/usr/share/emacs/<version>/etc/refcards/ を見て下さい (<version> はあなたの emacs のバージョンに置き換えて下さい)。

基本的な用語と慣習

始めは慣れないと思うかもしれませんが Emacs にはいくつか特別な用語法と約束事があり、必要に応じて紹介していきます。ただ、Emacs を使うにあたって当然知ってるべきとされるような、基本的な用語についてはあらかじめ説明しておいたほうが良いでしょう。

真っ先に知るべきはバッファの概念です。バッファは Emacs の中のデータを表します。例えば、Emacs でファイルを開いたとき、ディスクからファイルから読み込まれてバッファに中身が保存され、バッファを編集して後でディスクに上書き保存することができます。バッファはテキストに限られず、画像やウィジェットなども含めます。作業中でもバッファでアプリケーションを表示することすらできます。こういう考え方もできます: ディスク上のデータは'ファイル'であり、一方、Emacs 上のデータは'バッファ'である。

Emacs におけるキーシーケンスの決まり事も見慣れないかもしれません。即ち:

C-x は Control-x を示します

M-x は Meta-x を示します

ノート: 'Meta' は多くの場合 Alt キーに相当します。また、Esc キーを使うことも可能です。

例えば、Emacs を終了するには次のキーシーケンスを使います: C-x C-c。これは "Control を押しながら 'x' を押す。手を離す。Control を押しながら 'c' を押す" と読み替えることができます。Emacs にはメニューバーが存在しますが、キーシーケンスを使いこなせるように練習することを推奨します。以下から、Emacs で使われるキーバインディングと慣習について説明します。

移動

カーソルの移動は他のグラフィカルエディタとよく似ています。マウスや方向キーを使ってカーソルの位置 (Emacs ではポイントと呼ばれます) を変更することができます。一般的な移動コマンドは方向キーで出来ますが、Emacs には他にも便利なバインディングが存在します。1文字先に移動するには C-f を使用して、1文字前に移動するには C-b を使用します。C-nC-p でそれぞれ次の行と前の行に移動できます。基本的に、マウスや方向キーを使うよりもこれらのキーシーケンスを使うことが推奨されています。

当然、Emacs には、単語や文単位で移動できる、もっと高度な移動コマンドが用意されています。M-f は1単語先に移動して M-b は1単語前にポイント移動します。同じく、M-e は1文先にポイント移動して M-a は前に戻ります。

ここまでは、紹介した全ての移動コマンドはポイントに関連しています。M-< を使うことでバッファの最初にポイントを移動することができ、逆に、M-> はバッファの最後に移動します。特定の行番号にポイントを移動するには、M-g g を使います。M-g g は移動したい行番号を尋ねます。また、現在の行の先頭と末尾に移動するには、C-a あるいは C-e を使って下さい。

ノート: 上記のコマンドを含む全てのコマンドのキーバインドは、現在有効になっているモードによって多少ながら変わってくる可能性があります。しかしながら、同一の機能を実行するのにコマンドを置き換えることはあまりありません。詳しくはモードを見て下さい。

ファイルとバッファ

Emacs にはファイルを操作するためのコマンドが用意されています。このセクションでは最もよく使われるコマンドを紹介します。ファイルを開くには C-x C-f を使います (Emacs ではこのコマンドを 'find-file' と呼びます)。指定されたファイルが存在しなかった場合、Emacs は空のバッファを開きます。バッファを保存するとバッファの中身が入ったファイルが作成されます。C-x C-s を使うことでバッファを保存できます。別のファイル名でバッファを保存するには、C-x C-w を使って下さい (this is a mnemonic for the command 'write-file')。新しいファイル名が尋ねられ、ディスクにファイルが書き込まれます。C-x s で全てのバッファを保存することも可能です。最後に保存されていた状態からバッファに変更が加えられている場合、保存を実行するかどうか訊くプロンプトが表示されます。

ノート: ファイルに対応するバッファが既に開かれている場合 C-x C-f はディスクからファイルを読み込みません。ディスクからファイルを再度読み込むには、(1) まずバッファを閉じて (C-x k)、それからファイルを開くか (C-x C-f) あるいは (2) M-x revert-buffer を使ってディスク上のデータに戻して下さい。それか (3) C-x C-v でファイルを現在のバッファにロードするか (4) C-x RET r RET で同一コードでファイルを再読み込みしてください。

"find-file" や "write-file" などのインタラクティブなコマンドでは Emacs ウィンドウの一番下の行に入力が求められます。この行のことはミニバッファと呼びます。ミニバッファでは基本的な編集コマンドを使うことができ、多くの *nix シェルで利用できるのと同じようなタブ補完機能があります。<TAB> を2回連続で押すと補完リストが表示され、マウスを使ってリストから補完候補を選択することも可能です。ミニバッファでの補完はコマンドやファイル名など様々な形で使えます。

ミニバッファには履歴機能も存在します。前に入力したコマンドは Up Arrow または M-p を使うことで再呼び出しできます。

ミニバッファは C-g を押すことでいつでも終了できます。

ファイルを複数開いたら、ファイルを切り替える手段が必要です。Emacs 内に既に存在するバッファにファイルを開くと、Emacs はそのバッファに切り替えます。しかしながらこれは効率的とは言えません。Emacs には表示するバッファを選択する C-x b が存在します (タブ補完が使えます)。存在しないバッファの名前を入力すると、その名前で新しいバッファが作成されます。

ノート: 前のバッファに切り替えるには C-x b <RET> を使います。前のバッファがデフォルトだからです。

全てのバッファのリストを C-x C-b で表示することができます。バッファが必要なくなったときは C-x k で削除することが可能です。

編集

Emacs には多数の編集コマンドが存在します。まだ紹介していない最も重要なコマンドはおそらく 'undo' でしょう。C-_ または C-/ で実行することができます。基本的に移動コマンドには対応する削除コマンドが存在します。例えば M-<backspace> で後方の単語を削除することができ、M-d で前方の単語を削除できます。行末または文章の終わりまで削除するにはそれぞれ C-k または M-k を使います。

1行は80文字を超えていはいけないという経験則が存在します。1行の長さを長くしすぎないことで、特にウィンドウの縁で改行してしまうような場合に、可読性を維持することができます。改行の自動挿入 (または削除) は Emacs では詰め込み (filling) と呼ばれます。M-q を使うことでパラグラフを折り返させることができます。

文字列と単語はそれぞれ C-tM-t で置き換えることが可能です。例えば Hello World!World! Hello と置き換えられます。

単語の大文字小文字はすぐに修正できます。M-l は単語を小文字に変換し (HELLOhello)、M-u は単語を大文字に変換します (helloHELLO)。そして M-c は単語の最初の文字を大文字にして他の文字は小文字にします (hElLoHello)。

kill と yank とリージョン

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.

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.

C-w and M-w can be used to kill and copy a region, respectively.

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.

検索と置換

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. 次の検索結果に行くにはC-sを、前の検索結果に行くにはC-rをおします。 もし探しているものが見つかったのであれば、<RET> を押すことで検索を終了できます。 Alternatively, should you wish to return to the location you initiated the search, use 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.

I-search has some useful commands. Use M-e to edit the search field. Use M-c to toggle case-sensitive matching.

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.

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-u 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'). For instance

C-u 80 %

will insert a line of percent signs. Or

C-u 4 M-d

Will delete 4 words. In this case, we provided the amount of words desired to the command invoked by M-d.

You can make it a little quicker by using the equivalent M-<number> as universal argument.

M-80 %

インデント

Indentation is usually performed with either <TAB>, to indent a single line, or with C-M-\, to indent a region. If the region is active (i.e. highlighted), then <TAB> will also indent 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>

ウィンドウとフレーム

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でバッファを表示している場所のことです。 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.

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').

From now on the definition of these terms as they exist in Emacs will be used.

To split the window horizontally or vertically 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.

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.

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.

ノート: These commands do not affect buffers. For example, deleting a window does not kill the buffer it displays.

モード

Emacs モードは Emacs Lisp で書かれた拡張であり、接続されているバッファの挙動を制御します。大抵の場合、インデントやシンタックスハイライト、テキストを編集するときのキーバインドを提供します。洗練されたモードは Emacs を完全な IDE (統合開発環境) に変貌させます。Emacs は基本的にファイルの拡張子でモードをロードすべきか判断します。

シェルスクリプトを編集するのに役立つモードとして 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 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:

C-c (	 Insert a function definition

C-c C-f	 Insert a 'for' loop

C-c TAB	 Insert an 'if' statement

C-c C-w	 Insert a 'while' loop

C-c C-l	 Insert an indexed loop from 1 to n

'line-number-mode' と 'column-number-mode' はマイナーモードです。マイナーモードはメジャーモードを拡張することができ、同時に任意の数のマイナーモードを有効にできます。

マニュアル

Emacs を本当にマスターしたいのであれば、一番よく読むべき文章はやはり公式マニュアルです:

  • 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 them 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 の可能性はわかりません。このセクションではより高度なテクニックや機能を紹介します。

ただし全てを説明するとなると長くなりすぎます。そのためこのセクションはあくまで Emacs の雅やかな機能をいくつかデモするだけにとどめます。

全ての機能の詳細な説明はドキュメントを見て下さい。

TRAMP

TRAMP (Transparent Remote Access, Multiple Protocols) は多数のプロトコルで、リモートファイルに透過的にアクセスすることができる拡張です。ファイル名を入力するときに、特定の形式を入力すると TRAMP が呼びだされます。例:

/etc/hosts を root 権限で開く前に root のパスワードを要求するには:

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

SSH で 'myhost' に 'myuser' として接続して ~/example.txt ファイルを開くには:

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

TRAMP のパスは '/[protocol]:[[user@]host]:<file>' という形式で指定します。TRAMP がサポートしているのは上記の例に限りません。詳しくは TRAMP の info マニュアルを参照してください。マニュアルは Emacs に付属しています。

キーボードマクロとレジスタ

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.

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

Prepare a register by invoking the `number-to-register' command (C-x r n) then storing the number '0' in register '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-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-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.

<RET> M-0 C-x e

The result:

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

If you want to save your macro for later use, you must give it a name and save it to your configuration file:

name-last-kbd-macro 
insert-kbd-macro

All defined macros are stored in the macro ring. To cycle between macros, use

C-x C-k C-n  next macro
C-x C-k C-p  previous macro

You can also use registers to save virtually anything.

C-x r SPC Copy current point (position) to register
C-x r w   Copy current window configuration to register.
C-x r j   Restore register.

So if you often work with windows side by side, for different project, you can save the configuration for the different projects and easily switch from one view to the other.

You can list used registers with the list-registers command.

正規表現

Emacs Manual より: "正規表現 (regexp と略す) とは、一致する可能性がある一連の (無限個でもよい) 文字列を表現するパターンである"。このセクションでは正規表現自体には深くは立ち入りません (単純に説明しきれない)。そのかわり正規表現のパワーを簡単にデモンストレーションします。詳しい説明は Emacs Manual の Regular Expressions セクションを見て下さい。

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
ノート: '^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 でも矩形選択は可能です。C-SPC で普通にテキストを選択してから、複数の矩形コマンドを使うことができます。

C-x r t  Replace rectangle with text.
C-x r k  Kill (and save in kill-ring) rectangle.
C-x r y  Yank rectangle.
C-x r o  Blank out rectangle.

たとえハイライトされていても、上記のコマンドが矩形の外のテキストに影響を与えることはありません。

ブックマーク

Emacs は開いたファイルのリストを記録できます。

C-x r m カレントバッファをブックマークに追加。
C-x r b ブックマークからバッファを開く。
C-x r l ブックマークの確認。

Elisp インタプリタ

Evaluate an elisp expression using eval-last-sexp (C-x C-e). Emacs always spawns a *scratch* buffer when started. It will not be saved to disk, feel free to add any text / code you want. It is especially useful for elisp evaluation. Note that this buffer starts using lisp-interaction-mode by default.

Alternatively, Emacs provides a top-level elisp interpreter with the ielm command.

スマートなウィンドウ切り替え

長い間使っていると C-x o による伝統的なウィンドウ切り替えは面倒かもしれません。windmove コマンドはもっと便利にウィンドウを切り替えることができます。Shift を押しながら方向キーでウィンドウを指定するだけです。

windmove キーを有効にするには、設定ファイルに以下を記述してください:

~/.emacs
(when (fboundp 'windmove-default-keybindings)
  (windmove-default-keybindings))

シェルコマンドの実行

Use M-! to call an external command. Use a prefix argument (C-u M-!) to output the result to current buffer at point.

You can use M-| on a region to use it as input for a command. For instance

C-u M-| sort -u RET

will sort region, remove duplicates and replace the region with the result.

シェルバッファ

You can spawn a shell buffer and execute commands just like you would do in any terminal. A classic shell buffer can be spawned with the shell command.

Emacs features a very powerful shell entirely written in Emacs Lisp, the eshell. The major advantage over shells like csh or zsh is that the native shell language is elisp itself. So you can use all advanced feature of elisp for your shell functions.

対応する括弧のハイライト

show-paren モードを使うことができます。デフォルトでは、対応する括弧が表示されるまで少し遅延がありますが、show-paren-delay を 0 に設定することでなくすことができます。

~/.emacs
(setq show-paren-delay 0)
(show-paren-mode 1)

スペルチェック

辞書を選択するには:

M-x ispell-change-dictionary

To check a single work use M-$. You can start checking the whole buffer with

M-x ispell-buffer

You can enable on-the-fly spell checking by enabling the flyspell-mode. For source code files you can restrict the mode to comments by using the flyspell-prog-mode instead. You will also need the aspell package for spelling in Emacs. There are corresponding packages for each language, so for English, you'd also want aspell-en (as well as aspell).

テーブル

Emacs comes with some powerful functions to handle and generate tables for various languages. Let's show an example.

Fruits  Quantity
Apples  5
Melons  2

Now select the previous content in a region, and run

table-capture

Use two spaces for the column delimiter, and a line break (C-q C-j) for the row delimiter. This will lead to the following result.

+------+--------+
|Fruits|Quantity|
+------+--------+
|Apples|5       |
+------+--------+
|Melons|2       |
+------+--------+

You can revert back the operation with

table-release

There is a lot of handy table-* functions, like table-insert-row, table-span-cell, table-widen-cell, etc.

Finally, the ultimate purpose of the table functions is to convert it to the desired markup language. Use the table-generate-source for that. For LaTeX, the previous table would result in

% This LaTeX table template is generated by emacs 24.2.1
\begin{tabular}{|l|l|}
\hline
Fruits & Quantity \\
\hline
Apples & 5 \\
\hline
Melons & 2 \\
\hline
\end{tabular}

Using the table mode you can also do some spreadsheet work like sum on rows and columns, but you will quickly find it limited. Besides it is quite slow. Have a look at the org-mode for much more powerful possibilities.

予定表、スプレッドシート、文章作成

Emacs can offer powerful office features thanks to the famous and powerful Org mode. This mode is part of the standard Emacs distribution.

Org mode is originally a powerful TODO and Agenda agent, but has quickly evolved to a much wider set of features. There is simply too much to tell about Org that we cannot aford even to skim over all the features. So we will only whet your appetite with a few exemples.

Open a new file TODO.org file, Org mode should be loaded. If not, switch to it with M-x org-mode.

TODO.org
* First entry
** Subentry
   Some comments
*** Subsubentry
* Second entry
  - List item 1
  - List item 2

Now a few useful bindings.

  • TAB to cycle-fold current entry.
  • S-TAB to cycle-fold all entries.
  • M-RET to start a new item on the same level as the current one.
  • M-<left> and M-<right> to change level.
  • M-<up> and M-<down> to move item together with all its subsections.
  • S-<left> and S-<right> to change status. On list items it will change the item style.
  • S-<up> and S-<down> to change priority.
  • C-c ^ to sort all subentries of the current entry.
  • C-c . to add a timestamp to the current entry. Use Shift and arrows to skip days or weeks. Use the mouse on the calendar or press Enter on a specific day to choose it.

The spreadsheet features are very comprehensive. Let's give an excerpt from the manual:

    Finally, just to whet your appetite for what can be done with the
 fantastic `calc.el' package, here is a table that computes the Taylor
 series of degree `n' at location `x' for a couple of functions.

     |---+-------------+---+-----+--------------------------------------|
     |   | Func        | n | x   | Result                               |
     |---+-------------+---+-----+--------------------------------------|
     | # | exp(x)      | 1 | x   | 1 + x                                |
     | # | exp(x)      | 2 | x   | 1 + x + x^2 / 2                      |
     | # | exp(x)      | 3 | x   | 1 + x + x^2 / 2 + x^3 / 6            |
     | # | x^2+sqrt(x) | 2 | x=0 | x*(0.5 / 0) + x^2 (2 - 0.25 / 0) / 2 |
     | # | x^2+sqrt(x) | 2 | x=1 | 2 + 2.5 x - 2.5 + 0.875 (x - 1)^2    |
     | * | tan(x)      | 3 | x   | 0.0175 x + 1.77e-6 x^3               |
     |---+-------------+---+-----+--------------------------------------|
     #+TBLFM: $5=taylor($2,$4,$3);n3

Org mode recognizes a table when the pipe | is the first non-whitespace character on line. In the previous text, no line was drawn. This is done automatically when TAB is pressed after an entry or the |- sequence. The result column was not written by hand neither, it is computed from the calc formula on the last line. Use C-u C-c C-c to recompute all values in a table.

There is also some handy row and column manipulation bindings like those for the TODO file. Alt plus arrows will move columns and arrows.

For more in-depth details, refer to the official manual: C-h i m Org mode RET.

リファクタリングとスマート補完

(Eclipse などの) IDE が大抵備えているプログラミング機能が必要な場合、Semantic ツールが役に立ちます。Semantic は Emacs の標準ディストリビューションに含まれています。現在 C, C++, Scheme, Javascript, Java, HTML, Make がサポートされています。現在のサポート状況は CEDET のページ で確認できます。

Open a file in your favorite programming language supported by Semantic and turn on the Semantic minor mode with

M-x semantic-mode

Semantic will work for a few seconds parsing the libraries included from your file (in C that would mostly be the standard library for instance).

機能

Once done, you can start using the great Semantic features:

Smart completion

Press C-c , SPC to complete a symbol at point. If it is an argument, it will check for type correctness.

Jump to symbol

Press C-c , j to prompt for a symbol and jump to its definition. You can use auto-completion with TAB. Use a capital J to search for symbol accross files.

List symbol calls

Press C-c , g to prompt for a symbol and list the places where it is being referred to. You can use n and p to navigate through the resulting entries. Press RET to toggle details, and RET again on a reference to jump to it. Use a capital G in the first command to work across files.

Refactoring

First list use of the symbol you want to refactor with the aforementioned command. Now press ( to start defining a macro on the symbol. This is actually much more powerful than refactoring since you can apply any function to the symbol and even act on its surrounding symbols. Press C-x ) to finish the macro and E to call it.

Describe symbol

Call the following function to display details on a symbol.

semantic-ia-show-summary

This can be very useful for complexe data structures and function prototypes. There is no binding by default.

設定

Here follows a sample configuration for use of Semantic with all supported programming languages: an example binding and a display configuration.

;; Semantic with ghost display (allows M-n and M-p to browse completion).
(semantic-mode 1)
(define-key my-keys-minor-mode-map (kbd "C-c , d") 'semantic-ia-show-summary)
(setq semantic-complete-inline-analyzer-displayor-class 'semantic-displayor-ghost)

You can also add some specialization for a specific language. Here for C:

(add-hook
 'c-mode-hook
 (lambda ()
   (local-set-key (kbd "M-TAB") 'semantic-complete-analyze-inline)
   (local-set-key "." 'semantic-complete-self-insert)
   (local-set-key ">" 'semantic-complete-self-insert)))

文献

Emacs から直接 info マニュアルにアクセスすることが可能です: C-h i m Semantic RET

Emacs を git のマージツールとして使う

By default, Git provides support for using Emacs' Emerge mode as a merge tool. However you may prefer the Ediff mode. Unfortunately this mode is not supported by git for technical reasons. There is still a way to use it by evaluating some elisp code upon emacs call.

.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

Note that the command has to be on a single line. In the above example, we launch a new instance of Emacs. You might want to use emacsclient for quicker startup; it is not recommended though since the Ediff call is not really clean: it could mess with your current Emacs session.

If you want an instant startup you can use the -q parameter. If you want to launch Emacs quickly while preserving at least a part of your configuration, you can call Emacs with

 emacs -q -l ~/.emacs-light

where the light configuration file loads only what you need for Ediff.

See kerneltrap.org and stackoverflow for more details on this trick and the Ediff issue.

Caps Lock キーを Control キーとして使う

いわゆる 'emacs 小指' にならないようにこちらの挙動を好むユーザーもいます。X で試用してみたいときは、次を実行:

$ setxkbmap -option 'ctrl:nocaps'

また、キーを交換するには、次を実行:

$ setxkbmap -option 'ctrl:swapcaps'

永続的に設定したくなったら、.xinitrc ファイルに上のコマンドを追加してください。

リージョンを小文字から大文字にする必要がある場合は、デフォルトの C-x C-u キーバインディングを使って下さい、upcase-region 関数が呼び出されます。

Caps Lock がない場合は、両方の "Shift" にマッピングしてください:

$ setxkbmap -option "shift:both_capslock"

一部のデスクトップ環境にはキーボードのマッピングを変更するためのグラフィカルなツールが含まれています。例えば Plasma 5 ならシステム設定を開いて入力デバイスをクリックしてください。キーボードを選択して高度な設定タブから Caps Lock を Ctrl として使用するように設定できます。

カスタマイズ

Emacs は ~/.emacs を編集するか M-x customize を使うことで設定をすることができます。このセクションでは手動で ~/.emacs を編集する方に焦点をあて、いくつかのカスタマイズ例を示すことで、一般的な Emacs の設定をデモンストレーションします。customize コマンドは調整を行うためのシンプルなインターフェイスを提供しますが、おそらく Emacs に慣れてくるにつれて制約を感じるようになるでしょう。

All of the examples here can be performed while Emacs is running. To evaluate the expression within Emacs, use:

C-M-x with point anywhere within the expression.

or

C-x C-e with point following the last ')'

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)

カーソルの点滅を止めるには、次を使用:

(blink-cursor-mode -1)

同じく、前のセクションにある column-number-mode を有効にするには:

(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)
(menu-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'.

(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 にどれか一つをロードさせることができます。

例えば、2つの設定ファイルを定義してみましょう。

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

This is the full configuration we load for the daemon. But the plugins file is huge and slow to load. If we want to spawn a new Emacs instance that does not need the plugins features, it can be cumbersome to load it everytime in the long run.

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

And now we launch Emacs with

emacs -q -l ~/.emacs-light

You can create an alias to ease the call.

ローカル・カスタム変数

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, see the manual for complete details

  • Using M-x add-file-local-variable-prop-line, which adds a commented line at the beginning similar to:
// -*- my-compiler:g++; mode:c++ -*-
  • Or you can use M-x add-file-local-variable to add lines near the end of the file:
// Local Variables:
// my-compiler: g++
// mode: c++
// End:

Note that for the values to take effect, you will need to call M-x revert-buffer.

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

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.

カスタムカラーとテーマ

face を使うことで簡単に色をカスタマイズすることができます:

(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 サポート

Emacs は強力な LaTeX エディタです。LaTeX モードを必要に応じて自由に編集・作成することができます。

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.

Systemd ファイルのシンタックスハイライト

systemd-mode を使うことができます。

もしくは init ファイルに以下を追加することで、簡単に emacs で systemd のファイル (サービスやタイマーなど) をカラー表示できます:

 (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 に追加してください [1]:

;; 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.elpackage-archives 変数を使ってパッケージを検索します。デフォルトでは Emacs Lisp Package Archive (ELPA) が含まれています。M-x list-packages で Emacs が認識している全てのパッケージが列挙されたバッファが作成されます。マニュアル ((info "(emacs) Packages")) にはさらに詳しい情報が載っています。

サードパーティ製のパッケージのアーカイブを追加することもできます。MELPA が広く使われています。

人気の拡張の多くは [community] リポジトリのパッケージや AUR からインストールできます。大抵の場合、パッケージの名前の前に 'emacs-' が付きますが (例: emacs-lua-mode)、常にそうなっているわけではありません (例: auctexAUR)。

ヒント: Arch Linux の Wiki を編集している場合は Emacs Mediawiki パッケージが役に立ちます。

パッケージによっては設定ファイルに変更を加えないと機能が有効にならない場合があります。例えば、auctexAUR をインストールしたときは、設定ファイルに以下を追加:

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

パッケージのソースコードや README で有効化する方法が書かれているはずです。

トラブルシューティング

Emacs が 'Undefined color: "WINDOW_FOREGROUND"' というエラーメッセージで起動しない

mcpp パッケージまたは gcc パッケージをインストールする必要があります。xrdbX resources を前処理するために C のプリプロセッサ cpp を使用します。C のプリプロセッサがインストールされていない場合、xrdb は C のプリプロセッサを実行しないため WINDOW_FOREGROUND が16進数のカラーコードに展開されません。

カラー出力の問題

デフォルトでは、Emacs シェルはカラー表示に使われるエスケープシーケンスをそのまま表示してしまいます。つまり、カラー出力がされるべきところに変な記号が表示されます。

この問題を解決するには以下を ~/.emacs に記述してください:

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

X ウィンドウでの文字の表示問題

X ウィンドウで emacs を起動した時にメインウィンドウの文字が全て黒縁の白四角になってしまう場合 (対応するフォントがインストールされていない文字を表示したときと同じような記号)、xorg-fonts-75dpixorg-fonts-100dpi をインストールして X ウィンドウを再起動してください。

起動が遅い

ヒント: systemd ユニットとして Emacs を起動する方法でも起動時間を短縮できます。

しばしば起動時間が遅くなる原因は2つあります。

どちらが原因なのか調べるために、次のコマンドで Emacs を実行してください:

$ emacs -q

このコマンドでも Emacs の起動が遅い場合、不適切なネットワーク設定を参照してください。そうでない場合は、ほぼ確実に .emacs に問題があります。

  • /etc/hosts の設定が間違っていると、Emacs の起動が5秒以上遅くなることになります。詳しくはネットワーク設定ガイドの 'ホストネームの設定' を見てください。
  • 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:
(require 'anything)

you might use:

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

Cannot open load file: ...

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:

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

 (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'.

デッドアクセントキーの問題: '<dead-acute> is undefined'

このバグについて Google で検索すると、次のリンクが見つかります: 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 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 (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 classified as a feature ... Maybe it's just because the file is auto-loaded though pretty useless.

C-M-% やその他のバインディングが emacs nox で動作しない

これはターミナルが Xorg よりも制限的であることが原因です。ただし、ターミナルによっては多くのバインディングを扱うことができることがあります。2つの解決法が考えられます:

  • グラフィカルバージョンを使用する。
  • サポートされているバインディングに変更する。

例:

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

Emacs がフリーズする

シングルスレッドであるがゆえに、Emacs は様々な操作でブロックします。フリーズが発生する状況は様々です。例えば、Emacs はあなたからの入力を待っているのかもしれません (例えばあるフレームでミニバッファを開いているのに他のフレームで作業しようとしている場合など)。あるいは、Emacs は完了するまで時間のかかるコードを実行中なのかもしれません。あるいはバグを踏んだという可能性もあります。Emacs プロセスを殺さずに Emacs のブロックを解除する方法は複数存在します。

  • C-g を押してみる。Emacs が行っていることによっては、複数回押す必要があります。
  • ESC ESC ESC を押してみる。
  • 他のターミナルで killall -SIGUSR2 emacs を実行する。

Emacs-nox の出力が汚い

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. If this happens you can do M-x redraw-display, which will redraw the terminal's display. If this problem happens frequently, you might want to bind the command to a key, e.g. by putting something like

(global-set-key (kbd "<f7>") 'redraw-display)

in your .emacs file.

Graphical Emacs does not suffer from this issue.

emacs ターミナルにおかしなエスケープ数字 (utf-8) が表示される

.bashrc または .zshrc で以下の変数をエクスポートしてください:

~/.bashrc
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export TERM=xterm-256color

小文字の utf を使っていることがエラーの原因です (例: en_US.utf-8)。

利用可能なロケールは locale -a で確認できます。

tmux で emacs を使っているときに Shift + 矢印キーが動作しない

まず tmux の設定で xterm-keys を有効にしてください:

.tmux.conf
setw -g xterm-keys on

しかしながら、この設定は他のキーコンビネーションを破壊します。それを修正するために、以下を emacs の設定に記述します:

~/.emacs
(defadvice terminal-init-screen
  ;; The advice is named `tmux', and is run before `terminal-init-screen' runs.
  (before tmux activate)
  ;; Docstring.  This describes the advice and is made available inside emacs;
  ;; for example when doing C-h f terminal-init-screen RET
  "Apply xterm keymap, allowing use of keys passed through tmux."
  ;; This is the elisp code that is run before `terminal-init-screen'.
  (if (getenv "TMUX")
    (let ((map (copy-keymap xterm-function-map)))
    (set-keymap-parent map (keymap-parent input-decode-map))
(set-keymap-parent input-decode-map map))))

詳しくは tmux FAQ を参照。

KDE でウィンドウのリサイズがおかしい

KDE ユーザーには Emacs のウィンドウが正しくリサイズされないという現象がおこることがあります。リサイズした部分が透過されマウスでクリックしても下のウィンドウがクリックされます。この挙動を修正するには、KDE の GTK3 テーマを oxygen-gtk 以外のテーマに変更してください。例えば、gtk3 に含まれている Emacs テーマを使って下さい。

KDE で Emacs を完全に最大化するには、タイトルバーの Emacs アイコンをクリックして、More Actions > Special Window Settings を選択してください。そして "Size & Position" タブで "Obey geometry restrictions" を選択して、ドロップダウンメニューで "Force" を選んで右のラジオボタンで "No" を選んで下さい。

派生

Emacs の実装は多数存在します。おそらく GNU/Emacs が一番ポピュラーです。

軽量な Emacs 互換のテキストエディタは Arch のリポジトリや AUR にあります。

mg

mg (旧名 MicroGnuEmacs) は C で書かれた軽量な Emacs の実装です。

mg パッケージから入手できますが、上流の ページ からソースをダウンロードすることも可能です。mg は UTF-8 をサポートしていません。

zile

公式ウェブページによると "GNU Zile は軽量な Emacs クローンです。Zile は Zile Is Lossy Emacs の略となっています。Zile は出来る限り Emacs に近づくように書かれており、きっと全ての Emacs ユーザーの手に馴染むでしょう"。Zile は UTF-8 をサポートしていません。

zile パッケージからインストールできます。

最新の tarball は公式の GNU ミラー から落とせます。

uemacs

Linus Torvalds によるカスタム版 Micro-emacs。uemacs-gitAUR でインストール可能。

最新 (2005) の tarball は こちら にあります。

remacs

remacs はコミュニティによる Emacs の Rust 移植です。remacs-gitAUR パッケージでインストールできます。

参照