Emacs: Tabs, Space, Indentation Setup

By Xah Lee. Date: . Last updated: .

Setting Up Indent

How to set indentation to always use space?

Put this in your emacs init.

(progn
  ;; make indentation commands use space only (never tab character)
  (setq-default indent-tabs-mode nil)
  ;; emacs 23.1 to 26, default to t
  ;; if indent-tabs-mode is t, it means it may use tab, resulting mixed space and tab
  )

How to set indentation to always use tab?

There is no easy way to do it globally.

You need to look into each mode's documentation, and see if the mode supports that feature.

A simple workaround, is just to insert/delete literal tab yourself for indentation.

You can insert a literal tab by Ctrl+q Tab or Ctrl+q Ctrl+i.

Or, you can make the Tab key always insert a literal tab.

(defun my-insert-tab-char ()
  "Insert a tab char. (ASCII 9, \t)"
  (interactive)
  (insert "\t"))

(global-set-key (kbd "TAB") 'my-insert-tab-char) ; same as Ctrl+i

How to set default tab display width?

;; set default tab char's display width to 4 spaces
(setq-default tab-width 4) ; emacs 23.1 to 26 default to 8

;; set current buffer's tab char's display width to 4 spaces
(setq tab-width 4)

How to make the Tab key always do indent or completion?

Here's the official GNU Emacs's convention for controlling what the Tab key does, globally for programing language major modes:

;; make tab key always call a indent command.
(setq-default tab-always-indent t)

;; make tab key call indent command or insert tab character, depending on cursor position
(setq-default tab-always-indent nil)

;; make tab key do indent first then completion.
(setq-default tab-always-indent 'complete)

Note:

If you really want to control what the Tab key does, just hard set that key directly to a command of your choice. The disadvantage is that completion packages such as yasnippet that by default uses tab key, may not work automatically.

here's examples:

(global-set-key (kbd "TAB") 'my-command) ; same as Ctrl+i
(global-set-key (kbd "<tab>") 'my-command)

;; • the syntax (kbd "TAB") corresponds to ASCII 9 control character, which is also equivalent to (kbd "C-i"). At the core, Emacs uses characters with control bits to represent key press.

;; • the syntax (kbd "<tab>") is the tab key, higher level. when emacs is running in GUI, it can distinguish <tab> key vs the ASCII control character ASCII 9.

;; • by default (kbd "<tab>") is translated to (kbd "TAB").

;; example of a function that just insert a tab char
(defun insert-tab-char ()
  "insert a tab char. (ASCII 9, \t)"
  (interactive)
  (insert "\t")
)

[see Emacs: How to Define Keys]

How to make Return key also do indent of previous line?

;; make return key also do indent, for current buffer only
(electric-indent-local-mode 1)

;; make return key also do indent, globally
(electric-indent-mode 1)

Indent Commands

How to insert Tab character or Newline 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]

How to indent current line?

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

How to indent a text selection of source code?

Alt+x indent-regionCtrl+Alt+\】.

More indentation commands:

How to convert tabs to space in source code?

Select a region first, then Alt+x untabify or tabify.

How to show whitespaces?

Alt+x whitespace-mode

[see Emacs: Make Whitespaces Visible]

How to delete trailing whitespaces?

Emacs: Delete Trailing Whitespace

Completion

How to do completion?

Alt+x completion-at-pointCtrl+Alt+i】. Or Alt+x complete-symbol.

Emacs Quick Start

  1. Where to Download?
  2. Emacs First Day
  3. Basic Emacs Keys
  4. Basic Tips
  5. Less-known Tips
  6. Fun with Emacs
  7. Emacs on Windows
  8. Build Emacs on Linux
  9. Build Emacs from git

Manage Windows

  1. Split Windows
  2. Restore Split Windows
  3. Make Buffer Flow Side-by-Side

File

  1. Emacs Bookmark
  2. Open Recently Opened
  3. Open File Fast
  4. Open Last Closed File
  5. Open File Path Under Cursor
  6. Copy File Path

Buffer

  1. List Buffers
  2. Switch Buffer
  3. Ido Mode
  4. New Empty Buffer
  5. Next/Previous User Buffer

Dired

  1. File Manager, dired
  2. Batch Rename Files
  3. Dired Sort File by Time, Size
  4. Rename File, Space to Hyphen/Lowline

On Current File

  1. Run Current File
  2. Delete Current File
  3. Backup Current File
  4. Convert File Line Ending
  5. Open File in External App

Find Replace

  1. Search Text in Buffer
  2. isearch Current Word
  3. Isearch Whitespace Regex
  4. Highlight Text in Buffer
  5. List Delete Highlight Sort Lines
  6. Search Text in Directory
  7. Find Replace in Buffer
  8. Find Replace in Directory
  9. xah-find.el, Find Replace in Pure Elisp
  10. Regex Tutorial

Copy/Paste

  1. Copy/Paste and kill-ring History
  2. Copy/Cut Current Line If No Selection
  3. Copy/Cut All or Selection
  4. Paste or Paste Previous
  5. Show kill-ring
  6. Copy to Register
  7. 1 Key to Copy/Paste from Register
  8. Copy Append
  9. Sync Copy/Paste Clipboards with Linux X11

Unicode

  1. Unicode Basics
  2. Unicode Tutorial
  3. Emacs File Encoding FAQ
  4. Best Unicode Fonts for Programer
  5. Elisp: Unicode Escape Sequence
  6. Xah Math Input Mode
  7. Xah Unicode Browser Mode

Font

  1. Font Setup
  2. Proportional Font
  3. Cycle Fonts by Command
  4. Set Line Spacing

Whitespace

  1. Clean Empty Lines
  2. Delete Trailing Whitespace
  3. Delete Whitespace around Cursor
  4. Tabs, Space, Indentation Setup
  5. Newline Representation ^M ^J ^L
  6. Make Whitespaces Visible

Rectangle Edit

  1. Edit Column Text, Rectangle Commands
  2. Insert Alphabets A to Z
  3. Copy Rectangle Region to kill-ring
  4. Align Text
  5. picture-mode, ASCII Diagram

Line Wrap

  1. Hard Wrap Lines
  2. Toggle Line Wrap
  3. Reformat Lines for Source Code
  4. Visual Line Mode, Wrap Lines
  5. Novel Reading Mode
  6. toggle-word-wrap

Shell

  1. Run Shell in Emacs
  2. Why Run Shell in Emacs?
  3. Difference between shell, term, eshell
  4. eshell
  5. View Linux man Page

View Things

  1. Spell Checking
  2. View Info Page
  3. View Image File
  4. View Image Thumbnails
  5. Eww Web Browser

Org Mode

  1. Org Mode Markup Cheatsheet
  2. org-mode Tutorial
  3. Source Code Markup
  4. Org Mode for Todo
  5. Org Mode Customization
  6. Elisp: Parse Org Mode

HTML

  1. Emacs HTML Tips
  2. Insert HTML Tag
  3. Add Paragraph Tags
  4. Color CSS Color Values
  5. URL to Link, Simple
  6. URL to Link, with Date
  7. Link to Dead Link
  8. Citation to Link
  9. Word to Wikipedia Link
  10. Wikipedia URL to Link
  11. Amazon URL to Link
  12. Google Map URL to Link 🌐
  13. Chinese Char to Ref Link
  14. Image Path to Link
  15. Extract URL
  16. URL Percent Decode/Encode
  17. Replace HTML Entities
  18. Lines to List
  19. Lines to Table
  20. Color Source Code
  21. update-title
  22. CSS Compressor
  23. Markup Function Names
  24. Make Ruby Annotation
  25. Move Image File
  26. Xah HTML Mode

Emacs Efficiency

  1. Keyboard Macro
  2. Completion
  3. Abbrev Mode
  4. Interactive Abbrev
  5. Use Alias for Fast M-x
  6. Jump to Previous Mark
  7. Repeat Last Prompted Command
  8. Effective Windows Management
  9. Tips on Long Term Emacs Productivity

Misc

  1. Using magit for git
  2. Emergency vi
  3. Command to Open Menu
  4. Edit Tables
  5. Emacs Chinese Input
  6. GUI vs Terminal

Emacs Customization

  1. Emacs init file
  2. What's Major Mode
  3. What's Minor Mode
  4. M-x customize
  5. Organize Init File
  6. Byte Compile Elisp
  7. What's Hook
  8. Avoid Lambda in Hook
  9. Environment Variables in Emacs
  10. Check OS, Version, Host Name
  11. Check Defined/Loaded

packages

  1. Install Packages
  2. Install Package Manually

text editing

  1. Define Keys
  2. Set Mouse Buttons
  3. Tabs, Space, Indentation Setup
  4. auto bracket pair
  5. Copy/Cut Line If No Selection
  6. Isearch space for - _
  7. Ido Completion
  8. Icomplete Completion
  9. Move Cursor by camelCase
  10. Stop Cursor Going into Minibuffer Prompt
  11. Sync Clipboard with Linux X11

file

  1. Default Major Mode
  2. Auto Backup Off
  3. Auto Save
  4. Restore Opened Files
  5. Save Cursor Position
  6. Dired Customization
  7. Open Recently Opened
  8. Open Last Closed File

appearance

  1. Show Line Numbers
  2. Show Cursor Position
  3. Visual Line Mode, Wrap Lines
  4. High Light Current Line
  5. Set Default Window Size
  6. Font Setup
  7. Line Spacing
  8. Set Color Theme
  9. Highlight Paren
  10. Show lambda as λ
  11. Color CSS Hex Code

misc

  1. Default Browser
  2. Misc Init Tips

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc