Emacs: Indentation Commands

By Xah Lee. Date: . Last updated: .

Indent Current Line

indent-for-tab-commandTab
indent current line or region.

Indent a Text Selection

Alt+x indent-regionCtrl+Alt+\
Indent a region. Select a region first.
Alt+x indent-rigidly
Indent all lines in a region in a fixed way. Press to move to right or to left.

More indent commands:

Alt+x indent-relative
Indent current line in the same style as previous non-blank line.
Alt+x indent-relative-maybe
Like indent-relative indent current line like previous non-blank line, only if the previous line has more indent than current line.

Force Insert Tab Character

The following method works everywhere (also works in minibuffer).

To insert a literal tab char, press Ctrl+q Tab.

To insert a newline char, type Ctrl+q Ctrl+j.

You need to use the above method to insert these characters, because for example in minibuffer, pressing Tab does name completion and pressing Enter finishes the prompt. In most programing language modes, pressing Enter or Tab also does some auto indenting.

[see Emacs: Newline Convention CR LF]

Convert Tabs to Space in Source Code

Alt+x untabify
Convert tabs to spaces. In region. Press Ctrl+u first to do whole buffer.
Alt+x tabify
Convert spaces to tabs. In region. Press Ctrl+u first to do whole buffer.

Make Whitespace Visible

Emacs: Make Whitespaces Visible

Name Completion

Emacs: Name Completion

Tab/Space/Indent Setup

Emacs: Tab/Space/Indent Setup

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

File

Buffer

Find Replace

Copy/Paste

Unicode

Whitespace

Rectangle Edit

Line Wrap

Shell

View Special File

Editing Brackets

Org Mode

HTML

Emacs Efficiency

Misc