Emacs Basics Tips

By Xah Lee. Date: . Last updated: .

Here's answer to most frequently asked questions.

How to search text?

  1. Alt+x isearch-forwardCtrl+s】, then type your text. Emacs will search as you type.
  2. To find the next occurrence, press Ctrl+s again.
  3. To go to previous occurrence, press Ctrl+r. (emacs will first move to begging of current occurrence)
  4. To stop, press Enter to leave the cursor there.
  5. pressCtrl+g to return to the spot before search was started.

[see Emacs: Search Text in Buffer]

[see Emacs: Search Text in Directory]

How to find/replace?

Alt+x query-replaceAlt+%】. Emacs will prompt you for the find string and replace string.

Once emacs found a match, press

To undo, cancel search then Ctrl+_.

Find Replace menus in emacs
emacs menu for find replace commands

[see Emacs: Find and Replace Commands]

How to find replace for all files in a dir?

Emacs: Find Replace Text in Directory

How to insert/delete comment?

  1. Alt+x set-mark-commandCtrl+Space】 to mark, then move cursor to select text.
  2. Alt+x comment-dwimAlt+;】 to make the region into a comment or uncomment.

[see Emacs: Toggle Comment Current Line]

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 Alt+x 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.

[see Emacs: Edit Column Text, Rectangle Commands]

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】.

Alt+x 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?

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

[see Emacs: Newline Representation ^M ^J ^L]

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

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

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.

Alt+x global-subword-mode.

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

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

[see Emacs: Move Cursor by camelCase, snake_case]

How to have spell-checker turned on?

Alt+x flyspell-mode or flyspell-buffer.

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)

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode

Emacs Tutorial

Quick Start

Manage Windows



Find Replace




Rectangle Edit

Line Wrap


View Doc

Working with Brackets

Power Editing

Org Mode


Emacs Efficiency