emacs
The emacs
Screen
Mode
Line: The reverse video line at the
bottom which contains info such as a write-enabled/read-only indicator, buffer
(file) name, editing mode (fundamental is default), the current line number and
the cursor’s relative position in the buffer, the current character mode
(insert is default) and auto-fill mode.
Mini-buffer: the line at the bottom of the screen at which you can type
commands
Function Key
Conventions
Editing functions and most editing
commands are bound to keystroke combinations. These usually consist of a
special key and one character (the character is usually the first letter of the
associated command). Characters typed
in bound keystroke combinations are not case sensitive. Note: The function keys
referenced in this document are those defined by WinQVT’s use of the
unh_keys.km keymap file.
Bound Keystrokes
CTRL-[KEY] press CTRL and
[KEY]
executes
a specific command
must
press CTRL and desired key simultaneously
example:
CTRL-s (repeat last search)
ESC
[KEY] press
ESC then press [KEY]
executes
a specific command
must
press ESC and desired key sequentially
this
is also called the "meta" key and used as a “gold” key
THE
F11 KEY IN WINQVT EXECUTES ESC x
example:
ESC > (move to end of buffer)
CTRL-x
[KEY] press
crtl-x then press [KEY]
executes
a different command than CTRL-[KEY]
example:
CTRL-x u (undo last edit)
CTRL-[KEY1] CTRL-[KEY2] press
CTRL-[KEY], release, then press CTRL-[KEY2]
example:
CTRL-x CTRL-s (write buffer to file)
Do
not type spaces between keystrokes during complex functions
example:
ESC 15 CTRL-x e (execute stored
macro15 times)
Entering
Commands
ESC x invokes the
mini buffer (command prompt)
ESC x
<command> press
ESC, then press the "X" key, then enter the actual command name you
wish to execute
If you don't know
the entire command:
Enter
the beginning fragment and press return (or TAB).
emacs
will complete the word you entered, or present a list of possible commands.
Cursor
Movement and Navigation
Up
UpArrow
Down
DownArrow
Left
LeftArrow
Right RightArrow
Note: emacs will wrap around the beginning
and end of a line. A continuation character will appear at the end of a line of
wrapped text. Use ESC x auto-fill-mode
to toggle word wrap on and off; the status line will indicate fill mode.
End of Line CTRL-e
Start of Line CTRL-a
End of buffer ESC
>
Start of buffer ESC
<
Previous Screen END
key
Next Screen Page
Down key
Go to line y ESC x goto-line y (where y
is the desired line number)
What line am I on ESC x what-page
Search forward INSERT key in WinQVT
Incremental Search Forward CTRL-s
(use this same key to repeat your forward search)
Incremental Search Backward CTRL-r (use this same key
to repeat your backward search)
Quit Search after finding target [RETURN]
Quit Search
before finding target CTRL-g (quit
while search is still in progress)
Editing
Functions
Toggle Insert/Overwrite [HOME]
Toggle read-only
mode CTRL-x
CTRL-q (percent signs will appear in the status line)
By default, emacs will not overwrite
existing text, rather, it will insert the rectangular or linear text and move
any existing text over or down. Also,
note that in emacs y means yank something in but in vi y means yank something out.
Select text CTRL-@
Select entire
buffer CTRL-x h
Cut selected text
(linear) CTRL-w
(remaining text is moved left)
Copy selected text(linear) ESC w (kill-ring-save)
Paste stored text (linear) CTRL-y (existing text is moved right)
The
Kill Ring: The kill ring is a buffer in
which cut text is stored. It is called a ring because it stores all of your cut
text in sequence. CTRL-y yanks back only the most recently cut text from
the kill ring. Following CTRL-y with ESC y will yank back the next most
recently cut text. Continue pressing ESC y to retrieve each section of cut text
in sequence starting with the most recent one first. There is no kill ring for
rectangular text.
Cut selected text
(rectangular ) CTRL-x r k (rectangular kill)
(remaining text is moved left)
Copy selected text (rectangular) ??? (no kill ring for rectangular text)
Paste stored text (rectangular) CTRL-x r y (rectangular yank) (existing
text is moved right)
Clear selected region (rectangular) CTRL-x r c (cleared text is not stored in
the kill ring)
Delete selected region (rectangular) CTRL-x
r d (deleted text is not stored in the kill ring)
Open rectangle CTRL-x
r o
Search-and-replace ESC x replace-string (unforgiving)
Query-and-replace ESC % y
= replace and find next iteration (or spacebar)
n =
skip this one and find next iteration
. = replace and then quit search
, = replace but then let me view the result
! = replace all automatically without asking
q = do not replace this one and quit search
Note: The replace functions work from the
cursor forward and do not wrap to the beginning of the buffer.
You must first move to the beginning
of the buffer to affect the entire buffer.
Delete word ESC
d
Delete line CTRL-k
Capitalize word ESC c
Uppercase Word ESC u
Uppercase Region CTRL-x CTRL-u
Editing
Functions (continued)
Lowercase Word ESC l
Lowercase Region CTRL-x CTRL-l
Note: The word formatting functions will only affect
characters to the right of the cursor up to the next space. To affect the entire word, the cursor must
be on the first character of the word.
To remove spaces from the end of each
line:
ESC x
edit-picture (to enter picture mode)
CTRL-c
CTRL-c (to exit picture mode – this removes trailing spaces)
Windows and Buffers
Windows…
Split window horizontally CTRL-x 2
(some functions don’t work while the window is split)
Split window vertically CTRL-x 3
Other window CTRL-x o
Close all other windows CTRL-x 1
Close the current window CTRL-x 0
Set window scroll margins ESC x tpu-set-scroll-margins (emacs will
prompt you for values)
Buffers…
Load a file into a new buffer CTRL-x CTRL-f
Toggle between buffers CTRL-x b (emacs will prompt you for a
buffer name)
You can also use this toggle function to
create a new buffer by entering a new buffer name.
List of current buffers CTRL-x CTRL-b
You can then select a buffer from this
list by typing the letter o next to the desired buffername
Kill (delete) current buffer CTRL-x k
Save
buffers CTRL-x s
= (emacs will prompt you buffer by buffer)
General
Functions
Abort current command CTRL-g (the same key is
used to exit the minibuffer)
Access the shell ESC x shell
Output from shell commands are stored in
an emacs buffer.
You can cut information from the shell
output, switch buffers, and paste it into your other buffer.
To return to your emacs editing session,
use the exit command at the shell prompt, then issue the CTRL-x b function to
switch back to your original buffer.
Change Mode ESC
x modename
example:
ESC x fundamental-mode to return to fundamental mode
Change
tab characters to spaces Select the
desired region of text (see editing functions above)
ESC
x untabify
Note:
Do NOT remove tab characters from makefiles!
Insert binary character CTRL-q character
example:
CTRL-q CTRL-l to insert page break
Refresh
screen CTRL-l (repositions the window so that the cursor
is in the center of the window)
Repeat the next function x times ESC n
example:
ESC 15 ESC u will uppercase the
next 15 words
Undo last edit CTRL-x u
emacs stores previous functions in
sequence. You can repeat this undo function multiple times to undo previous
edits in sequence.
File
Management
Saving your files…
Exit
and write buffer to current file CTRL-x
CTRL-c
Write
buffer to the current file CTRL-x
CTRL-s
Write
buffer to a different file CTRL-x
CTRL-w (note the change in the status line)
Include
a file into the current buffer CTRL-x
i
Auto-saved journal files…
EMACS will do an auto-save of your buffer
every 300 keystrokes. An auto-saved file will appear in your directory as
#filename# during your editing session; this file will remain in your directory
if your editing session terminates abruptly, or if you choose not to save your
edits. You can recover these journaled edits.
Upon re-editing the file, emacs will warn you that there is an
auto-saved version which is newer than the version you are editing. You can
choose to recover to this auto-saved version before performing any edits. You
can also recover to an auto-saved file at any time during the editing session:
Recover
from auto-saved file ESC x
recover-file
Revert to a saved
file…
If you want to abandon all edits during
the current editing session, you can revert to the last version of the file
saved on disk.
Revert
to last saved version ESC x
revert-buffer
Backup versions…
If necessary, you can also recover by
using an EMACS backup file. EMACS will create a backup copy of your file each
time you edit the file. Once it's created, the backup file is not changed until
the next time you invoke the editor, at which time the backup file is
overwritten with the newer saved version. The name of the backup file is the
same as the file name with a tilde added to it. The easiest way to use these backup files is to edit them, then
save them to the original file name.
Multiple file
versions…
EMACS is capable saving numbered backup
versions of files. You can accomplish by adding the following line to your
emacs configuration file (see below section on Customizing emacs) :
(setq
version-control t)
emacs will also let you specify how many
versions to keep (emacs will ask you if it is ok to remove older versions) (x
represents the desired digit):
(setq
kept-new-version x)
Customizing
emacs
Bookmarks…
You can set bookmarks in your file and they
will be retained after you save it (unlike marks in TPU, which are buffer
attributes and are lost when you exit the editor). This may be useful for
placing tags at the beginning of certain sections of code in a program. Emacs
will automatically save your bookmarks when you exit. Bookmarks are stored in a
separate file called .emacs-bmk; this file stores bookmarks for multiple files.
EMACS reloads the bookmarks every time you invoke EMACS.
Create a bookmark here CTRL-x r m (emacs will prompt you to
enter a new bookmark name)
Jump to a bookmark CTRL-x r b (emacs will prompt you to enter an existing
bookmark)
Delete
a bookmark ESC x
bookmark-delete (then enter the bookmark name)
List your bookmarks CTRL-x r l (this opens a buffer containing a list of your
bookmarks)
You can then jump to a bookmark from this
list by typing the letter o next to the desired bookmark name.
Macros…
Begin recording macro keys CTRL-x
(
Finish recording macro keys CTRL-x )
Execute the current macro CTRL-x e
Execute the current macro n times ESC 5 CTRL-x e
Save the current macro ESC x
name-last-kbd-macro
Execute a stored macro ESC x <macroname>
Save the stored macro to a key ESC x global-set-key (enter the desired
key, then the macro)
To save the currently defined macro to a
new or existing macro file (emacs supports the use of multiple macro files):
Open
the macro file CTRL-x
CTRL-f (enter file name)
Go
to the end of the file ESC >
Save
the macro ESC x
insert-kbd-macro (enter the macro name)
Save
this file CTRL-x
CTRL-s
Close
(kill) this buffer CTRL-x k
To load an existing macro file:
Load
a macro file ESC x
load-file (enter file name)
HINT: You can save your macros to your
.emacs configuration file (see below); this ensures your macros will be loaded
every time you invoke emacs.
Configuration Files…
You can create an .emacs configuration
file in your home directory to accomplish customizations such as mapping
function keys, entering emacs environment commands, and setting emacs
variables.
To invoke EMACS without loading the .emacs
configuration file, use the -q option.
Other Features
Revision Control…
Emacs can facilitate program revision
control by using existing Unix utilities (SSCS, RCS, and CVS). Users can “lock”
files during a revision cycle. If another user visits a file that is locked,
the user will receive a warning
message. When revisions are complete, the lock can be removed. Emacs also
enables you to keep a revision history on any file so you can track your
modifications.
Format Modes…
emacs supports various editing modes; the
commands listed in this document work in the fundamental mode, which is the
default mode. Using some of these modes can assist in software development
since EMACS supports formatting modes for common programming languages
including C, C++, and Perl. EMACS will help you format your program, including
indentation and commenting, and also warns you when parentheses are unbalanced.
Program Development…
emacs can be used as an integrated
development tool. You can execute a compiler in an EMACS window, manipulate the
error output of the compiler and use it to move to the point of error in the
source code, and execute programs in a shell window. In addition, EMACS uses
the ETAGS facility (similar to the TAGS function in vi). emacs can utilize a
database created by ETAGS; the database would contain source code filenames and
program function names to facilitate writing code.
Emacs
Information Resources
OnLine Help CTRL-h (or F10
in WinQVT)
Invoke Tutorial CTRL-h t
What command does this key execute? CTRL-h k (then press the desired key)
What key is bound to this command? CTRL-h w (then enter the desired
command)
Obtain help on a specific command
(function) CTRL-h f (then enter
the desired command)
Apropos commands CTRL-h a (then enter the
desired string)
GNU Emacs (O’Reilly & Associates)
Unix in a Nutshell
(O’Reilly & Associates)
Unix Unleashed (SAMS
Publishing)
http://www.gnu.org/manual/emacs-20.3/emacs.html