Set 4 Space Indent in Emacs in Text Mode

后端 未结 20 2287
梦谈多话
梦谈多话 2020-11-29 15:27

I\'ve been unsuccessful in getting Emacs to switch from 8 space tabs to 4 space tabs when pressing the TAB in buffers with the major mode text-mode.

相关标签:
20条回答
  • 2020-11-29 15:47
    (setq-default tab-width 4)
    (setq-default indent-tabs-mode nil)
    
    0 讨论(0)
  • 2020-11-29 15:48
    (defun my-custom-settings-fn ()
      (setq indent-tabs-mode t)
      (setq tab-stop-list (number-sequence 2 200 2))
      (setq tab-width 2)
      (setq indent-line-function 'insert-tab))
    
    (add-hook 'text-mode-hook 'my-custom-settings-fn)
    
    0 讨论(0)
  • 2020-11-29 15:50
    (setq-default indent-tabs-mode nil)
    (setq-default tab-width 4)
    (setq indent-line-function 'insert-tab)
    (setq c-default-style "linux") 
    (setq c-basic-offset 4) 
    (c-set-offset 'comment-intro 0)
    

    this works for C++ code and the comment inside too

    0 讨论(0)
  • 2020-11-29 15:50

    Customizations can shadow (setq tab width 4) so either use setq-default or let Customize know what you're doing. I also had issues similar to the OP and fixed it with this alone, did not need to adjust tab-stop-list or any insert functions:

    (custom-set-variables
     '(tab-width 4 't)
     )
    

    Found it useful to add this immediately after (a tip from emacsWiki):

    (defvaralias 'c-basic-offset 'tab-width)
    (defvaralias 'cperl-indent-level 'tab-width)
    
    0 讨论(0)
  • 2020-11-29 15:51

    You can add these lines of code to your .emacs file. It adds a hook for text mode to use insert-tab instead of indent-relative.

    (custom-set-variables
     '(indent-line-function 'insert-tab)
     '(indent-tabs-mode t)
     '(tab-width 4))
    (add-hook 'text-mode-hook
          (lambda() (setq indent-line-function 'insert-tab)))
    

    I hope it helps.

    0 讨论(0)
  • 2020-11-29 15:53

    Update: Since Emacs 24.4:

    tab-stop-list is now implicitly extended to infinity. Its default value is changed to nil which means a tab stop every tab-width columns.

    which means that there's no longer any need to be setting tab-stop-list in the way shown below, as you can keep it set to nil.

    Original answer follows...


    It always pains me slightly seeing things like (setq tab-stop-list 4 8 12 ................) when the number-sequence function is sitting there waiting to be used.

    (setq tab-stop-list (number-sequence 4 200 4))
    

    or

    (defun my-generate-tab-stops (&optional width max)
      "Return a sequence suitable for `tab-stop-list'."
      (let* ((max-column (or max 200))
             (tab-width (or width tab-width))
             (count (/ max-column tab-width)))
        (number-sequence tab-width (* tab-width count) tab-width)))
    
    (setq tab-width 4)
    (setq tab-stop-list (my-generate-tab-stops))
    
    0 讨论(0)
提交回复
热议问题