Being Productive With Emacs Part 1
Phil Sung sipbiap[email protected] http://stuff.mit.edu/iap/emacs Special thanks to Piaw Na and Arthur Gleckler
“Emacs is the extensible, customizable, selfdocumenting realtime display editor.”
The many faces of Emacs
Emacs edits source code
The many faces of Emacs
Emacs is a hex editor
Mx hexlfindfile The many faces of Emacs
Emacs does diffs
Mx ediffbuffers The many faces of Emacs
Emacs is a file manager
Mx dired The many faces of Emacs
Emacs is a shell
Mx shell The many faces of Emacs
Emacs is a mail/news client
Mx gnus The many faces of Emacs
Emacs plays tetris
Mx tetris Why Emacs?
Provides an integrated environment
● Same editing commands available everywhere ● Large set of tools available at all times ● Move text between tasks easily
Why Emacs?
Easy to extend
● Elisp for customizing or adding new features ● Extension code has the full power of Emacs ● Dynamic environment: no restarting or recompiling Portable
Today's goal: get the flavor of Emacs
● Getting started with Emacs
● Editing tips Examples based on GNU Emacs 22 ● Demos of useful features ● Common Emacs concepts Later...
● Advanced customization ● Programming and extending Emacs with Elisp
Prerequisites (sort of)
Emacs basic concepts Take the tutorial to brush up: ● Files, buffers, windows, frames Ch t Keyboard commands
● Key commands, prefix keys, Mx, the minibuffer ● "Cx" means Ctrl+x "Mx" means Meta+x or Alt+x Basic tasks
● Opening and saving files, exiting Emacs It's all about text manipulation
Text in files
● grocery lists, HTML, code, ... Text outside of files
● shell, debugger, ... Text as a metaphor
● dired, gnus, ...
Text as a metaphor: dired
M-x wdired-change-to-wdired-mode after opening any directory
After editing names in this buffer, Cx Cs renames the modified files
Moving around in buffers
By character or line
Cp
Cb Cf
Cn
Moving around in buffers
Beginning, end of line By screen
● Ca, Ce ● Cv, Mv By word Beginning, end of
● Mf, Mb buffer
● By sentence M<, M> ● Ma, Me Go to line # ● Mg g
Moving around in buffers
Move multiple lines forward, backward
● Example: Cu 10 Cp (back 10 lines) ● Cu prefix generalizes to other commands Search for text
● Cs, Cr Exchange point (cursor) and mark
● Cx Cx
Killing ("cutting") text
Kill line
● Ck Kill many lines
● Cu 10 Ck (10 lines) ● Cu Ck (4 lines) ● Cu Cu Ck (16 lines)
Killing ("cutting") text
Kill region Kill sentence
● Cw ● Mk Save without killing Kill ("zap") to next
● Mw occurrence of character
● Mz CHAR
Yanking ("pasting") text
Yank
● Cy Yank earlier killed text
● My (once or more after Cy) The kill ring
● Almost all commands which delete text save it for possible later retrieval
The mark Remembers a previous cursor position
● Cx Cx to swap point (cursor) and mark
When you... the mark is set to... ● Cspc ● where you are ● M< or M> ● where you were ● Search for text ● where you started ● Yank text ● start of inserted text ● Insert a buffer ● start of inserted text
The mark
The mark ring
● Move to a previous mark: Cu CSPC Mark and point are also used to delineate 'the region'
● Many commands operate on the text in the region ● Set region by setting mark, then moving point
Undo
Undo previous actions
● C/ or C_ or Cx u Undo within current region
● Cu C/
The undo model, illustrated
A B C D
The undo model, illustrated
A B C D
These states are accessible with 'undo'
The undo model, illustrated
Undo some of your actions...
This is how most editors other than emacs work:
A B C D
These states are These states are accessible with 'undo' accessible with 'redo'
The undo model, illustrated
Now do something else...
This is how most editors other than emacs work:
A B C D
These states are no These states are longer accessible! accessible with 'undo' C'
The undo model, illustrated
How emacs handles this situation
A B C D
C'
The list of states is 'folded' so that all previous actions, inc luding undos, are undoable Incremental search
Search for text (like Firefox's "find as you type")
● Cs text
Cs again to find next occurrence RET to stop at found occurrence Cg to cancel and go back to start of search ● Cr for reverse search ● Many options available inside search; Ch k Cs to learn more
Search history
Search for previously searched string
● Cs Cs Browse and edit previous queries
● Cs then Mp, Mn
Incremental search
Search for regular expressions
● CMs regexp ● Regexp describes the form of what to look for ● Syntax may be slightly different from other REs you may have used
Emacs REs are a superset of Perl REs ● Mx rebuilder can help you test complex regexps
Searching and replacing
Search and replace, asking for confirmation
● M% or Mx queryreplace Display all lines matching RE
● Mx occur
RE search and replacement
Mx replaceregexp Replacement text can depend on found text! Replacement text gets these substitutions:
● \& (the matched string) ● \1, \2, etc. (references to parts of matched string) ● \# (number of matched lines so far) ● \? (prompt user for what to enter)
● \,(lispexpressi on ...) RE replacement example
Bill Gates Steve Jobs Eric Schmidt Mx replaceregexp Larry Ellison \(\w+\) \(\w+\) with GATES, Bill \,(upcase \2), \1 JOBS, Steve SCHMIDT, Eric ELLISON, Larry
More tips at http://steve-yegge.blogspot.c om/2006/06/shiny-and-new-emacs-22.html Integration with useful tools
Shell
● Mx shell Compile (invoke make)
● Mx compile Debug
● Mx gdb
Integration with useful tools
Grep
● Mx grep, Mx rgrep Man page reader
● Mx woman Invoke shell commands
● Mx shellcommand, Mx shellcommandonregion
...and then some
Calculator
● Mx calc Calendar
● Mx calendar Moon calendar
● Mx phasesofmoon
More helpful features
TRAMP: open remote files over SSH
● Cx Cf /user@host:~/remote/file VC: automatically deal with CVS, SVN, etc.
● Mx vcnextaction to commit modified file ● Mx vcdiff to view changes to current file etags: name search/completion for source code
Emacs server
Use a single Emacs session for all editing Do this once: Mx serverstart
● or put (serverstart) in your .emacs file To edit a file:
● prompt% emacsclient file ● File opens in existing Emacs frame ● Cx # when done editing
Macros
Remembers a fixed sequence of keys for later repetition Start recording macro: Cx ( Stop recording macro: Cx ) Replay macro: Cx e
Macro example Define macro: Md Cd Mu , [SPC] Cy Cn Ca "Remove first word and space, Bill Gates uppercase next word, insert comma and space afterward, reinsert first Steve Jobs word, move to beginning of next line" Eric Schmidt Larry Ellison Run macro repeatedly: Cx e e ... GATES, Bill JOBS, Steve SCHMIDT, Eric
ELLISON, Larry Narrowing
Restricts view/editing in a buffer to a certain region
● Cx n n or Mx narrowtoregion to narrow to region ● Cx n w or Mx widen to restore ('widen')
Registers
Store current window configuration
● Cx r w REGISTER REGISTER may be any letter or number Restore window configuration
● Cx r j REGISTER Registers can also store positions, text, numbers, file names...
Prefix arguments
Sometimes used to indicate repetition
● Cu 10 Cf (forward 10 characters) ● Cu Co (make 4 new lines) Sometimes modify following command
● C/ (undo) vs. Cu C/ (undo within region) ● Mx shell vs. Cu Mx shell A command's documentation (Ch f or Ch k) describes the effect of the prefix argument, if any
Major modes
Alters behavior, key bindings, and text display Switch mode in existing buffer:
● Mx javamode ● Mx pythonmode ● Mx fundamentalmode Or, use another command to create buffer:
● Mx shell ● Mx dired
Language major mode features
Languagespecific indentation, syntax coloring Languagespecific features:
● Lisp: commands for manipulating sexpressions ● Python: commands for (un)indenting blocks ● HTML: insert/close tags; preview in web browser ● Modes can define or redefine keys
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 Mx autofillmode Mx flyspellmode Mx followmode
Global minor modes
Offer completions for buffers, commands, etc.
● Mx icompletemode Show all buffer names on Cx b:
● Mx iswitchbmode
Minibuffer input
Common features whenever Emacs prompts you to enter something
● Most buffer editing, movement commands work ● Browse previous inputs with Mn, Mp ● Tabcompletion is often available Mx evalexpression, Mx find file, Mx switchtobuffer, ...
Getting help with Emacs
Help with key Help with mode
● Ch k ● Ch m Help with function Show key bindings
● Ch f ● Ch b Help about help
● Ch Ch
Getting help with Emacs
Apropos (search for command)
● Ch a Help with prefix key
● Ch (after prefix key) Manuals
● Mx info, then select emacs or efaq
“In the event of an emergency”
Cancel command
● Cg Undo!
● C/ or C_ What did I just do?
● Mx viewlossage
Common problems
Delete not deleting?
● Mx normaleraseisbackspacemode Keys with M not working?
● Use ESC instead ● ESC x instead of Mx ● ESC Ct instead of CMt
Migrating to Emacs
From Windows applications
● Mx cuamode: recovers Cz, Cx, Cc, Cv for their usual purposes From vi/vim
● Mx vipermode
Resources
Emacs on Athena
● http://web.mit.edu/olh/Emacs/ Emacs reference card
● http://web.mit.edu/olh/Emacs/Refcard.pdf
Invoking or installing Emacs
emacs21 on Athena: athena% emacs emacs22 on Ubuntu/Debian: aptget install emacssnapshotgtk emacs22 on Gentoo: emerge emacscvs emacs on Windows:
● http://ourcomments.org/Emacs/ EmacsW32Util.html
Bonus: Being Unproductive With Emacs
● Mx tetris, Mx hanoi, Mx doctor, ... Next time: Emacs lisp
● Customizing how Emacs works ● Writing new functions, commands, and modes ● Manipulating text programmatically ● Altering behavior of existing commands