wiki:EmacsTips
Last modified 6 years ago Last modified on 22.04.2013 23:32:50

Emacs Tips

How to make Emacs an even more pleasant place to hack on Fawkes.

Fedora Packages

You might want to install the following packages:

yum install protobuf-emacs clips-emacs emacs-git emacs-lua

Custom Extensions

Custom Script Directory

We assume that we can place custom Lisp files into the directory ~/.emacs.d/lisp. After creating the directory, add to your .emacs file:

(add-to-list 'load-path "~/.emacs.d/lisp/")

Reloading Config

There are (at least) two ways to reload the configuration. You can either restart Emacs, or do M-x load-file and then give ~/.emacs as file path.

Emacs YAML mode

To make editing of the configuration easier and more comfortable we recommend to install the Emacs YAML major mode. It supports syntax highlighting of the configuration file and a few commands to support editing. The following procedure describes how to install the mode in your home directory.

git clone https://github.com/yoshiki/yaml-mode
cp yaml-mode/yaml-mode.el ~/.emacs.d/lisp

Then edit the file ~/.emacs and add the following (the first line is to add the just created directory as a path to look for Emacs LISP files):

(require 'yaml-mode)
(add-to-list 'auto-mode-alist '("\\.yaml$" . yaml-mode))
(add-hook 'yaml-mode-hook
          '(lambda () (define-key yaml-mode-map "\C-m" 'newline-and-indent)))

Colored Compilation Output

The Fawkes build system uses colored output. By default, Emacs is not able to handle this nicely. To fix this, add the following to your .emacs file:

; Support ANSI color codes in shell mode, e.g. for compilation
(require 'ansi-color)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
(defun colorize-compilation-buffer ()
  (toggle-read-only)
  (ansi-color-apply-on-region (point-min) (point-max))
  (toggle-read-only))
(add-hook 'compilation-filter-hook 'colorize-compilation-buffer)

Keyboard Shortcuts

Some additional keyboard shortcuts could be helpful to speed things up.

C-c C-c to compile

To setup the sequence C-c C-c to compile add the following to your .emacs file:

(add-hook 'c++-mode-hook
  '(lambda ()
    (define-key c++-mode-map "\C-c\C-c" 'compile)
   )
)

(add-hook 'c-mode-hook
  '(lambda ()
    (define-key c-mode-map "\C-c\C-c" 'compile)
   )
)

C-c d to insert current date

To insert the current date at the cursor, e.g. when creating a file for the header, add the following to your .emacs file:

;  (insert (format-time-string "%a %b %d %H:%M:%S %Y"))
(defun insert-date-time (arg)
  (interactive "P")
  (let ((format "%a %b %d %H:%M:%S %Y")
	(system-time-locale "en_US"))
    (insert (format-time-string format)))
  )
(global-set-key (kbd "C-c d") 'insert-date-time)

C-% to go to matching parenthesis

For long code blocks it is often hard to find the matching opening or closing parenthesis. The following code allows to press C-% (on a German keyboard that is Ctrl-Shift-5) to get to the parenthesis matching the one at the current cursor position.

(defun forward-or-backward-sexp (&optional arg)
  "Go to the matching parenthesis character if one is adjacent to point."
  (interactive "^p")
  (cond ((looking-at "\\s(") (forward-sexp arg))
        ((looking-back "\\s)" 1) (backward-sexp arg))
        ;; Now, try to succeed from inside of a bracket
        ((looking-at "\\s)") (forward-char) (backward-sexp arg))
        ((looking-back "\\s(" 1) (backward-char) (forward-sexp arg))))

(global-set-key (kbd "C-%") 'forward-or-backward-sexp)