8/7/2019 EmacsTips
http://slidepdf.com/reader/full/emacstips 1/6
Emacs as Your IDE
T his is a no-frills list ing towards using Emacs as a development environment. C-xmeans CTRL-x. M-x means ALT -x (or ESC and then x).
You can press the TAB key almost anywhere for autocomplet ion. Wherever you inputtext, use M-p and M-n to traverse up and down the history list .
C-u is the numeric-prefix operator. To go up by 10 lines, for example, one would sayC-u 10 C-p.
Stuck!
Exit Emacs: C-x C-cCancel command: C-g
File HandlingOpen a file: C-x C-f (Remote file: /user@host:path/to/file via ftp)Change to a directory: C-x dSave file: C-x C-sSave as: C-x C-wClose a file (kill buffer): C-x k Reload file: C-x C-vChange to another open file: C-x bList all open files (buffers): C-x C-bMaximise a window: C-x 1Switch to the other window: C-x o
Editing
Start marking: C-<SPACE> (Use arrow keys to select region)Cut: C-wCopy: M-wPaste: C-y (previous: M-y)Undo: C-x uDelete word: M-dComplete word: M-/Open a new line: C-o
Kill upt o end of line: C-k (Is appended to clipboard)Indent current line to GNU C standard: <TAB>T oggle read-only: C-x C-qParagraph-ise current region: M-qSpell-check current word: M-$ (whole file: M-x ispell)
Search/Replace
Emacs as Your IDE http://deep.syminet.com/emacside.h
of 6 01/11/11 0
8/7/2019 EmacsTips
http://slidepdf.com/reader/full/emacstips 2/6
Search forward: C-s (Press C-s to search again)Search backward: C-rSearch and replace: M-%List occurrences: M-x occurRun grep: M-x grepReg-ex search: C-M-s
[XKCD pays tribute to Emacs]
Navigation
Beginning of file: M-<End of file: M->Jump back to where you were from beginning/end: C-u C-<spc>Beginning of line: C-aEnd of line: C-ePrevious word: M-bNext word: M-f Previous matching bracket: C-M-b (if it doesn't work, try ESC followed by C-b)
Next matching bracket: C-M-f (or ESC C-f)Out line mode: C-u 1 C-x $ (C-x $ to revert)Go to line: M-g (or M-x goto-line) T he arrow area is so far away!?Previous line: C-pNext line: C-nPrevious character: C-b
Emacs as Your IDE http://deep.syminet.com/emacside.h
2 of 6 01/11/11 0
8/7/2019 EmacsTips
http://slidepdf.com/reader/full/emacstips 3/6
Next character: C-f Page Up: M-vPage Down: C-v
Vertical Copy
Sometimes you will need to copy a vertical patch of data, e.g. one column in a table.
First press C-<space> where you want to start copying. T hen go to the end of thecolumn and press C-x r k. T o paste the column press C-x r y. (If you don't want todelete original column, just press C-_ there once to restore it and then press C-x r yat target .)
Registers
T o save the current location in register a: C-x r <SPC> aT o jump to the location in register a: C-x j aT o list saved registers: C-x r l
Symbol Lookup
Assuming CODEDIR to be the t op-level source directory, first u pdate your ~/.bashrc
like so:
alias mktags='cd $CODEDIR && etags `find $CODEDIR -name "*.[h|c]"` && cd -'
T hen run:
source ~/.bashrc
mktags
When Emacs asks for the T AGS file, specify $CODEDIR/T AGS.
Go to function/variable definition: M-. (M-* to return)Next definition of the tag: C-u M-. (e.g. same method in multiple classes)Search for occurrences of symbols: M-x t ags-search (M-, to search forward)Find symbols as reg-exps: M-x find-tag-regexpAutocomplete symbol: M-<TAB>
Re-run mktags when the codebase has changed drastically.
Compilation and Debugging
Compile: M-x compile
Stop compilation: C-c C-k Go to next error: C-x `Go to source line giving the error: C-c C-cDebug with GDB: M-x gdbInsert breakpoint: C-x <SPACE>Run a shell command: M-! (C-u M-! to insert output)Open a shell: M-x shell
Emacs as Your IDE http://deep.syminet.com/emacside.h
3 of 6 01/11/11 0
8/7/2019 EmacsTips
http://slidepdf.com/reader/full/emacstips 4/6
T o change the default s tring for M-x compile, edit your ~/.emacs file:
(setq compile-command "gmake -f Makefile.gnu all")
Comparing Two Files
Start a diff session: M-x ediff
Next match: nPrevious match: pCopy from file A to B: aCopy from file B to A: bSave file X: wXSee diff output: DIgnore whitespace differences: ##Compare directories: M-x ediff-directories
Keyboard Macros
Begin recording: C-x (
End recording: C-x )Execute macro: C-x e
Example: Add 'extern' before all function protoypes in a file.
Go to the f irst prototype, and say:
C-x (
C-a
extern<SPACE>
C-n
C-x )
T hen say C-u 50 C-x e to replace 50 prototypes.
Perforce CVS Integration
Get p4.el and then follow these steps.
$ emacs -batch -f batch-byte-compile p4.el
Put the p4.el in some directory, say /home/foo/bar . Then edit your ~/.emacs asfollows:
(setq load-path (cons " /home/foo/bar" load-path))
(load-library "p4")
You should see a P4 menu in Emacs, and you can do CVS operations from withinEmacs now.
Symbol Lookup with cscope
Download cscope program, and xcscope.el and cscope-indexer from an OSS repository.Save the xcscope.el onto some directory in Emacs' load-path. Edit your ~/.emacs as
Emacs as Your IDE http://deep.syminet.com/emacside.h
4 of 6 01/11/11 0
8/7/2019 EmacsTips
http://slidepdf.com/reader/full/emacstips 5/6
follows:
(require 'xcscope)
T o add a directory ~/foo to your load-path, use t he following direct ive in .emacs file,making sure it appears before the "require xcscope" directive:
(setq load-path (cons "~/foo" load-path))
Place cscope-indexer in your $PATH. You can then use these keystrokes whilebrowsing C source code:
Index files: C-c s IFind global definition: C-c s dFind symbol definition: C-c s sFind this file: C-c s f Find funct ions calling this function: C-c s cFind functions called by this function: C-c s CFind this patt ern: C-c s eFind this text string: C-c s t
Find files including this file: C-c s i
Java
cscope can be used to browse Java symbols too. Use these commands at thetop-level directory to create the index:
find . -name "*.java" > cscope.files
cscope -b
T hen add this line at the bottom of xcscope.el to load cscope-mode whenever a Javafile is opened:
(add-hook 'java-mode-hook (funct ion cscope:hook))
File Operations
Operations such as move, copy, delete etc. are available in the directory editor: M-xdired
Calculations
Built-in calculator: M-x calculator
Change input base: i (b | o | h)Change ou tput base: o (b | o | h)
More Help
Built- in tu torial: C-h tInfo reader: C-h i
Emacs as Your IDE http://deep.syminet.com/emacside.h
5 of 6 01/11/11 0
Recommended