I can't find this: How do I use 4 SPACES instead of a TAB in EMACS?

后端 未结 5 822
广开言路
广开言路 2021-01-05 21:40

I am making the jump to EMACS, and I can\'t find what I need to do in my .emacs file to get php-mode AND all other modes to insert 4 spaces instead of

相关标签:
5条回答
  • 2021-01-05 22:03

    You're missing a (setq c-basic-indent 4). So you should have:

    ;; 4 spaces rather than tabs
    (setq-default indent-tabs-mode nil)
    (setq-default tab-width 4)
    (setq c-basic-offset 4)
    (setq c-basic-indent 4)
    
    0 讨论(0)
  • 2021-01-05 22:06

    You can use M-x untabify to convert all tabs to spaces.

    Well in that case you can set the value of indent-tabs-mode to nil for your php minor mode.

    You may also find the wiki: http://www.fnal.gov/docs/products/emacs/emacs/emacs_23.html#SEC185 and Jamie Zawinski's post: http://www.jwz.org/doc/tabs-vs-spaces.html to be informative.

    0 讨论(0)
  • 2021-01-05 22:08

    Change the variable indent-tabs-mode to nil. You can do it interactively (for just one buffer) by M-x set-variable. To make it permanent (and for all buffers), put

     (setq-default indent-tabs-mode nil)
    

    in your init file.

    To make a tab do just 4 spaces in most modes, also add

     (setq-default tab-width 4)
    

    For C based modes (like PHP) you will have to do:

    (setq c-basic-offset 4)
    
    0 讨论(0)
  • 2021-01-05 22:11

    This is what I've done.

    ;;;; Tab settings ;;;;
    ;Tab width is 3
    (setq tab-width 3)
    (setq-default tab-width 3) ;;going to force it. yessir.
    ;Use spaces always.
    (setq-default indent-tabs-mode nil)
    ;Jump by 3.
    (setq c-basic-offset 3)
    ;this defaulted to 4 and had to be reset to 3. the prior settings did not override it. Lame.
    (setq perl-indent-level 3)
    ;Tab stop list out to col 60
    ;Manually set by x3
    (setq tab-stop-list '(3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60))
    
    0 讨论(0)
  • 2021-01-05 22:20

    The default behavior of TAB in Emacs (in programming modes) is to automatically indent to the right place, where this place is decided based on indentation rules described by the major mode (it's one of the most important job of a major-mode, actually, much more demanding in general than color-highlighting). Of course if the line is already indented to the right place, hitting TAB won't do anything.

    So you can force TAB to behave in a dumb way, like you're asking, but then you'd miss out on most of the fun. E.g. you could do something like

    (global-set-key "\t"
      (lambda ()
        (interactive)
        (let ((prevline-indent (save-excursion (forward-line -1) (current-indentation))))
          (if (< (current-column) prevline-indent))
              (indent-to prevline-indent)
            (insert "    ")))))
    

    But instead, I recommend you use TAB the way Emacs intended and explain to Emacs how you like your code to be indented. Probably by setting something like:

    (setq-default indent-tabs-mode nil) ;; Prefer SPC over TAB when indenting.
    (setq c-basic-offset 4)             ;; I like indenting by 4 spaces.
    

    From what you say, the TAB indentation in your php-mode doesn't work right, so there might simply be a bug in your php-mode. Tell us exactly in which case it is not working right, showing the text that gets mis-indented, and explaining in which way the indentation does not match your expectations.

    0 讨论(0)
提交回复
热议问题