Being Productive With Emacs
http://stuff.mit.edu/iap/2009/emacs/
Phil Sung
MIT, CSAIL; psung@alum.mit.edu
Emacs is the
extensible,
customizable,
self-documenting
real-time
display
editor.
The many faces of Emacs
Emacs is an editor for code
Emacs is a file manager (M-x dired)
Emacs is a terminal emulator (M-x shell)
Emacs shows diffs (M-x ediff)
Emacs is an IDE (M-x gdb)
Emacs is a PDF viewer
Emacs is a mail/news/RSS reader (M-x gnus)
Emacs plays Tetris (M-x tetris)
Why Emacs? (1)
- Provides an integrated environment
- Same editing commands available everywhere
- Large set of tools available at all times
- Move text between tasks easily
Why Emacs? (2)
- Easy to extend
- Macros for one-off tasks
- Elisp for customizing or adding new features
- Extension code has the full power of Emacs
- Dynamic environment: no restarting or recompiling
- Portable
Agenda
- Today
- Getting started with Emacs
- Editing commands
- Demos of useful features
- Common Emacs metaphors/concepts
- Basic customization
- Next week
- More advanced customizations
- Programming and extending Emacs with Elisp
A primer
- Installing and invoking Emacs
- Unix-like OSes: $ emacs
- Windows: look for "EmacsW32"
- Mac OS: look for "Carbon Emacs" or "Aquamacs"
- Emacs basic terminology
- Files, buffers, windows, frames
- Keyboard commands
- C-x means Ctrl+x
- M-q means Meta+q or Alt+q
- M-x long-command-names...
- Take the tutorial (C-h t) — really
It's all about text manipulation
- Text in files: grocery lists, HTML, code, etc.
- Text outside of files: shell, debugger, etc.
- Text as a metaphor: dired, gnus
Text as a metaphor: dired
Moving around in buffers (1)
Moving around in buffers (2)
Beginning, end of line | C-a | C-e |
By word | M-b | M-f |
By sentence | M-a | M-e |
By paragraph | M-{ | M-} |
By screen | M-v | C-v |
Beginning, end of buffer | M-< | M-> |
Go to specific line number | M-g g | |
Moving around in buffers (3)
- Search for text
- Exchange point (cursor) and mark
Killing (cutting) text
Delete character | C-d or DEL |
Kill line | C-k |
Kill word | M-d |
Kill sentence | M-k |
Kill region | C-w |
Save region without killing | M-w |
Kill ("zap") to next occurrence of character | M-z |
Yanking (pasting) text
Yank | C-y |
Yank earlier killed text | M-y |
The kill ring: almost all commands which delete text save it for possible
later retrieval
The mark
- Remembers a previous cursor position
- C-x C-x swaps point and mark
- Set it to the current position with C-SPC
- Many operations that move point large distances set the mark to where you
were.
- For example: M-<, M->, or searching for text
- The mark ring: move to a previous mark with C-u C-SPC
The mark
- Mark and point also delineate 'the region'
- Set region by setting mark (C-SPC), then moving point
- Or, highlight with mouse
- Many commands operate on text in region
Undo
- Undo previous actions
- Undo within current region
- Just highlight region first
Macros
- Remembers a fixed sequence of keys for later repetition
- Start recording macro: F3
- Stop recording macro: F4
- Replay macro: F4
Macros
- Example task
- Convert "Bill Gates" to "GATES, Bill"
- Hint: M-u converts a word to uppercase
Macros
M-d | Kill first word |
C-d | Delete subsequent character |
M-u | Uppercase next word and move past it |
, | Insert a comma |
SPC | Insert a space |
C-y | Yank deleted word |
C-f | Move to beginning of next line |
Macros
- Replay macro once
- Replay macro 5 times
- Replay macro until "the end"
Incremental search
- Search for text (like "find as you type")
- Search: C-s TEXT
- Search for next occurrence: C-s
- Stop at found occurrence: RET
- Cancel and return to start of search: C-g
- C-r to search backwards
- Many options available inside search; C-h k C-s to learn more
Search history
- Search with previously searched string: C-s C-s
- Examine previous searches: C-s then M-p or M-n
Searching and replacing
- Search and replace, asking for confirmation
Regular expression search
- C-M-s REGEXP
- Regexp syntax may be slightly different from other REs you may have
used
- Display all lines matching regexp:
Regexp search and replacement
- Search and replace, asking for confirmation
- Replacement text can depend on found text!
- \& — the matched string
- \1, \2, etc. — references to parts of matched string
- \# — number of lines matched so far
- \? — prompt user and insert entered text
- \,(...) — arbitrary lisp expression
Regexp replacement example
- Again, replace "Bill Gates" with "GATES, Bill"
- Use M-x replace-regexp and replace "\(\w+\) \(\w+\)" with "\,(upcase \2), \1"
Integration with useful tools
- M-x grep
- M-x man
- M-x shell-command
Miscellaneous features
- TRAMP: open remote files over SSH
- C-x C-f /user@host:path/to/remote/file
- VC mode
- Show diff with last revision: C-x v =
- Make a new commit: C-x v v
Miscellaneous features
- Narrowing restricts view/editing in a buffer to a certain
region
- C-x n n or M-x narrow-to-region to narrow to the current region
- C-x n w or M-x widen to restore
- Registers
- C-x r w REGISTER to store current window configuration
- REGISTER may be any letter
- C-x r j REGISTER to restore
- Registers can also store positions, text, numbers, filenames, etc.
Major modes
- Alters behavior, key bindings, and text display
- Switch modes in an existing buffer:
- M-x java-mode, M-x python mode, M-x fundamental-mode, etc.
- Or, use another command to create buffer:
- M-x shell, M-x dired, etc.
Language major mode features
- Language-specific indentation, syntax coloring
- Language-specific features
- Lisp: commands for manipulating s-expressions
- Python: commands for (un)indenting blocks
- HTML: insert/close tags, preview in web browser
Minor modes
- Extra functionality you can turn on or off
- Any number of minor modes may be active at once
- Independent of major mode functionality
- M-x auto-fill-mode
- M-x flyspell-mode
- M-x follow-mode
Getting help with Emacs
- "What does this key do?" — C-h k
- "What does this function do?" — C-h f
- "What does this mode do?" — C-h m
- "What keys are bound and what do they do?" — C-h b
- Search for a command ("apropos") — C-h a
Getting help with Emacs
- "Help! I'm lost!" — C-h C-h
"In the event of an emergency"
- Cancel command — C-g
- Undo — C-/
- "What did I just do?" — M-x view-lossage
Next steps
- Online tutorial — C-h t
- Online manual — C-h r
Resources
- Emacs on Athena
- Emacs reference card
Emacs is the
extensible,
customizable,
self-documenting
real-time
display
editor.
Review
- Emacs concepts/terminology
- Editing commands
- Applications (major modes)
- Automation (macros)
- Basic customization (minor modes)
Next week...
- More extensive customizations
- Augmenting or replacing Emacs behavior
- Manipulating text programmatically with Elisp