Basic Emacs


Quick Start

Since Emacs was originally made for non - GUI environments all of the commands in Emacs can be executed from the keyboard. There are two methods to executing an Emacs command from the keyboard. One is simply to type the name of the command. The other is to press a key stroke. Both methods are explained below. Emacs has literally hundreds of commands, but you only need a few ( even less for the GUI version ) to get a tremendous amount of work done.

Note:  Starting Emacs
Most current versions of emacs have a GUI that will be invoked by typing "emacs " at a command prompt.  If you want to run emacs in the command shell you will need to invoke emacs by typing "emacs -nw".

 

Notation used for listing key strokes:

C- means press your Ctrl key
M- means press your Meta, Esc, or Alt key
C-x

means press your Ctrl key, and hold it down while pressing the specified character,
in this case the "x"

C- x means press your Ctrl key, release it and then press the specified character
in this case the "x"

 

Method 1:  Executing a command by typing the name of the command
To execute a command by typing the name of the command you must first press the "Meta" key ( usually the Esc or Alt key ), then press the "x" key, and then type the name of the command.

Example:
Emacs has a command called "undo" that undoes what you just did.   You can execute it by pressing the meta key ( Esc or Alt ), then pressing the "x" key, and then typing "undo". Executing a command in this style would be denoted below as:

M- x undo

Pressing the tab key ( after typing a few characters ) will complete the name of the command for you or give you a list of possible completeions.



Method 2:  Executing a command by pressing a key stroke
To execute a command by typing a key stroke ( aka "keybinding" or "short cut" ) hold down the Ctrl key while pressing another key, then release those keys and press another specified key.

Example:
Emacs has a command called "undo" that undoes what you just did.   You can execute it by holding down the Ctrl key while pressing "x", releasing those keys and then pressing "u". In other word words:

C-x u


Core Commands


Help:

Help M- x help
Tutorial M- x help <RETURN> t
Emacs Manual
( the best )
M- tmm-menubar <RETURN> h r
or
M- x help i ( then scroll down to "Emacs" ) <RETURN>


Hardcore:

Read the menubar choices when in a shell M- x tmm-menubar
Undo What You Just Did C-x u
Abort a command C-g
Save Your Work C-x C-s
Quit Emacs C-x C-c

 

Cursor Navigation:

Go To The End Of A Line C-e
Go To The Start Of A Line C-a
Go To The End Of A File M- >
Go To The Top Of A File M-<
Go One Screen Down C-v
Go One Screen Up M-v


Files & Buffers:

Go To A File C-x C-f <file name> ( pressing the tab key invokes auto completion)
Insert A File C-x i
Kill the current buffer C-x k
Invoke the buffer menu

M- x buffer-menu

To execute a command on a buffer in the buffer menu:
- move the cursor over the buffer name
- press the letter of the command (repeat if desired )
- press "x" to execute all of those marked commands

"d" - delete a buffer
"s" - save a buffer to a file
"u" - remove a previous marking/command

 

Invoke "Dired"
( the file explorer )

C-x d

To execute a command on a file move your cursor over the file:
- delete: press "d" ( over one or more files ), then x to run the delete(s)
- remove command/marking : press "u"
- copy: press "C" type path and file name
- move: press "M" type path and file name

 


Searching And Replacing:

Search forward
C-s <RETURN> C-W <string>
or
M- x isearch-forward  <RETURN> C-W <string>
Search backward
C-r <RETURN> C-W <string>
or
M- x isearch-backward  <RETURN> C-W <string>
Repeat forward search M- x isearch-repeat-foreward
Repeat backward search M- x isearch-repeat-backward
Search & Replace M- x replace-string <search string> <RETURN> < replace string> <RETURN>

 

Managing Blocks Of Text:

Delete To The End Of A Line C-k
Bring That Line Back C-y
Mark a block of text
(analogous to selecting rows of text in a GUI app)
1. C-<spacebar> at the top of your selection
2. Move cursor beneath the region you want to "select"

or

1. M- x set-mark-commad  at the top of your selection
2. Move cursor beneath the region you want to "select"
Mark a rectangle
(analogous to selecting columns of text or white space in a GUI app)
1. Put your cursor over the upper left corner of the rectangular region you you want to "select"

2. C- <spacebar>

3. Move your cursor one column to the right of the bottom right corner of the rectangular region you want to "select"
Delete A Block Of Text
( "Cut or Copy")

1. Mark a block of text
2. C-w

Get That Block Of Text Back
("Paste")
1. C-y
Delete Columns Of Text
( "rectangle editing")
( "Cut or Copy")
1. Mark a rectangle
2. C-x r k
Get Those Columns Of Text Back
("Paste")

1. Move the cursor to where you want the upper left corner of the rectangular region/columns of text to be placed

2. C-x r y

Create/Insert A Blank Columns Of Space
(called "opening a rectangle")

1. Mark a rectangular region

2. M- x open-rectangle

Fill a rectangular region with a string

1. Mark a rectangle.

2. M- x string-rectangle <RETURN> "yourString" <RETURN>

Execute a command only on selected code

( Called "narrowing".   "Narrowing" makes the unmarked code become invisible and inaccessible while you operate on the block of text you just makred)

1. Mark a block text
2. C-x n n
4. Do your commands on that region ( i.e. a search and replace)
5. C-x n w("widen, make the rest of the doc accessible, visible again)

 

Splitting The Screen Into Multiple Windows ( console mode ):

Split Screen Into 2 Horizontal Windows C-x 2
Split Screen Into 2 Vertical Windows C-x 3
Move cursor to the next window
C-x 0  (zero)  
Make Other Windows Go Away C-x 1

 

Compiling & Running Programs:

Compile The File You Are In 1. M- x compile ( hit return)
2. <compile command><file name>
Go To Next Error Msg & Move Cursor To Corresponding Code C-x `
Scroll To Next Error MSG M-n
Scroll To Previous Error MSG M-p
Go-To A Line M- x goto-line <line number>

Get A Shell ( command line ) Window:
( run the program or other commands )
make sure you type 'exit' at the command prompt in shell mode before

M- x shell

 


Main Emacs Page