Emacs: Tab/Space/Indent Setup
Set Default Tab Display Width
;; set default tab char's display width to 4 spaces (setq-default tab-width 4) ; emacs 23.1 to 26 default to 8 ;; set current buffer's tab char's display width to 4 spaces (setq tab-width 4)
Set Indent Commands to Always Use Space Only
Put this in your emacs init file:
(progn ;; make indent commands use space only (never tab character) (setq-default indent-tabs-mode nil) ;; emacs 23.1 to 26, default to t ;; if indent-tabs-mode is t, it 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.
You need to look into each mode's documentation, and see if the mode supports that feature.
A simple workaround, is just to insert/delete literal tab yourself for indentation.
You can insert a literal tab by Ctrl+q Tab .
Or, you can make the Tab key to always insert a literal tab.
(defun my-insert-tab-char () "Insert a tab char. (ASCII 9, \t)" (interactive) (insert "\t")) (global-set-key (kbd "TAB") 'my-insert-tab-char)
But major mode may override your key. You can force your Tab keybinding by using a hook for a major mode. [see Emacs: Change Major Mode Keys]
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)
- To set globally, use
setq-default(but major modes may override it.)
- To set for current buffer only, use
- To set for specific major mode only, use
setqin a hook. [see Emacs: What's Hook?]
- Major mode may not respect these settings (either because it has its own design, or because of bad quality). (most or all bundled GNU Emacs major modes for programing languages do conform.)
- 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) (global-set-key (kbd "<tab>") 'my-insert-tab-char) ;; • the syntax (kbd "TAB") corresponds to ASCII 9 control character, which is also equivalent to (kbd "C-i"). At the core, Emacs uses characters with control bits to represent key press. ;; • the syntax (kbd "<tab>") is the tab key, higher level. when emacs is running in GUI, it can distinguish <tab> key vs the ASCII control character ASCII 9. ;; • by default (kbd "<tab>") is translated to (kbd "TAB").
To make sure that major mode does not override your key, see Emacs: Change Major Mode Keys
[see Emacs: How to Define Keys]
Make Return Key Also Do Indent of Previous Line
Put this in your emacs init file:
;; make return key also do indent, for current buffer only (electric-indent-local-mode 1) ;; make return key also do indent, globally (electric-indent-mode 1)
Emacs: Indentation/Completion Commands