Вы находитесь на странице: 1из 15

EMACS

By Justin Higgins

What is a text editor?


If youve ever used Microsoft word you know
what a text editor is (albeit a bloated one).
When you write a program in this class you will
use a text editor to write the file. In this file you
will write a series of commands which upon
execution will be performed.
In our case: we will use the text editor to write
IDL programs. Naturally the file you create will
have a series of IDL commands (these ARE
different from UNIX commands)

Emacs vs VIM
There are primarily two text editors programmers
use: Emacs and VIM
Both have incredible functionality with VIM
focusing more on fewer keystrokes and Emacs
focusing on customization.
We will teach you Emacs, although it makes no
difference in the future if you become proficient
in one or the other.
Beware! You are choosing a side in the holiest of
holy geek wars.

Open Emacs
To open emacs in a terminal simply type
emacs.
To directly open a file with emacs from the
terminal type emacs file_name
If you do this on the ugastro server a new
window will open up with the GUI emacs.
If you have a Mac you can open Emacs in
terminal the same way, but you will not get
the same GUI (it will be built in the terminal).

Emacs basics
Most emacs commands use a Ctrl-x plus some
characters to perform a command:
Examples:
To open a file: Ctrl-x Ctrl-f (then type the file
path)
To save a file: Ctrl-x Ctrl-s
To close a file: Ctrl-x Ctrl-c

Editing
To edit a file simply type in the main window.
Search forward: Ctrl-s (type the search string)
type Ctrl-s repeatedly to step through searches
Search reverse: Ctrl-r
Beginning of line: Ctrl-a
End of line: Ctrl-e
Beginning of file: Esc-<
End of file: Esc->

Ooops! I made a poopsy!


To undo an action: Ctrl-_ (or Ctrl-x u)
To cancel a command you are typing: Ctrl-g
Example:
(I want to save a file but I accidentally typed:
Ctrl-x Ctrl-c which will close it if I hit enter!)
Ctrl-g
Type multiply times if not stopping. this will work
anywhere within emacs

Intermediate
Goal: Once you are comfortable you should
very seldom use your mouse/track pad or
have to type the arrow key repeatedly.
Go to a line: M-x goto-line
Query replace: Esc-% (type search
stringentertype replace string.enter, skip
a change with n and execute one with
spacebar)

Markers
You can set a marker where you would like
some text operations executed. Do this by
typing: Ctrl-space (you will see mark set on
the bottom)
Now wherever you move youre the cursor
next, will define the mark up region (the
region you want to edit).

Using markers (dont inhale)


Remove or kill a line: Ctrl-k
Remove a region of text:
Set marker
Move cursor to bottom of remove region
Ctrl-w
IMPORTANT: After you remove a line or region
that text is temporarily stored and can be
pasted by typing Ctrl-y (for yank)

Buffers
You can have multiple buffers within one window
of emacs. That is OS will only show that one
terminal window of emacs is open, but in that
window can be MANY files.
Say you open a file (file1) and then want to open
up another file (file2) whilst leaving file1 open.
Simply type Ctrl-x Ctrl-f and the path for file2 and
youve now opened up file2 in a new buffer.
To switch back to file1, type Ctrl-x b file1.
If you forget which files are open in buffers: Ctrl-x
Ctrl-b displays available buffers

Viewing multiple buffers


simultaneously
This is perhaps one of the most useful tricks you will use in
emacs.
You will often want to compare two or more of your
programs. Instead of opening up multiple emacs windows
you split one emacs window into many. In each of these
windows you can display a buffer.

To split the window horizontally: Ctrl-x 2


To split the window vertically: Ctrl-x 3
To close the window (YOU ARE IN): Ctrl-x 0
To close all the windows EXCEPT the one you are in: Ctrl-x 1
To switch between windows: Ctrl-x o

Advanced: Macros
Eventually you become so 1337, so Super
Saiyan that even the shortcuts arent fast
enough for you. Luckily you may define your
own shortcuts with macros.
Begin defining a macro: Ctrl-x (
Define one iteration of the operation
End macro definition: Ctrl-x )
Execute the macro: Ctrl-x e

Iterative operations
To execute a command iteratively: Ctrl-u #
command (where # is the number of
executions)
Example:
To indent a region 4 spaces:
Define region.
Ctrl-u 4 Ctrl-x <tab>

Last remarks
You will only master these by practice. If you
take the time to learn the commands the
programming bottleneck is simply the
execution of your code (and not your typing
speed).
Of course, it is easy to get distracted with
emacs shortcuts. Remember they are there
to simplify a taskthat is, if it is too much
work to use a shortcut DONT USE IT!