Repeating TABs on subsequent lines in text files (but keeping TABs disabled for code)

﹥>﹥吖頭↗ 提交于 2019-12-24 08:47:00

问题


I am editing a text file foo.txt using emacs.

I press C-q TAB to insert a TAB character at the beginning of a line and then follow with a few characters.

Once I press ENTER, emacs inserts eight spaces on the following line.

How do I specify in my .emacs that I would like TABs to be repeated on subsequent lines with TABs?

Importantly, I dislike TAB characters in program code, and so I have (setq-default indent-tabs-mode nil) to make sure that TABs are inserted only when I explicitly ask for them.


回答1:


Emacs inserts SPC chars because you told it to, by setting indent-tabs-mode to nil (my preference too, BTW).

If you want Emacs to indent using TAB chars in a particular mode (buffer), but you want it to use SPC chars in general (i.e., in other modes), then set indent-tabs-mode to t in those modes where you want TABs. Just use setq when you are in the mode, since it is a buffer-local variable. For example:

 (add-hook MY-mode-hook (lambda () (setq indent-tabs-mode t)))



回答2:


The real answer is: No, there is no way to do this by some simple configuration setting in emacs. indent-tabs-mode is either on or off, and indentation will behave according to that.

But, just because this feature is not there, doesn't mean you can't add it!

This is actually not a simple problem from what I found. Whether or not to use tabs or spaces is determined by indent-tabs-mode in C mostly. Assuming that you are running a recent version of emacs, the auto indentation is coming from electric-indent-mode which uses indent-according-to-mode in a post-self-insert-hook to do the indentation.

What I did for this was define a buffer local minor mode, when this mode is active indent-tabs-mode will be temporarily set depending on the first character in the last line while running indent-according-to-mode.

So when smart-electric-indent-tabs-mode is active, and your last line started with the tab, the next line will indent with a tab too, else it will just use whatever indent-tabs-mode would normally be set to.

You could add the following to your config to activate it. The add-hook clause is put in there for your convenience, you can activate it on the fly like a normal minor mode if you'd like.

(define-minor-mode smart-electric-indent-tabs-mode
  "When on, indenting will use tabs if the current line does,
    else it will indent according to `indent-tabs-mode'."
  :init-value nil
  :lighter " smart-tabs"
  :keymap nil
  :global nil)

 (defadvice indent-according-to-mode (around maybe-use-tabs activate)
  "Follow `smart-electric-indent-tabs-mode'."
  (let ((indent-tabs-mode
         (or (and smart-electric-indent-tabs-mode
                  (save-excursion
                    (save-restriction
                      (widen)
                      (beginning-of-line 0)
                      (looking-at "\t"))))
             indent-tabs-mode)))
    ad-do-it))

;; if you want, add a text mode hook
(add-hook 'text-mode-hook 'smart-electric-indent-tabs-mode)

This has only been tested to work during electric indentation



来源:https://stackoverflow.com/questions/25021526/repeating-tabs-on-subsequent-lines-in-text-files-but-keeping-tabs-disabled-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!