简体   繁体   中英

Emacs: Insert tab instead of spaces

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM