The Notorious Mick Beaver’s Guide to Using Emacs With C++
GNU Emacs is not only a great text editor, but also a great IDE for developing with C++. As of January 2011, the documentation for putting all of the pieces together was, arguably, scattered.
- Install GNU Emacs
- Mac OS X - http://emacsformacosx.com/
- Microsoft Windows
- Download ftp://ftp.gnu.org/gnu/emacs/windows/emacs-23.2-bin-i386.zip
- Unzip
- Put in “C:\Program Files (x86)”
- Double-click “C:\Program Files (x86)\emacs-23.2\bin\addpm.exe” to add GNU Emacs to the Start Menu
- Linux - You KNOW what to do.
- Configure Emacs
- Emacs uses a configuration file named ‘.emacs’
- Mac OS X - put it in your home directory ‘~’
- Microsoft Windows - put it in %APPDATA%, or the folder specified by the %HOME% environment variable.
- Prosper.
Tools
- Semantic
- This is the C++ code analyzer and completer
- GDB-UI
- The new visual debugger built into Emacs that uses GDB as the backend debugger
- C-style
- Emacs will help you indent your code as you go. There are a number of different built-in styles, but you can define your own if you want
- Compile
- Build your code from Emacs. This will give you the opportunity to jump directly to warnings and errors in the compilation process.
- Speedbar
- The Emacs file browser. When coupled with Semantic, it can be a code browser.
Keybindings/Commands To Know
- C-h t - tutorial
- C-h w - Type in a function/command and it will tell you which keys it is bound to
- C-h k - Type in a key/key combination and tell you which command is bound to it
- C-c , J - Jump to the definition of the symbol under point (“point” means the logical position right before the cursor)
- M-x speedbar - Start the Speedbar file/code browser
- M-x gdb - Start the GDB debugger
- M-x shell - Start a command line shell within Emacs
- M-x ielm - Interactive Emacs Lisp Mode to test out your Elisp code
- M-x describe-function - Get help on a function/command
- M-x describe-variable - Get help on a settings variable
Configuration
;; Here is a sample .emacs configuration for working with C++.
;; Note that everything after a semicolon is a comment.
;; Part I. Essentials to editing
;; Specify a directory where to load additional Emacs Lisp files.
(add-to-list 'load-path (expand-file-name "~/.emacs.d"))
;; Use spaces instead of tabs
(setq-default indent-tabs-mode nil)
;; Turns on syntax highlighting.
(global-font-lock-mode t)
;; Sets the syntax highlighting to the maximum amount of colorization.
(setq font-lock-maximum-decoration t)
;; Do not make backup recovery files when editing.
(setq make-backup-files nil)
;; Use Shift + the arrow keys to move between windows in a frame
(when (fboundp 'windmove-default-keybindings)
(windmove-default-keybindings))
;; Part II. Configure Emacs as a great C++ IDE
;; Include the C/C++ mode Elisp package
(require 'cc-mode)
;; In versions of Emacs greater than 23.2, do the following
(when (or (> emacs-major-version 23)
(and (= emacs-major-version 23)
(>= emacs-minor-version 2)))
;; Use the GDB visual debugging mode
(setq gdb-many-windows t)
;; Turn Semantic on
(semantic-mode 1)
;; Try to make completions when not typing
(global-semantic-idle-completions-mode 1)
;; Use the Semantic speedbar additions
(add-hook 'speedbar-load-hook (lambda () (require 'semantic/sb))))
;; Treat .h files as C++ files (instead of C)
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
;; Use the indentation style of "The C++ Programming Language"
(setq c-default-style "stroustrup")
;; Return adds a newline and indents
(define-key c-mode-base-map (kbd "RET") 'newline-and-indent)
;; Run compile when you press F5
(global-set-key (kbd "<f5>") 'compile)
Where To Go For Help
- Emacs Wiki - http://www.emacswiki.org
- Emacs Manual - http://www.gnu.org/software/emacs/manual/emacs.html
- An Introduction to Programming in Emacs Lisp - http://www.gnu.org/software/emacs/emacs-lisp-intro/html_node/index.html
- Emacs Lisp Reference - http://www.gnu.org/software/emacs/manual/elisp.html
- Semantic Manual - http://www.gnu.org/software/emacs/manual/html_node/semantic/
- Emacs Quick Reference Card
- C:\Program Files (x86)\emacs-23.2\etc\refcards\refcard.pdf
- http://pages.cs.wisc.edu/~mick/downloads/emacs/23.2/etc/refcards/refcard.pdf
- Practical Common Lisp (helps with understanding Emacs Lisp) - http://www.gigamonkeys.com/book/