5

How can I control the indentation of a line in a plain text file?

In fundamental-mode, when you begin to type a line (call it Line A) after a previous line by hitting return, the line will automatically inherit the indent of the previous line.

When I manually delete the automated indent, and want 3 tabs as indent instead by hitting tab three times, it seems that the second tab is converted to spaces, because I saw the cursor didn't jump by tab but by a character when I hit the second tab.

When I create another line (call it Line B) and remove any automated indent of it, if I hit just one tab key, it will jump to the same indent as that of Line A, and Line A has an indent more than just one tab.

1
  • Indentation in Emacs is meant to add as many space as needed to conform to the document requirements. It's the "requirement" of Fundamental mode that indent would be computed based on the current indent and the length of the word to the left on the line above it. It seems like what you wanted is simply to insert a TAB character, in which case you could type C-q TAB. I'm not sure if Emacs has a special mode for this kind of indenting, but there's a good chance it has, giving Google a spin might be worthwhile.
    – wvxvw
    Jun 18, 2015 at 7:56

1 Answer 1

3

First of all, are you really editing in fundamental-mode? If you're talking about a plain text file, are you sure you're not in text-mode? You can check with M-: major-mode.

In general, have a look at the manual node on "Indentation".

tab runs indent-for-tab-command in fundamental-mode. The first part of its docstring reads:

Indent the current line or region, or insert a tab, as appropriate. This function either inserts a tab, or indents the current line, or performs symbol completion, depending on tab-always-indent.

You can control the tab stops via tab-stop-list:

List of tab stop positions used by tab-to-tab-stop. This should be a list of integers, ordered from smallest to largest.

If you'd prefer all of your tabs to be converted to spaces, you can turn off indent-tabs-mode with (setq-default indent-tabs-mode nil).

If you'd like to prevent enter from auto-indenting, have a look at the threads for How to disable auto-indentation of new lines? and Emacs version 24.4: New obnoxious loss of indentation on hitting RETURN. Short version: either turn off electric-indent-mode or bind enter to newline rather than newline-and-indent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.