【发布时间】:2011-07-06 00:29:35
【问题描述】:
出于多种原因,我更喜欢将编辑器配置为在按下 TAB 时插入空格。
但最近我发现制表符应该在 make 文件中保留为制表符。
如何在每次需要编写 make 文件时插入标签(\t,而不是 " ")而不重新配置编辑器?
我使用以下编辑器: Emacs、Kate、gedit 和 Visual Studio 编辑器。
【问题讨论】:
标签: emacs makefile text-editor
出于多种原因,我更喜欢将编辑器配置为在按下 TAB 时插入空格。
但最近我发现制表符应该在 make 文件中保留为制表符。
如何在每次需要编写 make 文件时插入标签(\t,而不是 " ")而不重新配置编辑器?
我使用以下编辑器: Emacs、Kate、gedit 和 Visual Studio 编辑器。
【问题讨论】:
标签: emacs makefile text-editor
要在 Emacs 中手动插入选项卡,请使用 ctrl-Q TAB。 control-Q 导致插入下一个键而不是解释为可能的命令。
【讨论】:
只要您在正确的位置按下正确的键,Emacs 的 Makefile 模式就会负责在哪里插入制表符和空格。要么,要么我错过了问题中的一些细节。
【讨论】:
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)
)
)
【讨论】: