Using the Emacs Editor

Using the Emacs Editor

Table of Contents



1. Introduction
  1.1 What can it do?
  1.2 History

2. Basics
  2.1 How to start
  2.2 Emacs commands
  2.3 Moving the point
  2.4 Quitting Emacs
  2.5 Cut and Pasting
  2.6 Files and buffers
  2.7 Searching and replacing
  2.8 Modes of buffers
  2.9 Miscellaneous

3. Advanced Commands
  3.1 Viewing multiple buffers
  3.2 Commands and key bindings
  3.3 Compiling from within Emacs

4. Other Resources


1. Introduction

1.1 What can it do?
The Emacs text editor is a popular and powerful program that is available on many platforms ( e.g. , UNIX, DOS, Windows 9x and NT , and OS/2). Some of its features include:

  • simpler to use than vi
  • editing and viewing multiple files at one time
  • spell checking
  • compiling programs from within Emacs
  • automatic indention of programs
  • a powerful macro language that may be used to extend the functions of Emacs

1.2 History

Emacs was created by Richard Stallman in 1975. GNU Emacs is the most popular version of Emacs and is directly derived from Stallman's original version. The GNU (Gnu is Not UNIX) project consists of programmers that volunteer their time to develop free software and is associated with Stallman's Free Software Foundation . There are other versions of Emacs that are not free.

2. Basics

2.1 How to start

To run Emacs, simply type at the UNIX prompt:

emacs file_to_edit

Emacs will be run and start editing the file specified. The screen will look something like:


#include <fcntl.h>
#include <assert.h>

void tty_info( const char *name )
{
int i;
for( i = 0; i < 3; i++ )
fprintf(stderr,"%s -> fd %d: isatty = %d, pgrp = %d
", name, i,
isatty(i), tcgetpgrp(i));
}

