Emacs
From ArchWiki
Emacs is an advanced text editor often compared with vi* or vim*. Its core is a elisp interpreter (the emacs dialect of LISP), which is extended by elisp scripts. It has an X frontend that supports GTK as of version 22, but you can also use it from the command line.
*Note that the Church of emacs has found that Vi/Vim may cause dry mouth, blurred vision, drowsiness, dizziness, profuse sweating, tremors, sexual problems, headache, nausea and abdominal pain.
Contents |
[edit] Installation
To install regular GNU emacs execute this as root
pacman -S emacs
There are also alternatives like xemacs.
[edit] Quick Start
The complexity of Emacs may deter the novice, but once you made your first steps and learned your first control sequences, you will realize the benefit of using Emacs and the excellent support you can get by its various plug-ins. These packages make Emacs into a highly efficient tool for Java programming, LaTeX type setting or HTML editing.
Break the barrier now! Dare to dive into Emacs!
This short tutorial is meant to guide you in your first steps and give you all the knowledge to beat Vi users in speed and safety.
[edit] Starting Emacs, File Commands
Start your emacs editor:
emacs mytextfile
or if you want to use it from the console:
emacs -nw
Emacs is configured in ~/.emacs, and it is controlled by keyboard shortcuts.
You are probably used to the reappearing menu pattern from GUIs: File, Edit, View, Help. Emacs has a structure that looks similar in the beginning, but turns out to be badly organized--flooding you with a lot of irrelevant functions, but hiding the most basic editing commands like cut & paste from you. In Emacs you are better off learning the control key sequences rather than fussing with the menu. You will find it much more efficient once you become proficient at it.
The Emacs community has a special tradition of naming the key modifiers CTRL and ALT:
C-x CTRL-x
M-x Meta-x, i.e., ALT-x or ESC-x
Here is a list of the basic file commands:
C-x C-f Open... (Find file)
C-x C-s Save
C-x C-w Save as... (Write file)
C-x s Save all...
C-x k Close... (Kill buffer)
C-x C-c Exit (Close emacs)
Most interactive commands like "find file" or "write file" prompt for input in the so-called mini buffer, the last line of the emacs window. The minibuffer supports edit commands like cut & paste and, what is very comfortable, the file name completion known from Unix shells: Just press the tab key to complete a filename, and space to get a list of possible completions, from which you can select one with the mouse. Navigating through your file structure with the keyboard and completion is much faster than double-clicking through file dialogs. The minibuffer also facilitates a history: repeated up brings back the last inputs. Minibuffer commands:
tab Filename completion
space List completions
up History invokation
[edit] Editing Commands
Cursor movement and basic editing is almost identical to Windows editors. Here is a list of useful deleting commands:
C-d Delete (to the right)
M-d Delete word
C-k Delete to the end of the line (Kill characters)
Essential for editing is cut & paste. All deleting commands that delete more than one character are indeed cut-commands. They accumulate the killed characters in the cut buffer until you move the cursor with other commands. If you start cutting more text then, the cut buffer will be emptied before accumulating the new characters. Another way to cut is to mark text and killing the selection, region in Emacs terminology. Here are the keys for cut & paste:
C-space Start selection
C-w Cut
M-w Copy
C-y Paste (yank)
C-_ Undo
Usually you will cut text by a sequence of C-k commands or by C-space, marking a region and C-w and then paste it back with C-y. B.2.1 Find and Replace
C-s Find... (Search)
C-s C-s Find again
M-% Replace...
C-g Abort
Find and replace always start at the cursor position. If you want to move to the next occurence in a search, just hit C-s again.
After entering text and replacement you are prompted at each occurrence whether to replace or not. You have the options yes, no, ! to replace all and C-g to abort. (Abort and undo work in all contexts.)
[edit] Indentation
Many Emacs plug-ins (modes in Emacs terminology) provide special indentation modules for the supported programming language. You invoke these modules by:
tab Indent line
M-C-\ Indent region
If you do not have a special Emacs mode for you file syntax, you have to indent manually. To indent a region, mark it and indent it with C-x tab. The region then becomes inactive, but you can reactivate it with M-C-z and indent it for one more column. A better way is to supply an argument to the indentation command and indent the region as far as you want in one step. You can provide arguments to commands by C-u <arg>. Examples:
C-u 4 C-x tab Indent the region by four columns.
C-u -2 C-x tab Unindent the region by two columns.
A good default indentation is provided in the "indented text mode". You can switch to it by invoking the command `indented-text-mode', which has no key sequence binding. Commands without a key sequence bound to them are launched by M-x <command>. Completion with tab also works for commands, therefore type:
M-x indented tab
[edit] Editing Several Files
Emacs is designed for convenient editing of several files at a time. It has a clean philosophy of "multi editing", by distinguishing buffers, frames and windows.
Buffer: Data structure to hold a file to edit or a process to interact.
Frame: An Emacs "window" (in standard terminology) with title and menu bar. It contains one or more windows (in Emacs terminology).
Window: A horizontal part of the frame (usually the whole free area or half of it) with a status bar displaying file name etc. A window displays one buffer. Several windows can display the same buffer.
Here are some commands to change the buffer displayed in the current window:
M-C-l Switch back to the previously displayed buffer.
C-x b Switch to entered buffer (completion with tab works)
C-x C-b Display buffer list.
C-x k Kill buffer.
You will find that you often need M-C-l. An alternative to C-x b is C-x C-f, since the latter does not load the file again if it already occupies one buffer. Window and frame commands:
C-x 2 Split window into two
C-x 1 Unsplit window (keep this)
C-x 0 Unsplit window (keep others)
C-tab Switch window
C-x 5 2 New frame
C-x 5 0 Delete frame
All these commands do not affect the buffers. Deleting a window does not kill the buffer.
[edit] Modes and Customization
This is where the big potential of Emacs is present, and here it gets complicated. We focus on some very basic issues.
An Emacs mode is a plug-in written in Emacs LISP that controls the behaviour of the buffer it is attached to. Usually it provides indentation, syntax highlighting and hotkeys for compiling and jumping to error locations. Sophisticated modes turn Emacs into a full-fledged IDE (integrated development environment). Emacs decides on base of the file extension which editing mode is loaded.
Useful modes for editing Tutch files are the indented text, line and column number mode. They can be used in parallel and are invoked by
M-x indented-text-mode return
M-x line-number-mode return
M-x column-number-mode return
The indented text mode is one of the major modes, which are exclusive. The other two are minor modes which can be used as add-ons onto any major mode. Keys for the indented text mode are:
tab Indent line like the previous one.
C-e C-j Start new line with indentation (use instead of return).
You can configure Emacs to load these modes automatically when loading `.tut'-files. Personal customization of Emacs is put into the file `~/.emacs'. Here are the necessary lines to add:
(command-execute 'line-number-mode)
(command-execute 'column-number-mode)
(setq auto-mode-alist
(append
'(("\\.tut$" . indented-text-mode)
("\\.req$" . indented-text-mode))
auto-mode-alist))
[edit] More Editing Commands
Here is a random list of some useful commands:
M-q Justify paragraph.
M-t Transpose words.
M-- M-l Make word lower case.
M-- M-u Make word all upper case.
M-- M-c Make word capital.
Here are more deleting commands:
M-backspace Backspace word
M-k Delete paragraph
M-\ Delete spaces
M-space Delete spaces, leave one
M-^ Join two lines
[edit] Troubleshooting
[edit] Problems displaying characters in X Windows
If when you start emacs in X windows all the characters in the main window are white boxes with black borders (the ones you see if you try to view characters for which you do not have the correct font installed), you need to install xorg-fonts-75dpi and/or xorg-fonts-100dpi and restart X windows.