在文本文件中的后续行上重复TAB(但是为代码禁用TAB)

时间:2014-07-29 17:33:25

标签: emacs tabs

我正在使用emacs编辑文本文件foo.txt

我按 C-q TAB 在一行的开头插入一个 TAB 字符,然后跟几个字符。

按Enter后,emacs会在以下行中插入八个空格。

如何在我的.emacs中指定我希望TAB在后续的TAB行中重复?

重要,我不喜欢程序代码中的TAB字符,所以我(setq-default indent-tabs-mode nil)确保仅在明确要求时才插入TAB。

2 个答案:

答案 0 :(得分:1)

Emacs通过将SPC设置为indent-tabs-mode(我的偏好也是BTW)来插入nil个字符,因为您告诉了它。

如果您希望Emacs在特定模式(缓冲区)中使用TAB字符缩进,但您希望它一般使用SPC字符(即,在其他模式下),则设置{{在您想要indent-tabs-mode s的那些模式中,1}}到t。当您处于模式时,只需使用TAB,因为它是缓冲区局部变量。例如:

setq

答案 1 :(得分:1)

真正的答案是:不,没有办法通过emacs中的一些简单配置设置来实现这一点。 indent-tabs-mode打开或关闭,缩进将根据该行为。

但是,仅仅因为此功能不存在,并不意味着您无法添加它!

这实际上不是我发现的一个简单的问题。是否使用制表符或空格主要取决于C中的indent-tabs-mode。假设您运行的是最新版本的emacs,则自动缩进来自electric-indent-mode,它使用indent-according-to-mode中的post-self-insert-hook进行缩进。

我为此做的是定义缓冲区本地次要模式,当此模式处于活动状态时indent-tabs-mode将根据运行indent-according-to-mode时最后一行中的第一个字符进行临时设置。

因此,当smart-electric-indent-tabs-mode处于活动状态,并且您的最后一行开始使用该标签时,下一行也会使用标签缩进,否则它只会使用通常设置为indent-tabs-mode的任何内容。< / p>

您可以将以下内容添加到配置中以激活它。为了您的方便,add-hook子句放在那里,如果您愿意,可以像普通的次要模式一样快速激活它。

(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)

这仅在电动压痕期间进行了测试