Emacs Basics Tips

By Xah Lee. Date: . Last updated: .

Here's answer to most frequently asked questions.

How to search text?

How to find replace?

Emacs: Find Replace in Current File

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 //)

Emacs: Edit Column Text, Rectangle Commands

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

Emacs: Edit Column Text, Rectangle Commands

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 Convention CR LF]

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?

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)

How to find a command?

here are 4 commands every emacs user should know.

Alt+x describe-key
Find the command name of a keyboard shortcut.
Alt+x describe-function
Find the keyboard shortcut of a command.
Alt+x apropos-command
Search command by name.
Alt+x describe-mode
Show current mode's documentation.

For example, Alt+x describe-key, Enter, then press Ctrl+f. Emacs will then tell you the commad name corresponding to the key you just pressed.

Now try the other commands above.

List Lines

Emacs: List/Delete Matching Lines, Sort Lines

Highlight Words

Emacs Highlighting Commands

Sort Lines

Emacs: List/Delete Matching Lines, Sort Lines

Delete Trailing White Spaces

Emacs: Delete Trailing Whitespace

Make Spaces and Tabs Visible

Alt+x whitespace-mode. [see Emacs: Make Whitespaces Visible]

Multiple Clipboards

Emacs: Copy to Register

No restarting emacs when add to init file

Just select the new emacs lisp code, then Alt+x eval-region.

[see Emacs: Init File Tutorial]

Start a second shell

Ctrl+u Alt+x shell.

[see Emacs: Run Shell in Emacs]

Refresh a opened file to its saved state

Alt+x revert-buffer. If someone modified the file, typically emacs will auto detect it, and when you type something, emacs will automatically ask you what to do.

;; set file to auto refresh when change detected (For example, changed by other)
(global-auto-revert-mode 1)

Open file as hex-decimal (byte-code editor)

To open the file as hex, Alt+x hexl-find-file. If the file is already opened, Alt+x hexl-mode.

[see Emacs: Set Default Major Mode]

Set background color

Emacs: Set Color Theme

Spent a few hours here?

Emacs Tutorial

Emacs Init

Emacs Keys

Elisp Basics

Elisp Examples

Elisp Advanced

Elisp Write Major Mode

Emacs Tutorial

Quick Start

Manage Windows



Find Replace




Rectangle Edit

Line Wrap


View Special File

Editing Brackets

Org Mode


Emacs Efficiency