Emacs Init: Tab, Indent
Set Default Tab Display Width
Put this in your Emacs Init File:
;; set default tab char's display width to 4 spaces ;; default is 8 (setq-default tab-width 4)
Set Indent Commands to Always Use Space Only
(progn ;; make indent commands use space only (never tab character) (setq-default indent-tabs-mode nil) ;; default is t ;; t means it may use tab, resulting mixed space and tab )
Set Indent Commands to Always Use Tab Characters Only
There is no easy way to do it globally.
A simple workaround, is just to insert/delete literal tab char yourself for indentation.
You can insert a literal tab by Ctrl+q Tab .
Make Tab Key Do Indent or Completion
Here's the official GNU Emacs's convention for controlling what the Tab key does, globally for programing language major modes:
;; make tab key always call a indent command. (setq-default tab-always-indent t) ;; make tab key call indent command or insert tab character, depending on cursor position (setq-default tab-always-indent nil) ;; make tab key do indent first then completion. (setq-default tab-always-indent 'complete)
- Major Mode may not respect the value of tab-always-indent
- Major modes may have its own idea of indentation or system to control whether indentation insert tab char or space.
- Major modes may have its own idea of controlling whether the Tab key should do indentation or completion.
If you really want to control what the Tab key does, just hard set that key directly to a command of your choice. The disadvantage is that completion packages such as yasnippet that by default uses Tab key, may not work automatically.
;; example of a function that just insert a tab char (defun my-insert-tab-char () "insert a tab char. (ASCII 9, \t)" (interactive) (insert "\t")) (global-set-key (kbd "TAB") 'my-insert-tab-char)
To make sure that major mode does not override your key, see Emacs: Change Major Mode Keybinding
Make Return Key Also Do Indent
put this in your Emacs Init File:
;; make return key also do indent, globally (electric-indent-mode 1)