2

I am confused by space (SPC) and TAB characters in Emacs.

I am often using the default fundamental mode.

When I select a region and indent the lines in the region by C-u 9 C-x TAB, I intend to indent the lines by 9 SPC characters.

But after I perform the operation, when I place my cursor at the beginning at such a line, and type right arrow key, it jumps to the 8th position with the 9th position also blank, as if there were a TAB character followed by a SPC character. This is contrary to my intention to indent the lines by 9 SPC characters.

I was wondering how to understand the uses and display of SPC and TAB characters in Emacs? Is there some general principles that I can follow to understand?

Btw, I may have asked similar question about the confusion between SPC and TAB characters in Emacs before, either here or Unix.SE or SO. But I can't find it.

7
  • 2
    There is no standard way to display a tab character. By default, Emacs uses both tab and space characters for indentation. You can choose to use only space characters by setting the default or buffer-local value of indent-tabs-mode to nil.
    – xuchunyang
    Nov 5, 2018 at 21:54
  • Mixed spaces and tabs are one of a number of poor legacy defaults in Emacs. You would think that this would have been changed at some point, but it seems that it has not been :-(
    – Qudit
    Nov 5, 2018 at 22:48
  • Why, out of curiosity, are you using fundamental mode?
    – Dan
    Nov 6, 2018 at 12:48
  • @Dan No particular reason. Not familiar with many other modes. What do you recommend?
    – Tim
    Nov 6, 2018 at 12:49
  • @Tim, two small observations: a) take into account in Emacs characters in line are counted starting from 0, not 1. b) do you need maybe a nice writing environment like olivetti github.com/rnkn/olivetti?
    – Ian
    Nov 6, 2018 at 13:50

2 Answers 2

7

Like most things in emacs, the tabs policy is configurable. It sounds to me that you'd like to follow the most common apporach and use the tab key as an indentation tool and not have tab characters in your files at all.

First, you should set (setq-default indent-tabs-mode nil) early on in your emacs config. To convert existing files, emacs comes with functions tabify and untabify. See the No Tabs wiki page for more.

Secondly, the exact tab behaviour depends on emacs modes. Major modes usually have defaults and ways to change the indentation behviour. You really should use a mode tailored for the syntax you are writing. If such one does not exist yet, setting the tab policy is the way to start one. The fundamental mode is very basic by design and should be left as it is.

5

To make tabs and spaces visible, you can enable whitespace-mode with M-x whitespace-mode RET or M-x global-whitespace-mode RET. Have a look at variable whitespace-style (e.g. M-x customize RET whitespace-style RET).

As you found, indent-rigidly (which you're running with C-x TAB) indents with both tabs and spaces. It will use tabs as much as possible and then make up the rest of the needed indentation with spaces. If you want to prevent use of tabs you can set indent-tabs-mode nil (e.g. with M-x set-variable RET indent-tabs-mode RET nil RET, or see e.g. here for how to set it in your emacs configuration).

2
  • 1
    indent-tabs-mode is buffer-local, so maybe show how to set the default value as well.
    – phils
    Nov 6, 2018 at 0:06
  • 1
    @phils done by referring to another question Nov 6, 2018 at 22:53

Your Answer

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

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