问题
For several reasons I prefer to configure my editor to insert spaces when TAB is pressed.
But recently I discovered that tabs should remain as tabs in make files.
How do I insert tab (\t
, not " "
) without reconfiguring editors each time I need to write make files?
I use the following editors: Emacs, Kate, gedit, and the Visual Studio editor.
回答1:
Emacs' Makefile mode takes care of where to insert tabs and spaces as long as you press the right keys at the right places. Either that, or I missed some details in the question.
回答2:
To manually insert a tab in Emacs, use ctrl-Q TAB. control-Q causes the next key to be inserted rather than interpreted as a possible command.
回答3:
The Smart inference of indentation style section of the NoTabs page on EmacsWiki was very helpful. It shows you how to set up spaces for most projects, but switch to tabs if the file you're editing has more lines starting with tab than lines starting with space.
Here's the code:
(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))))
[in my c-mode hook, or whatever other mode I want to have smart indentation]
(setq indent-tabs-mode nil)
(infer-indentation-style)
This could still be an issue when editing new files that should always have tabs like makefiles. For those, your mode hook would just set it to tabs. For example:
(add-hook 'makefile-mode-hook
'(lambda()
(setq indent-tabs-mode t)
)
)
来源:https://stackoverflow.com/questions/5146621/emacs-insert-tab-instead-of-spaces