Emacs Advanced Tips

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page is some commonly needed operations in emacs.

To call a emacs command named “xyz”, press 【Alt+x】 then type “xyz”, Enter ↵.

Search Text, Find/Replace Text

How to search text?

Press 【Ctrl+s】 (isearch-forward), then type your text. Emacs will search as you type. To find the next occurrence, press 【Ctrl+s】 again. To go to previous occurrence, press 【Ctrl+r】. To stop, press Enter ↵ to leave the cursor there. Or type 【Ctrl+g】 to return to the spot before search was started.

This command is also under the menu 〖Edit ▸ Search〗.

Ctrl+s】 twice will search your last searched word.

For similar commands, see: Emacs: Search & Highlight Words.

To search text in a directory, see: Emacs: Searching for Text in Files (grep, find).

How to find/replace?

Type 【Alt+%】 (query-replace). Emacs will prompt you for the find string and replace string.

Once emacs found a match, you can press y to replace, n to skip, or ! to do all replacement without asking.

If you made a mistake, you can cancel by pressing 【Ctrl+g】. If you want to revert the find/replace you did, you can undo by 【Ctrl+/】.

For regex replace and detail on issues of letter cases, see: Emacs: Find & Replace Tutorial.

Find/Replace menus in emacs
emacs menu for find/replace commands

How to find/replace for all files in a dir?

  1. Call dired to go to a dir.
  2. Mark the files you want to work on (m to mark, u to unmark).
  3. Call dired-do-query-replace-regexpQ】.

For detail, see: Interactive Find/Replace String Patterns on Multiple Files.

Editing Related Questions

How to insert/delete comment?

  1. Select a block of text. (call set-mark-commandCtrl+Space】 to mark, then move cursor.)
  2. call comment-dwimAlt+;】 to make the region into a comment or uncomment.

How to add a prefix to every line? (such as # or //)

  1. Move to the beginning of first line you want.
  2. Press 【Ctrl+Space】 (set-mark-command) to mark cursor position.
  3. Move cursor to the beginning of the last line.
  4. Then call string-rectangleCtrl+x r t
  5. Then type what you want to insert, then Enter ↵.

The command string-rectangle can be used to insert a vertical column of string across mulitple lines at any column position, not just at the beginning of lines.

For detail, see: Emacs: Manipulate Column Text, string-rectangle, ASCII-Art.

How to delete the first few n chars of every line?

Mark 【Ctrl+Space】 the beginning of first line and move cursor to the last line, and move it to the right n chars. Then call kill-rectangleCtrl+x r k】.

kill-rectangle can be used to delete any rectangular block of text, not just at the beginning of lines.

How to replace unprintable characters such as tab or newline characters in Emacs?

Call query-replaceAlt+%】. When you need to insert a tab character, type 【Ctrl+q】 first, then press Tab ↹. Same for inserting a line return.

For detail, see: Emacs: Newline Representations ^M ^J ^L.

How to change file line endings between Windows/Unix/Mac?

Call set-buffer-file-coding-system, then give a value of “mac”, “dos”, “unix”. Then, save.

For detail, see: Emacs: Newline Representations ^M ^J ^L

How to record a sequence of keystrokes?

Emacs: Keyboard Macro

How to move thru camelCaseWords?

You can set emacs so that word moving commands will move cursor into between CamelCaseWords.

Call global-subword-mode. Subword mode is available since Emacs 23.2 (). 〔➤ Emacs 23.2 Features (released 2010-05)

To set it permanently, put this in your emacs init file:

(global-subword-mode 1) ; 1 for on, 0 for off

How to have spell-checker turned on?

Call flyspell-mode or flyspell-buffer. This is under the menu 〖Tools ▸ Spell Checking〗.

To have it always on, put the following in your emacs init file:

(defun my-turn-spell-checking-on ()
  "Turn flyspell-mode on."
  (flyspell-mode 1)

(add-hook 'text-mode-hook 'my-turn-spell-checking-on)

For discussion of problems about spell checking, see Emacs Spell Checker Problems.

Emacs Customization

How to Set Emacs's User Interface to Modern Conventions

Like it? Buy Xah Emacs Tutorial.
blog comments powered by Disqus