0

In the fundamental mode, when I type the tab key, sometimes I got a tab and sometimes 4(?) whitespaces. When does which happen?

When I type the tab key, I want a tab, and when I type the whitespace key, I want a whitespace. How can I ensure that?

4
  • disable indent tabs mode. C-q TAB => TAB, TAB => whitespaces
    – shynur
    Jul 1, 2023 at 9:19
  • (keymap-global-set "C-i" 'self-insert-command) is probably enough. TAB is normally bound to indent-for-tab-command (in the global key map), which is responsible for the behavior you see. Disabling indent-tabs-mode as @shynur suggests, makes it so that no TAB characters are inserted for indentation, only spaces, and you can still insert a literal TAB character as he shows. That might be enough for your purposes, but it certainly doesn't do what you asked: "When I type the tab key, I want a tab". The keymap setting does that.
    – NickD
    Jul 1, 2023 at 13:02
  • I should probably have said that redefining the key in the global map is enough unless you enable some minor mode that redefines it differently. See Active keymaps in the Elisp manual for more info.
    – NickD
    Jul 1, 2023 at 13:29
  • What do you mean by "the whitespace key"?
    – phils
    Jul 13, 2023 at 5:32

1 Answer 1

1

Set both indent-tabs-mode and indent-line-function.

(setq indent-tabs-mode t 
      indent-line-function 'indent-to-left-margin)

Note that indent-tabs-mode is a buffer-local variable and indent-line-function is not.

You can see how makefile-mode (in make-mode.el) do it to ensure that hitting TAB actually inserts tabs in Makefile.

EDIT: you can also use (indent-tabs-mode) instead of (setq indent-tabs-mode t). Note that using setq way might not trigger indent-tabs-mode-hook.

9
  • Does (setq *-mode t) make sense? IIRC, you need to call *-mode manually.
    – shynur
    Jul 11, 2023 at 18:22
  • (define-minor-mode indent-tabs-mode "Toggle whether indentation can insert TAB characters." :global t :group 'indent :variable indent-tabs-mode) we can do that on indent-tabs-mode because of :variable indent-tabs-mode
    – yufu
    Jul 11, 2023 at 22:04
  • I don't think that’s the reason. Almost every mode declares such a variable implicitly. This minor mode variable is defined in C code, so perhaps it is a pseudo mode. Anyway, it is better to call to enable it, IMO.
    – shynur
    Jul 12, 2023 at 18:47
  • Most mode does not have such variable, only the function. Yeah, I think that's a special case here. Because there are no codes run when we enable/disable indent-tabs-mode, so it's equivalent as setting the variable directly. Well, I actually do not think too much before you brought up haha. I just replicate what makefile-mode does.
    – yufu
    Jul 12, 2023 at 22:24
  • indent-tabs-mode-hook might not be run though.
    – yufu
    Jul 12, 2023 at 22:30

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.