Emacs Esoteric Tips

Xah Lee, 2006-04

This page shows some advanced emacs tips.

Before continuing, here are some things every serious emacs user should know. I used them about daily. (And they are all listed under the graphical menu Help)

• To find the command name of a keybinding, use the command describe-key (Ctrl+h k). This command also lets you find out the command name of a graphical menu or mouse click.

• To find the documentation of a command, or find its keybinding, use describe-function (Ctrl+h f).

• If you forgot the name of a command, you can use apropos-command (Ctrl+h a), which lists all commands of a given string.

• In any mode, if you want to see its documentation, use describe-mode (Ctrl+h m).

Editing Related

I'm annoyed at empty lines with white spaces or white spaces trailing after semi-colon in code.

delete-trailing-whitespace.

I have a set of different texts i want to paste frequently. Is there a way to use like multiple clipboards?

Yes. Select the region, then type “Alt+x copy-to-register”, give it a name such as 1. To paste it, use insert-register.

Alternatively, you can define a function and bind it to a key, so that it inserts the string when you press a button. Here's a example:

(defun insert-p ()
  "insert <p></p> at cursor point."
  (interactive)
  (insert "<p></p>")
  (backward-char 4))

(global-set-key (kbd "<f8>") 'insert-p)

Put the above in your emacs customization file.

Lisp Edit

How to match parenthesis?

show-paren-mode. Also, backward-up-list (Ctrl+Alt+↑) and forward-sexp (Ctrl+Alt+→).

For more detail, see: Tips For Editing Lisp Code With Emacs.

Suppose you are working in lisp. How to select the entire lisp block the cursor is on?

“Ctrl+Alt+↑” a few times until you reach the top or desired block, then “Ctrl+Alt+Space” to select the whole paren block.

For more detail, see: Tips For Editing Lisp Code With Emacs.

In elisp file (myFile.el), is there a way to jump to a function's definition the cursor is on?

find-function.

Grep Lines

How to list lines containing a specific string?

list-matching-lines. There's also delete-matching-lines.

How to highlight all occurrences of word in a file?

Type “Alt+x highlight-regexp”.

How to sort lines?

sort-lines, reverse-region, sort-fields, sort-numeric-fields. These are the most useful, but there are few others. Type “Alt+x apropos-command” with search string “sort”.

Misc

How to “refresh” a opened file to its current saved state? For example, other people have edited the file thru a network.

revert-buffer. Alternatively, just type something in the buffer and emacs will automatically warn you with a prompt; answer r to revert.

Is there a way for emacs to interpret the file as hex-decimal? (i.e. byte-code editor)

hexl-find-file, or “Alt+x hexl-mode”.

Display

How to set up emacs so that each buffer opens in another frame?

“(setq pop-up-frames t)”

How to set the background color so that all new “frames” will have that color? (not set-background-color)

“Alt+x customize-variable RET default-frame-alist RET”

If you want to code elisp manually, see Ctrl+h v default-frame-alist. Here's a example you can put in your emacs customization file:

(setq default-frame-alist
  '((tool-bar-lines . 0)
    (menu-bar-lines . 1)
    (background-color . "cornsilk")))

Alias Long Commands to Shorter Names

Many emacs's commands have long names. You can use the alias function to make them shorter for frequently used commands.

For example, here is some of my aliases:

; get rid of yes-or-no questions. y or n is enough
(defalias 'yes-or-no-p 'y-or-n-p)

; shortening of often used commands
(defalias 'ddqrr 'dired-do-query-replace-regexp)

(defalias 'gf 'grep-find)
(defalias 'fd 'find-dired)
(defalias 'sh 'shell)

(defalias 'qrr 'query-replace-regexp)
(defalias 'ntr 'narrow-to-region)
(defalias 'lml 'list-matching-lines)
(defalias 'dml 'delete-matching-lines)
(defalias 'rof 'recentf-open-files)

(defalias 'eb 'eval-buffer)
(defalias 'er 'eval-region)

The default keyboard shortcut for execute-extended-command is “Alt+x”. The x is in the middle of keyboard and is one of the slowest key to reach. You can define “Alt+a”, which is right under the finger. (See A Ergonomic Keyboard Shortcut Layout For Emacs)

(global-set-key (kbd "M-a") 'execute-extended-command) ; was backward-sentence

Related essays:


Page created: 2005-08.
© 2005 by Xah Lee.
Xah Signet