Emacs - How to Use
Emacs (derived from Editing MACroS) is a popular text editor used mainly on
UNIX-based systems by programmers, engineers, students, and system
administrators. It was created by Richard Stallman at MIT. It is written in
LISP and provides advanced features that let you add, delete, insert, and
otherwise manipulate text. Emacs is commonly used to enter the source code for
programs. Emacs also offers a number of convenient capabilities such as the
ability to initiate a program compiler and to handle e-mail from within the
editor.
A popular version of Emacs is GNU Emacs. This is the version used by the TAMU
Computer Science Department. Emacs has an X-windows version too named XEmacs.
Pre-requisite information
You should be able to access your CS Unix account by logging on to the CS unix
server interactive.cs.tamu.edu. If you are unable to log in, go to the helpdesk in Room # 210.
Entering Emacs
The usual way to invoke Emacs is with the shell command emacs. Once you log in
to your Unix CS account, at the prompt, type
emacs filename
Note : On a text-only terminal(putty or Tera Term), the Emacs display
occupies the whole screen. On the X Window System, Emacs creates its own X
windows to use.
If filename exists, Emacs clears the screen and displays the contents
of the file else Emacs clears the screen and then displays an initial help
message and copyright notice as shown (Fig. 1).
Fig 1.
Note : In Emacs, usually keystrokes/key combinations start with Control or Alt,
which correspond to the Ctrl or Alt keys on the keyboard. Hence, if a keystroke
is Control-q, it is denoted at C-q and indicates that you need to press the
“Ctrl” key and the letter “q”. Similarly, Alt-q will
denoted as M-q and indicates that you need to press the “Alt” key
and the letter “q”.
Emacs works on the concept of buffers. Any file that you open using Emacs is a
buffer. Hence, buffer and file shall be used interchangeably in this tutorial.
Exiting Emacs
To exit emacs, press Ctrl x Ctrl c. If the file you are editing has been
modified you will be prompted to save the changes. Respond with a yes(y) or
no(n). You will be brought back to the command prompt.
Getting Help
Emacs has extensive online help, most of which is available via the help key, C-h.
Type C-h twice to see a list of subcommands; type it three times to get a
window describing all these commands
Some of the most useful help commands are:
-
C-h t
help-with-tutorial. Run the Emacs tutorial. This is very helpful for beginners.
-
C-h a
command-apropos. Prompts for a keyword and then lists all the commands with
that keyword in their long name.
-
C-h k
describe-key. Prompts for a keystroke and describes the command bound to that
key, if any.
Note: In Emacs, most editing commands(move, delete, etc) are arranged
around textual objects (typically characters, words, lines and
paragraphs, though this is customizable according to the mode you are working
in).
Move commands
Emacs provides many motion commands, which are arranged around textual objects.
All commands take numeric arguments are repetitions. Following are some
motion commands that are available :
-
Characters
-
C-f forward-char. Moves forward (to the right) over a character.
-
C-b backward-char. Moves backward (to the left) over a character.
-
Words
-
M-f forward-word. Moves forward over a word.
-
M-b backward-word. Moves backward over a word.
-
Lines
-
C-n next-line. Moves down to the next line.
-
C-p previous-line. Moves up to the previous line
-
C-a beginning-of-line. Moves to the beginning of the current line.
-
C-e end-of-line. Moves to the end of the current line
-
Sentences
-
M-a backward-sentence. Moves to the beginning of the current sentence.
-
M-e forward-sentence. Moves to the end of the current sentence.
-
Paragraphs
-
M-{ backward-paragraph. Move to the beginning of the current paragraph.
-
M-} forward-paragraph. Move to the end of the current paragraph.
-
Pages
-
C-x [ backward-page. Moves to the beginning of the current page.
-
C-x ] forward-page. Moves to the end of the current page.
-
Buffers(files)
-
M-< beginning-of-buffer(file). Moves to the beginning of the buffer.
-
M-> end-of-buffer(file). Moves to the end of the buffer.
Edit commands
-
INSERT
Emacs is “self inserting”, which means that you can start typing in
as soon as the file is opened, no need of any special insert commands.
-
DELETE (Kill)
-
Characters
-
C-d delete-char. Deletes the character to the right of the cursor or the
current character.
-
DEL delete-backward-char. Deletes the character to the left of the
cursor. Backspace works the same as DEL.
-
Words
-
M-d kill-word. Kills to the end of the word to the right of the cursor
(forward).
-
M-DEL backward-kill-word. Kills to the beginning of the word to the left
of the cursor (backward)
-
Lines
-
C-k kill-line. Kills to the end of the current line, not including the
newline. Thus, if you're at the beginning of a line it takes two C-k's to kill
the whole line and close up the whitespace.
-
C-u 0 C-k kill-line. Kills to the beginning of the current line, not
including the newline.
-
Sentences
-
M-k kill-sentence. Kills to the end of the current sentence, including
any newline within the sentence.
-
C-u -1 M-k kill-sentence. Kills to the beginning of the current
sentence, including any newlines within the sentence.
-
COPY/CUT(or Kill and yank)
-
Copy works the same as deleting and undeleting. Selected or marked text is
killed and yanked back.
-
The yank command is C-y Yank last killed text.
-
Cut is the same.
-
Search commands
Emacs has a facility called the incremental search. There are
basically two commands.
-
C-s isearch-forward. it searches incrementally, one character at a time,
as you type the search string. This means that Emacs can often find what you're
looking for before you have to type the whole thing. You can search for the
next match at any point by typing another C-s at any point.
-
C-r isearch-backward. works the same way, but searches backward
-
For non-incremental search, type either C-s or C-r and
hit Enter.
-
For text search(non-incremental), type C-s and hit Enter and
then type text and hit Enter, where text is the text that you want to search
for.
-
Replace commands
Emacs' most important command for replacing text is called query-replace (bound
to M-%). This command prompts you for the text to replace, and the text
to replace it with, and then searches and replaces within the current buffer.
query-replace is interactive: at each match, you are prompted to decide what to
do; you have the following options:
-
SPC Perform this replacement.
-
DEL Don't perform this replacement.
-
RET(Enter) Terminate query-replace without performing this replacement.
-
ESC Same as RET.
-
. Perform this replacement but then terminate the query-replace.
-
! Perform this replacement in the rest of the file without querying (ie
unconditionally).
-
Text selection commands
In Emacs, the selected text chunk is called a region. Various operations can be
performed on a region. To create a region,C-@ set-mark-command.
This sets the mark exactly where the cursor is, and you can move the cursor
elsewhere and you have: the region.
There are some special commands that are
specifically designed to set the region around some interesting text.
-
M-@ mark-word. Sets the region around the next word, or from point to
the end of the current word, if you're in the middle of one.
-
M-h mark-paragraph. Sets the region around the current paragraph.
-
C-M-@ mark-sexp. Sets the region around the same sexp that C-M-f would
move to.
-
C-x C-p mark-page. Sets the region around the current page.
-
C-x h mark-whole-buffer. Sets the region around the entire buffer.
-
Indentation
In programming language modes, Emacs uses the TAB key to indent a line
automatically, in accordance with indentation rules for your programming
language. Emacs allows you to customize the indentation for your own style.
-
Modes
Emacs customizes commands for different kinds of text is through major and
minor modes. Every buffer has a major mode, and may have zero or more minor
modes. Sometimes Emacs chooses a major mode for you automatically, typically
based on a file extension (e.g., files ending in .c will automatically be in C
Mode). Some major modes are :
-
Fundamental Mode
The basic mode in reference to which all specialized modes are defined.
Perfectly fine for editing any kind of text, just doesn't provide any special
features.
-
Text Mode For editing text. Has special commands for spell-checking,
centering lines, etc.
-
Outline Mode For editing a stylized type of outline. Implements folding
of outline levels, etc.
-
Lisp Mode For editing Common Lisp source code. Has an interactive link
to a Common Lisp interpreter in another buffer.
-
Tcl Mode For editing Tcl source code. Has an interactive link to a Tcl
interpreter in another buffer.
-
C Mode For editing C source code. Has special indentation, etc.
To Top of Page
©CSG Helpdesk