1

Greetings

lately I've been trying to learn Emacs and configure it as an IDE, so I started learning the basics, until I got to the segment of configuring indentations.

I do understand that the term Tab stop, means, well, where tabs stop. So if we are able to set stop points for a tab, then why do we need tab widths?

I would be glad if a professional would be able to point me in the right direction, because I cannot understand the difference between the terms tab stop and tab width.

1 Answer 1

1

We have both because a text editor is not a typewriter, basically.

The tab-width tells Emacs how to display a tab character in the buffer. A tab width of 8 causes Emacs to render a tab character the equivalent width as up to 8 spaces. For a line which started with a tab, it would have the appearance of 8 spaces. For a line which started with 6 spaces and then a tab, the tab would have the visual appearance of 2 spaces. The important thing is that a tab character is a specific kind of textual data in a digital format.

For "tab stops", you want to think of a physical analogue typewriter -- you hit the tab key and the carriage moves to the next tab stop position. There's no concept of a tab character here because it's just a distance across a sheet of paper. Tab stops in Emacs are analogous -- they just say where to move the cursor to, and they have no bearing on which digital characters are used to get it there -- you might need many space and/or tab characters to reach the next tab stop position. Your tab-stop-list might be implicit (multiples of tab-width) or it might be an explicit list of arbitrary positions.

If indent-tabs-mode is non-nil, then moving to a tab stop may involve a mixture of tab characters and space characters, with the tabs generally occupying the same amount of space as tab-width spaces would.

Finally, what the TAB key on your keyboard does is another matter again. It could do anything, but typically it's configured to perform indentation in some way, and that might cause it to convert space characters to tab characters.


Summary:

  • A tab stop is just a column position (to which certain commands will move the cursor).
  • A tab character (ascii character code 9) is something that might appear in your text buffer.
  • The tab-width variable tells Emacs how to display a tab character.
  • In the absence of an explicit tab-stop-list, the tab-width variable also determines the implicit tab stops.
  • Indenting is complicated, and what happens when you type the TAB key can vary greatly in different circumstances.

Further reading:

  • C-hig (emacs)Tab Stops

    Note that the tab stops discussed in this section have nothing to do with how tab characters are displayed in the buffer. Tab characters are always displayed as empty spaces extending to the next “display tab stop”. *Note Text Display::.

  • C-hig (emacs)Text Display

    The ASCII character set contains non-printing “control characters”. Two of these are displayed specially: the newline character (Unicode code point U+000A) is displayed by starting a new line, while the tab character (U+0009) is displayed as a space that extends to the next tab stop column (normally every 8 columns). The number of spaces per tab is controlled by the buffer-local variable ‘tab-width’, which must have an integer value between 1 and 1000, inclusive. Note that the way the tab character in the buffer is displayed has nothing to do with the definition of <TAB> as a command.

  • C-hig (emacs)Indentation

    The simplest way to perform indentation is the <TAB> key. In most major modes, this runs the command ‘indent-for-tab-command’. (In C and related modes, <TAB> runs the command ‘c-indent-line-or-region’, which behaves similarly, *note C Indent::).

    <TAB> Insert whitespace, or indent the current line, in a mode-appropriate way (‘indent-for-tab-command’). If the region is active, indent all the lines within it.

    The exact behavior of <TAB> depends on the major mode. In Text mode and related major modes, <TAB> normally inserts some combination of space and tab characters to advance point to the next tab stop (*note Tab Stops::). For this purpose, the position of the first non-whitespace character on the preceding line is treated as an additional tab stop, so you can use <TAB> to align point with the preceding line. If the region is active (*note Using Region::), <TAB> acts specially: it indents each line in the region so that its first non-whitespace character is aligned with the preceding line.

    In programming modes, <TAB> indents the current line of code in a way that makes sense given the code in the preceding lines. If the region is active, all the lines in the region are indented this way. If point was initially within the current line’s indentation, it is repositioned to the first non-whitespace character on the line.

    If you just want to insert a tab character in the buffer, type ‘C-q <TAB>’ (*note Inserting Text::).

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.