int main( void )
{
pid_t pid1, pid2;
int pipe1[2];
int term_fd;

term_fd = open("/dev/tty", O_RDWR, 0666);
assert(term_fd >= 0);
tty_info("main1");
pipe(pipe1);
-----Emacs: b.c (C)--9%---------------------------------------


The mode line is the one that is next to the last line from the bottom and will be highlighted. This line divides the editing portion of the screen from the command line (or echo area in Emacs jargon) at the very bottom. When a file is loaded into Emacs, it is loaded into a buffer. Buffers are edited, not files. The mode line shows the name of the buffer. To update the actual file, the buffer must be saved. Emacs will prompt the user to save any modified buffers when it exits.

Emacs maintains a backup file when a file is saved. The backup file is a copy of the last version of the file. Its name is the original file name with a tilde (~) appended to the end.

2.2 Emacs commands

The user uses special keys to send commands to Emacs. The CONTROL and META keys are used to distinguish keys used as commands from keys used to enter text into a buffer. The CONTROL key should be familiar. It is used like a shift key. In Emacs notation, C-x means to hold down the CONTROL key while hitting the x key. Many terminals do not have a key labeled META. Such terminals, use the ESC key for the META key. It is used differently than the CONTROL key. The META key is hit and released before the next key is hit. For example, in Emacs notation, M-x means to hit the META key (and release) and then hit the x key.

2.3 Moving the point

The point marks the location of the cursor on the screen. Often Emacs is configured to use the cursor keys to move the point. The following key commands move the point:

C-p
move up one line (useful if cursor keys do not work) (p for previous)
C-n
move down one line (n for next)
C-f
move right one character (f for forward)
C-b
move back one character (b for back)
C-d
delete the character at the point
<DEL>
delete the character before the point ( i.e. , backspace)
C-a
move to beginning of line
C-e
move to end of line
C-v
move down a screen
M-v
move up a screen
M-<
move to beginning of the buffer
M->
move to end of buffer

Sometimes the backspace key is mapped to return C-h (which is the help key for Emacs see below ). Most terminal/telnet programs allow this to be changed so that the backspace key returns <DEL>.

2.4 Quitting Emacs

Emacs can be exited in two ways. The first way is to type C-x C-c, this command terminates the Emacs process. (Note that this command is activated by two keystrokes and that the CONTROL key is held down for both!) The user will be asked to save any modified buffers.

The second way does not terminate Emacs. The C-z command suspends the Emacs process. A suspended process is not terminated, but a shell prompt appears and the user can enter commands. The user may unsuspend the process and continue editing by typing:

fg

at the shell prompt. Emacs does not ask to save any buffers when suspending.

Which way should be used? It depends. If the user is about to logout, Emacs should be terminated. If the user only wishes to execute a few UNIX commands and then return to editing, suspending Emacs is more convenient.

2.5 Cut and Pasting

To move and copy text in Emacs, the user must first mark the text to copy or move. A region of text is marked by first setting the mark at one end of the region and then moving the point to the other end. The C-@ or C-SPC (SPC is the spacebar) commands set the mark. If the region is to be moved, hit C-w to cut the region. The region will be removed from the buffer and saved. If the region is to be copied, use M-w. This command copies the region, but does not remove it. Then move the point to the position where the text show go and hit C-y. This command yanks the text back.

2.6 Files and buffers

Emacs supports multiple buffers. The user may load files into the buffers, switch between them and copy text from one buffer to another.

C-x C-f
read a file into a buffer
C-x C-s
save current buffer back to file
C-x i
insert contents of file into current buffer
C-x C-w
write buffer to specified file
C-x b
select another buffer
C-x C-b
list all buffers

Text from one buffer can be copied or moved to another buffer. Simply cut (C-w) or copy (M-w) from one buffer, move to the other and paste (C-y).

2.7 Searching and replacing

Emacs provides several ways to search for text in a file. The two most useful commands are:

C-s
search forward
C-r
search backward

When these commands are used, a prompt will appear at the bottom. Then the user types in the text to search for. Emacs does not wait for the entire text to be entered, it starts searching immediately. For example, if one searches for the word write , as soon as the w is hit, Emacs moves to the first occurrence of the letter w. When the r is hit, Emacs moves to the first occurrence of wr and so on. To stop a search, hit ESC. To cancel, type C-g.

The M-% command is most common replace command. It will prompt the user for the string to replace and what to replace it with. It then will scan through the buffer and prompt the user to confirm each replacement. Possible responses are:

SPC
(space bar) replace string, go on to next match
DEL
(delete key) skip without replacing, go on to next match
!
replace all remaining matches
ESC
stop replacing

2.8 Modes of buffers

Emacs supports multiple modes for buffers. The mode of a buffer determines how some commands work in that buffer. For example, files that end in .c or .h are automatically put in C mode. In this mode, Emacs perform several extra functions:

  • When a ), ] or } is typed, Emacs will show the user the matching (, [ or { by moving the point to the matching symbol for a fraction of a second.
  • If the TAB key is hit, Emacs will indent the line to the correct depth. (However, this will only work correctly if the lines above are properly indented.) The user can use this feature to catch some syntax errors. If the line is not indented as the user expects, it may be because of a syntax error in the line (or a line above).

2.9 Miscellaneous

Here are some useful commands:

C-k
erases to end of line. If line is blank, the line itself is erased.
C-g
aborts a partially typed command.
C-x u
undo last change to buffer.
C-l
redraw screen
C-h
starts Emacs on-line help
M-$
check spelling of word at point

3. Advanced Commands

3.1 Viewing multiple buffers

It can be very useful in some circumstances to look at two different buffers when editing. The following commands are used:

C-x 2
split the screen to show two buffers
C-x 1
go back to one buffer on screen
C-x o
switch cursor to other buffer

3.2 Commands and key bindings

Emacs supports many different commands. Each command has a name that uniquely identifies it. Some of the most common commands are also bound to keys. For example, the C-x 2 key sequence is bound to the split-window-vertically Emacs command. The association of a key sequence with a command is called a key binding . Emacs allows the user to redefine (and create new) key bindings; however, this is beyond the scope of this document.

For the commands with no key binding, the command must be invoked by name using the execute-command command that is bound to M-x. (Of course, M-x can be used to execute any command.)

3.3 Compiling from within Emacs

Emacs allows programmers to compile code without leaving Emacs. In addition, Emacs can often parse any error messages generated and automatically move to the offending line in the source code. This can be very convenient!

To compile your code, use the compile command (i.e., type M-x compile). The following prompt will appear on the bottom line:

Compile command: make -k

This says that Emacs will use the make -k UNIX command to compile your code. This will only work if the programmer has constructed a makefile that is used by the make command. If a makefile has not be constructed, the programmer must replace make -k with the appropriate command. It is possible to edit text in prompts on the bottom line (in fact, the prompt is called the mini-buffer ). For example, to compile a C program named a.c , erase the make -k , replace in with cc a.c and hit Return. If there are modified buffers that have not yet been saved back to their files, Emacs will ask the programmer if the changes should be saved. Remember the compiler will compile what is saved to the file, not what is in Emac's buffers!

The compile command will split the screen in two buffers. The new buffer will show the result of compiling the code. If there are errors, they will be displayed in the buffer. To have Emacs look up the line that each error occurs on, use the C-x ` command. (The last character is a backquote.) The first time this command is used, it will show where the first error is. The next time it will show where the second error is, etc. For this to work, the compiler must print out its error messages in a format that Emacs understands. Almost all C and C++ compilers output error messages in the correct format.

4. Other Resources

Here is a list of other Emacs resources on the web:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值