ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

Emacs: Tabs, Space, Indentation Setup, Delete Trailing Whitespace

Xah Lee,

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-regionCtrl+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 NameArea of ActionAction
delete-trailing-whitespacebufferdelete all trailing whitespaces
whitespace-cleanuptext selectiondelete whitespace in a smart way
delete-whitespace-rectangleselection as rectangledelete 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

blog comments powered by Disqus