Emacs: Tabs, Space, Indentation Setup
This page tells you how to:
- Setup indentation to use tab character.
- Setup indentation to use space character.
- Setup Tab ⇥ key to insert a literal tab char.
- Setup Tab ⇥ key to do indentation or completion.
- Convert a source code file's indentation from tab to space or vice versa.
Setting Up Indent
How to set indentation to always use space?
Put this in your emacs init.
;; make indentation commands use space only (never tab character) (setq-default indent-tabs-mode nil) ; emacs 23.1, 24.2, default to t ;; if indent-tabs-mode is t, it means it may use tab, resulting mixed space and tab
How to set indentation to always use tab?
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 Ctrl+q Ctrl+i.
Or, you can make the Tab ⇥ key 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) ; same as Ctrl+i
How to set default tab display width?
;; set default tab char's display width to 4 spaces (setq-default tab-width 4) ; emacs 23.1, 24.2, default to 8 ;; set current buffer's tab char's display width to 4 spaces (setq tab-width 4)
How to make the Tab ⇥ key always 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: Change Major Mode Keys
- 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.
(global-set-key (kbd "TAB") 'my-command) ; same as Ctrl+i (global-set-key (kbd "<tab>") 'my-command) ;; • 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"). ;; example of a function that just insert a tab char (defun insert-tab-char () "insert a tab char. (ASCII 9, \t)" (interactive) (insert "\t") )
〔►see Emacs: How to Define Keys〕
How to make Return ↩ key also do indent of previous line?
;; 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)
How to insert Tab character or Newline character?
The following method works everywhere (also works in minibuffer).
To insert a literal tab char, press 【Ctrl+q Tab ⇥】.
To insert a newline char, type 【Ctrl+q Ctrl+j】.
You need to use the above method to insert these characters, because for example in minibuffer, pressing Tab ⇥ does name completion and pressing Enter ↵ finishes the prompt. In most programing language modes, pressing Enter ↵ or Tab ⇥ also does some auto indenting.
For detail, see: Emacs: Newline Representations ^M ^J ^L.
How to indent current line?
indent-for-tab-command 【Tab ⇥】.
How to indent a text selection of source code?
More indentation commands:
indent-relativeindent current line like previous non-blank line.
indent-relativeindent current line like previous non-blank line, only if the previous line has more indent than current line.
indent-rigidlymove all lines in a region, right or left.
How to convert tabs to space in source code?
Select a region first, then call
How to see whitespaces?
whitespace-mode 〔►see Emacs: Make Whitespaces Visible〕
How to delete trailing whitespaces?
How to do completion?
completion-at-point 【Ctrl+Alt+i】. Or call
or, buy something from Best Keyboard for Emacs