An Introduction to Emacs
– This introduction borrows heavily from Andy Harris's Emacs Guide, Prima Tech 2000.
Table of Contents
Why choose emacs
- Emacs is good for powerful text editing
- Know which editor is good for the job. Emacs might not always be the best choice.
- Emacs can radically change the way you do certain jobs. You do not yet realize how, because you have most likely adapted to certain inefficiencies in your current workflow.
- Emacs requires time, but, if you are willing, you will find that it profoundly improves the way you do some of your work.
Emacs Conventions
- Emacs assumes you are smart. You will have to learn things.
-
Is built to run at 100% within terminal. Do not assume any GUI for
emacs when you learn it.
- Example of -nw vs. GUI.
- Keyboard commands. Most have reasonable mnemonics. The benefit is that your hands will never have to leave the keyboard to perform extremely complex commands.
Control combinations
- A combination of the CTRL key and some other key.
- Remember C-g
-
Multi-key sequences
- C-x C-f example
- All file mgmt commands start with C-x
Meta combinations
- Control combos are not enough to encompass all of emacs' commands.
-
Meta on various systems: I know how mac works, figure out your OS
meta key.
- ESC works as Meta on any system. You don't hold it down though.
-
Meta as amplifier of CTRL.
- Example, C-f, M-f.
- M-x and command names.
Help and Documentation
- Before going online, check emacs for help. It is always better.
-
If you don't know how to ask for help, use C-h ? or C-h C-h
-
C-h cDefines a key combination (egC-x C-f) -
C-h iOpens the info pages (Fantastic source for Emacs and Elisp documentation) -
C-h tOpens the tutorial
-
Apropos Help (C-h a)
Similar to searching an index. Provides info on any matching command.
Refcards
All emacs installations come with refcards for your convenience. These are pdfs with a list of common commands for certain modes in emacs. In my Mac installation of emacs, I can find these refcards in
/Applications/Emacs.app/Contents/Resources/etc/refcards
They are likely in a similar location in your installation.
Editing
Movement Commands
-
C-fC-bC-pC-nMove Forward, Backward, Previous-line, Next-line -
M-fM-bMove Forward by word, Backward by word -
C-vM-vScroll Vertically down, Scroll Vertically up -
C-aC-eGo to beginning of line, end of line. -
M-<M->Go to beginning of buffer, end of buffer. -
C-lCenter the cursor
Deleting text
-
C-dDelete character to the front of point. -
M-dDelete word to the front of point. -
<DEL>Delete character to the back of point (normal). -
M-<DEL>Delete word to the back of point.
Killing and Yanking
Copy/paste doesn't work the exact same way in Emacs as it does in most editors. At first this might be confusing, but later you will begin to see the advantages of doing it this way.
-
C-kKill the line after cursor. -
C-yYank the last thing that was killed. -
M-yAfter usingC-y, yanks the last thing that was killed before that, and so on through the kill ring. Cycles through each of the previous things that were killed one at a time.
Marking regions
-
C-SPACESet/unset the mark. After the firstC-<SPACE>, move the cursor to where you want to highlight the appropriate region, then use one of the kill commands below on this region. -
C-wM-wKill the current region, "copy" the current region into the kill ring.
Undoing
-
C-_(That's Control + Underscore, so the keys are Ctrl, Shift, and Minus "-") Undo's the last action
Files, Buffers, and Windows
Files
Actual file on disk. This is only written to upon request.
-
C-x C-fFind file. Will look for a file if it exists, otherwise will create the file for you. -
C-x C-sSave current buffer to file. -
C-x sPrompt to save each buffer. -
C-x C-wWrite the current buffer to specified file (same as save-as).
Buffer:
A copy of the file being edited in emacs. The actual file does not change until you ask emacs to save the buffer. You can save a buffer to a different file with
-
C-x bSwitch buffer to … (default is the last buffer you had open). -
C-x kKill the current buffer. -
C-x C-bShow list of all current buffers. You can navigate this list to open, kill, save, etc.. on any or some buffers. -
M-x kill-some-buffersPrompts to kill each buffer currently open.
Window
Where buffers are shown. "Current window" refers to the one your cursor is currently in.
- Working with multiple windows
-
The difference between windows and frames
-
C-x 0Delete the current window. -
C-x 1Make the current window the only visible one (full-screen it). -
C-x 2Split the current window into two horizontally. -
C-x 3Split the current window into two vertically. -
C-x oMove cursor to the next window (order is up to down, left to right). -
C-M-vscroll-other-windowScroll the "other" window, really useful when you have only two windows.
-
Frame
Holds windows, only useful with Gris. Not much to be said. There is no concept of a frame when you use Emacs in terminal.
Working with Line Numbers
-
M-x goto-lineM-g gGo to the specified line number (it will prompt you for the line you want).
Major Modes & Minor Modes
-
C-h mDisplay help on current modes -
C-cThe usual key combination prefix before minor-mode specific commands. For instanceC-c C-cin C will comment a region.
Advanced Editing
Search and Replace
-
C-sSearch for string forwards through current buffer. Each repeated execution ofC-swill repeat that search for the same string until you press<RET>orC-g. -
C-rSame asC-sbut goes backwards through your buffer instead. -
M-x replace-string -
M-x query-replace-string -
M-x replace-regexp -
M-x query-replace-regexp
Keyboard Macros
-
C-x (Start recording macro -
C-x )Stop recording macro -
C-x eM-x call-last-kbd-macroExecute the last recorded macro.
Remember
C-g! It is useful if you want to cancel recording a macro.
- Example of macro: Indent every other line by two spaces.
Spell Checking
- Installing ispell
-
Using ispell (M-x ispell-buffer)
flyspell-mode
Emacs and Unix
Shell
M-x shell
Managing Files and Dirs with Dired
Directory Editor
C-x d, or C-x C-f on a directory
Most editing commands supported, such as C-s
Specific dired commands are a single key.
- Rename file: R
- Copy file: C
- Set permission on file: M
- Mark files for deletion: d
- Delete marked files: x
- Unmark files: u
Remember C-h m. Let's try this now and see what else we can do.
File backups
Emacs automatically creates timely backups of your files. You will see these in the directory of the file being edited.
Backup Files: Previous copy of the file. Tilda (~)
Auto-Save files: Current copy of the file prior to saving explicitly. Gets updated frequently and on system errors. Hashes surround files. #filename#
Date: 2012-11-14 17:49:50 EST
HTML generated by org-mode 7.3 in emacs 24