Emacs: Indentation/Completion Commands

By Xah Lee. Date: . Last updated: .

Here's how to indent code.

Indent Current Line

Alt+x indent-for-tab-commandTab】.

Indent a Text Selection

Select a region, then

Alt+x indent-regionCtrl+Alt+\

More indent commands:

indent-relative
Indent current line in the same style as previous non-blank line.
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.
indent-rigidly
Indent all lines in a region, right or left.

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 Representation ^M ^J ^L]

Convert Tabs to Space in Source Code

Select a region first, then Alt+x untabify

Use tabify to change spaces to tabs.

Make Whitespace Visible

Emacs: Make Whitespaces Visible

Name Completion

Emacs: Name Completion

Tab/Space/Indent Setup

Emacs: Tab/Space/Indent Setup

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

ELisp Examples

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

Working with Brackets

Org Mode

HTML

Emacs Efficiency

Misc