【问题标题】:Emacs: Insert tab instead of spacesEmacs:插入制表符而不是空格
【发布时间】:2011-07-06 00:29:35
【问题描述】:

出于多种原因,我更喜欢将编辑器配置为在按下 TAB 时插入空格。

但最近我发现制表符应该在 make 文件中保留为制表符。

如何在每次需要编写 make 文件时插入标签(\t,而不是 " ")而不重新配置编辑器?

我使用以下编辑器: EmacsKategeditVisual Studio 编辑器。

【问题讨论】:

    标签: emacs makefile text-editor


    【解决方案1】:

    要在 Emacs 中手动插入选项卡,请使用 ctrl-Q TAB。 control-Q 导致插入下一个键而不是解释为可能的命令。

    【讨论】:

    • 谢谢,这一直困扰着我一段时间。
    • 谢谢。我相信将来我将不得不再次谷歌:D
    【解决方案2】:

    只要您在正确的位置按下正确的键,Emacs 的 Makefile 模式就会负责在哪里插入制表符和空格。要么,要么我错过了问题中的一些细节。

    【讨论】:

    • 是的,对于 emacs,如果您处于 makefile 模式(或 BSDmakefile 模式),则插入制表符而不是空格应该是自动的。如果由于配置混乱而无法插入选项卡,请使用“C-q选项卡”。
    【解决方案3】:

    EmacsWiki 上 NoTabs 页面的 Smart inference of indentation style 部分非常有帮助。它向您展示了如何为大多数项目设置空格,但如果您正在编辑的文件中以 tab 开头的行多于以空格开头的行,则切换到 tab。

    代码如下:

    (defun infer-indentation-style ()
      ;; if our source file uses tabs, we use tabs, if spaces spaces, and if        
      ;; neither, we use the current indent-tabs-mode                               
      (let ((space-count (how-many "^  " (point-min) (point-max)))
            (tab-count (how-many "^\t" (point-min) (point-max))))
        (if (> space-count tab-count) (setq indent-tabs-mode nil))
        (if (> tab-count space-count) (setq indent-tabs-mode t))))
    

    [在我的 c-mode 钩子中,或者我想要智能缩进的任何其他模式]

    (setq indent-tabs-mode nil)
    (infer-indentation-style)
    

    在编辑应始终具有诸如 makefile 之类的选项卡的新文件时,这仍然可能是一个问题。对于那些,您的模式挂钩只会将其设置为选项卡。例如:

    (add-hook 'makefile-mode-hook 
      '(lambda() 
         (setq indent-tabs-mode t)
       )
    )
    

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 1970-01-01
      • 2011-05-27
      • 2013-09-29
      • 2014-03-14
      • 1970-01-01
      • 2010-11-08
      • 2011-03-28
      相关资源
      最近更新 更多