Repeating TABs on subsequent lines in text files (

2019-08-01 00:21发布

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.

标签: emacs tabs
2条回答
我想做一个坏孩纸
2楼-- · 2019-08-01 01:11

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

查看更多
Juvenile、少年°
3楼-- · 2019-08-01 01:15

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)))
查看更多
登录 后发表回答