This page tells you how to setup tabs and indentation for emacs, and useful commands to convert or control indentation.
How to stop mixed tabs and space?
Put this in your emacs init.
(setq-default indent-tabs-mode nil) ; emacs 23.1, 24.2, default to t
How to set default tab width?
(setq tab-width 4) ; emacs 23.1, 24.2, default to 8
How to convert tabs to space in source code?
Select a region first, then call untabify or tabify.
How to indent a region of source code?
Call indent-region 【Ctrl+Alt+\】.
How to insert Tab or Newline char?
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.
For detail, see: Emacs: Newline Representations ^M ^J ^L.
How to see whitespaces?
Call whitespace-mode 〔☛ How to Use and Setup Emacs's whitespace-mode〕
How to delete trailing whitespaces?
| Command Name | Area of Action | Action |
|---|---|---|
delete-trailing-whitespace | buffer | delete all trailing whitespaces |
whitespace-cleanup | text selection | delete whitespace in a smart way |
delete-whitespace-rectangle | selection as rectangle | delete whitespace in text selection start/end points as a rectangle |
For more fine control of deleting whitespaces, you can use use query-replace, or query-replace-regexp. 〔☛ Emacs: Find/Replace Tutorial〕