This page describes the extension ‘Clean Auto Indent’:

Clean Indent

When automatic indentation is enabled (RET is bound to ‘newline-and-indent’), upon inserting a new line Emacs will put the cursor under the first non-blank character of the previous line. Because it is not designed to handle virtual spaces, Emacs will do that by inserting the necessary amount of white-space characters (tabs or spaces) so that the cursor can be positioned at the desired location. The problem is that if nothing is typed at that line, the block of spaces are left hanging forever.

For example, with auto-indentation enabled attempt to insert one blank line between the two printf() lines. On the screenshot the cursor is at the end of the first line.


The natural inclination is to simply press RET.


New line is inserted. Now walk away by pressing down arrow.


The trailing white-space area is highlighted in red.

There are solutions already built in Emacs. These spaces can be removed by invoking M-x ‘delete-trailing-whitespace’, or even making it automatic by attaching to ‘before-save-hook’.

The problem is that in such configuration all white spaces, including those from lines you didn't edit, will be deleted. This is inadmissible in an environment of collaborative development where one central version control repository is shared. Your commits will have tons of unrelated changes.

`clean-aindent.el’ is a small extension that keeps track of the last auto-indent operation and, if it is abandoned, would take care to trim down the abandoned white space characters.

It will look like this after it will look like this after you move to the next line:


Simple Indent

Simple indent, if activated, will bypass the default language dependent indentation of ‘newline-and-indent’.

For example, look at this fragment of Javascript code. It is a typical technique of establishing namespaces by anonymous function at the top level. The desire, after that, is to have all functions nested in this namespace to start from column 1. But indentation won't work by default. Here cursor is at the end of the line.


You press RET (assuming it is bound to ‘newline-and-indent’). Based on the indentation configuration for Javascript, counting the number of nested curly braces, your cursor ends up indented at this position.


But this is not what you wanted, you wanted to simply start at position 1. Here you have two choices, the complicated one is to figure out how to configure the language indentation for Javascript, and the simple one is to activate simple indentation. The result after pressing RET will be this.


With simple indentation activated, the cursor is always positioned at the indentation of the previous non-blank line. After that, if needed, you can indent (via space or TAB) or unindent (via M-Backspace) in order to position based on the logic of the code you intend to enter.


Unindent is a function that quickly navigates the indentation of the previous non-blank lines.

For example, this is your initial cursor position.


You press RET (assuming it is bound to ‘newline-and-indent’, and simple indent activated), your cursor ends up at this indent position.


But you are done with this “progn” sequence, you intend to enter code at the nest level of “if”, you can press Backspace 4 times or use ‘clean-aindent--bsunindent’, which is bound to M-Backspace. The cursor jumps to the indentation preceding the current position based on the indentation of the previous non-blank lines.


Backspace Unindent will work not only on blank lines but, also, if invoked while cursor is at the indentation space of a line or at the first non-blank character of the line. It will have the same effect, the entire line will be unindented based on the previous non-blank lines.


‘clean-aindent-mode’ is an extenstion that offers simple indentation (if configured so): no language mode magic, cursor is simply aligned under the previous non-blank line. It works in concert with the other feature, backspace unindent. If you press M-DEL, the cursor or the line is unindented, again looking at previous non-blank lines. And the third one, in its most minimal installation ‘clean-aindent-mode’ will take care to strip unused blank space left by ‘newline-and-indent’.

The combination of these 3 small features intends to implement a simple, but to me, powerful, mode of dealing with indentation. It used to exist in old Borland editors (Turbo C/Pascal).

The installation and configuration instructions are inside README.adoc.


CategoryIndentation, AutoIndentation