Emacs can be used as a unified environment for software development
under UNIX. If things are set up properly, you can use it for
everything.
We'll assume you're using Emacs 19. On Athena this
is now the default. Some places are still using Emacs 18 or Emacs 20.
Key bindings
- C-x means to hold down control and press the x key
- M-x means to hold down the meta (or alt or compose) key
and press the x key.
- M-x can also be done by pressing ESCAPE then
pressing the x key.
- C-x C-y means to hold down control and press the x key
then let go then hold down control and press the y key
- M-C-x means to hold down control and alt keys and press the x key
Buffers and files and windows
- C-x C-f
- open file in a new buffer
- C-x C-w
- write file out with a different name
- C-x C-s
- save buffer under current name
- C-x b
- switch to buffer
- C-x k
- kill current buffer
- C-g
- cancel
- C-_
- undo
C-x 2
split current window
C-x o
switch to other window
C-x 1
make current window only window
C-x 0
remove current window
C-x 5 2
make another X window
C-x 5 0
delete the current X window
.emacs file and elisp
- At the core of Emacs is an interpreter for elisp, a variant
of LISP
- Emacs is configured by writing elisp code
- Single line commands can be entered with:
- M-:
- enter elisp command
- .emacs file run on startup
- Add emacs19 specific things to .emacs with:
(defun startup19 () "startup for emacs19"
(...)
(...) ; Stuff for emacs19 only
(...)
(cond (window system
(...)
(...) ; Stuff for emacs19 and X only
(...)
))
)
(if (string-match "^19" emacs-version) (startup19))
- Examples in dotfiles locker on Athena:
add dotfiles; cd /mit/dotfiles/Emacs
- Enter more complicated commands in lisp-interaction-mode:
- M-x lisp-interaction-mode
- enters lisp-interaction-mode
- C-j
- executes commands in lisp-interaction-mode
Regions
- Used to select regions of text on which to perform an operation
- Select with C-SPACE then move to end
- Also select with mouse button 1 and drag
Editing C code in c-mode
M-x compile
- In c-mode, M-x compile runs make for you
- To make C-c C-v do a compile in c-mode:
;; Use C-c C-v in c-mode to do a compile
(defun my-save-and-compile ()
(interactive "")
(save-buffer 0)
(compile "make -k"))
(define-key c-mode-map "\C-c\C-v" 'my-save-and-compile)
If global-map is used instead of c-mode-map
then applies to makefile and other modes.
- Middle button over an error goes to it in the source
- M-n goes to next error with:
(define-key c-mode-map "\M-n" 'next-error)
Tags
- Keeps track of what is defined where
- Generate TAGS file with:
etags *.[ch]
- To go to where a function is defined, do M-.
over the function call
- To do a query replace over all files,
do M-x tags-query-replace
- To search for something across all files, M-x tags-search
- To go to next occurance of search string, M-,
Faces and colorization
- List faces with M-x list-faces-display
- (set-face-foreground 'face "color")
- (set-face-background 'face "color")
- Highlight active region with:
(transient-mark-mode 1)
- Use either hilit19 or font-lock minor mode to enable hilighting
(font-lock-mode 1)
or
(require 'hilit19)
- Update hilighting with C-=
- Colorize on file writes:
;;; colorize on file writes
(if (not (memq 'hilit-rehighlight-buffer write-file-hooks))
(setq write-file-hooks
(cons 'hilit-rehighlight-buffer write-file-hooks)))
- Example of setting C coloring for hilit19:
;; comments
(hilit-translate comment 'pink)
;; string constants
(hilit-translate string 'lightsalmon)
;; "#include"
(hilit-translate include 'yellow)
;; keywords (if, for, ...)
(hilit-translate keyword 'green)
;; function definitions
(hilit-translate defun 'aquamarine2)
;; "#define"
(hilit-translate define 'yellow)
;; struct definitions
(hilit-translate decl 'gray80)
- Add colorization to compiles with:
;; Hilit for compilation-mode
(hilit-set-mode-patterns
'compilation-mode
'(("^[a-z./_A-Z0-9]*:[0-9]*:" nil keyword)
("^\W\\^\W$" nil error)
("^make.*: \\*\\*\\*.*$" nil error)
("^make.*: \\(Entering\\|Leaving\\) directory.*$" nil define))
nil 'case-insensitive)
(add-hook 'compilation-mode-hook 'hilit-rehighlight-buffer)
(setq compilation-finish-function
(lambda (cb msg)
(hilit-rehighlight-buffer cb)))
Adding filename to mode mappings
- Can add mappings with something like:
(setq auto-mode-alist
(append '(("\\.pl$" . perl-mode)
("\\.txt$" . text-mode)
("\\.doc$" . text-mode)
("\\.java$" . java-mode)
("\\.perl$" . perl-mode))
auto-mode-alist))
- Can add modes from lockers with things like:
(autoload 'java-mode "/afs/sipb/contrib/emacs/elisp19/cc-mode"
"java mode for java code" t)
Using the shell within emacs
- Start shell with M-x shell
- For command line history, use M-p and M-n
Using GDB within emacs
- Start with M-x gdb
- For command line history, use M-p and M-n
- Some other commands:
- C-c TAB
- step one statement
- C-x C-a C-b
- in c file will set a break point at that line
Obtaining more documentation
- M-x info enters info mode
- Clicking with middle mouse button follows hyperlinks
- Provides documentation for lots of GNU software
- Menus on toolbar provides access to info and features
- C-h m provides help with the current mode
File management with dired
- Use C-x C-f to open up a directory rather than a file
- f descends into subdirs and opens files
- o opens files in other windows
- d marks files for deletion
- u unmarks marked files
- x deletes marked files
- C copies files
- R renames files
![[BACK]](images/back.gif)
Prepared by
Erik Nygren (nygren@mit.edu)
and
Mike Whitson (mwhitson@mit.edu)