The Way of the great learning involves manifesting virtue, renovating the people, and abiding by the highest good.

2009年2月27日星期五

Emacs as Your IDE

This is a no-frills listing towards using Emacs as a development environment. C-x means CTRL-x. M-x means ALT-x (or ESC and then x).

You can press the TAB key almost anywhere for autocompletion. Wherever you input text, 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 say C-u 10 C-p.

Stuck!

  • Exit Emacs: C-x C-c
  • Cancel command: C-g

File Handling

  • Open a file: C-x C-f (Remote file: /user@host:path/to/file via ftp)
  • Change to a directory: C-x d
  • Save file: C-x C-s
  • Save as: C-x C-w
  • Close a file (kill buffer): C-x k
  • Reload file: C-x C-v
  • Change to another open file: C-x b
  • List all open files (buffers): C-x C-b
  • Maximise a window: C-x 1
  • Switch to the other window: C-x o

Editing

  • Start marking: C- (Use arrow keys to select region)
  • Cut: C-w
  • Copy: M-w
  • Paste: C-y (previous: M-y)
  • Undo: C-x u
  • Delete word: M-d
  • Complete word: M-/
  • Open a new line: C-o
  • Kill upto end of line: C-k (Is appended to clipboard)
  • Indent current line to GNU C standard:
  • Toggle read-only: C-x C-q
  • Paragraph-ise current region: M-q
  • Spell-check current word: M-$ (whole file: M-x ispell)

Search/Replace

  • Search forward: C-s (Press C-s to search again)
  • Search backward: C-r
  • Search and replace: M-%
  • List occurrences: M-x occur
  • Run grep: M-x grep
  • Reg-ex search: C-M-s

[XKCD pays tribute to Emacs]

Navigation

  • Beginning of file: M-<
  • End of file: M->
  • Beginning of line: C-a
  • End of line: C-e
  • Previous word: M-b
  • Next 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)
  • Outline mode: C-u 1 C-x $ (C-x $ to revert)
  • Go to line: M-g (or M-x goto-line)
     
    The arrow area is so far away!?
  • Previous line: C-p
  • Next line: C-n
  • Previous character: C-b
  • Next character: C-f
  • Page Up: M-v
  • Page 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- where you want to start copying. Then go to the end of the column and press C-x r k. To paste the column press C-x r y. (If you don't want to delete original column, just press C-_ there once to restore it and then press C-x r y at target.)

Registers

  • To save the current location in register a: C-x r a
  • To jump to the location in register a: C-x j a
  • To list saved registers: C-x r l

Symbol Lookup

Assuming CODEDIR to be the top-level source directory, first update your ~/.bashrc like so:
alias mktags='cd $CODEDIR && etags `find $CODEDIR -name "*.[h|c]"` && cd -' 

Then run:

source ~/.bashrc mktags 

When Emacs asks for the TAGS file, specify $CODEDIR/TAGS.

  • 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 tags-search (M-, to search forward)
  • Find symbols as reg-exps: M-x find-tag-regexp
  • Autocomplete symbol: M-
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-c
  • Debug with GDB: M-x gdb
  • Insert breakpoint: C-x
  • Run a shell command: M-! (C-u M-! to insert output)
  • Open a shell: M-x shell
To change the default string 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: n
  • Previous match: p
  • Copy from file A to B: a
  • Copy from file B to A: b
  • Save file X: wX
  • See diff output: D
  • Ignore 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 first prototype, and say:

    C-x (     C-a     extern     C-n     C-x ) 
Then 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 as follows:

(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 within Emacs 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 follows:
(require 'xcscope) 
To add a directory ~/foo to your load-path, use the following directive 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 while browsing C source code:

  • Index files: C-c s I
  • Find global definition: C-c s d
  • Find symbol definition: C-c s s
  • Find this file: C-c s f
  • Find functions calling this function: C-c s c
  • Find functions called by this function: C-c s C
  • Find this pattern: C-c s e
  • Find 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 the top-level directory to create the index:
find . -name "*.java" > cscope.files cscope -b  
Then add this line at the bottom of xcscope.el to load cscope-mode whenever a Java file is opened:
(add-hook 'java-mode-hook (function cscope:hook))  

File Operations

Operations such as move, copy, delete etc. are available in the directory editor: M-x dired

Calculations

  • Built-in calculator: M-x calculator
  • Change input base: i (b | o | h)
  • Change output base: o (b | o | h)

More Help

  • Built-in tutorial: C-h t
  • Info reader: C-h i

Nervous Breakdown

Get therapy from a psychiatrist: M-x doctor

没有评论: