<p>I've been unsuccessful in getting Emacs to switch from 8 space tabs to 4 space tabs when pressing the <kbd>TAB</kbd> in buffers with the major mode <code>text-mode</code>. I've added the following to my <code>.emacs</code>:</p>

<pre><code>(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)

;;; And I have tried
(setq indent-tabs-mode nil)
(setq tab-width 4)
</code></pre>

<p>No matter how I change my <code>.emacs</code> file (or my buffer's local variables) the <kbd>TAB</kbd> button always does the same thing.</p>

<ol>
<li>If there is no text above, indent <strong>8</strong> spaces</li>
<li>If there is text on the previous line, indent to the beginning of the second word</li>
</ol>

<p>As much as I love Emacs this is getting annoying. Is there a way to make Emacs to at least indent 4 space when there's not text in the previous line?</p>

[{'answer_id': 69992, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Have you tried</p>\n\n<pre><code>(setq  tab-width  4)\n</code></pre>\n'}, {'answer_id': 70027, 'author': 'Bert F', 'author_id': 11296, 'author_profile': 'https://Stackoverflow.com/users/11296', 'pm_score': 6, 'selected': True, 'text': '<blockquote>\n<p>Do not confuse variable <code>tab-width</code> with variable <code>tab-stop-list</code>.\nThe former is used for the display of literal <code>TAB</code> characters.\nThe latter controls what characters are inserted when you press the <code>TAB</code> character in certain modes.</p>\n</blockquote>\n<p>-- <a href="https://www.gnu.org/software/emacs/manual/html_node/efaq/Changing-the-length-of-a-Tab.html" rel="nofollow noreferrer">GNU Emacs Manual</a></p>\n<pre><code>(customize-variable (quote tab-stop-list))\n</code></pre>\n<p>or add <em>tab-stop-list</em> entry to <em>custom-set-variables</em> in <em>.emacs</em> file:</p>\n<pre><code>(custom-set-variables\n  ;; custom-set-variables was added by Custom.\n  ;; If you edit it by hand, you could mess it up, so be careful.\n  ;; Your init file should contain only one such instance.\n  ;; If there is more than one, they won\'t work right.\n \'(tab-stop-list (quote (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120))))\n</code></pre>\n<p>Another way to edit the tab behavior is with with <code>M-x edit-tab-stops</code>.</p>\n<p>See the <a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Tab-Stops.html" rel="nofollow noreferrer">GNU Emacs Manual on Tab Stops</a> for more information on <code>edit-tab-stops</code>.</p>\n'}, {'answer_id': 70205, 'author': 'cjm', 'author_id': 8355, 'author_profile': 'https://Stackoverflow.com/users/8355', 'pm_score': 3, 'selected': False, 'text': '<p>Try this:</p>\n\n<pre><code>(add-hook \'text-mode-hook\n  (function\n   (lambda ()\n     (setq tab-width 4)\n     (define-key text-mode-map "\\C-i" \'self-insert-command)\n     )))\n</code></pre>\n\n<p>That will make TAB always insert a literal TAB character with tab stops every 4 characters (but only in Text mode).  If that\'s not what you\'re asking for, please describe the behavior you\'d like to see.</p>\n'}, {'answer_id': 70329, 'author': 'David Webb', 'author_id': 3171, 'author_profile': 'https://Stackoverflow.com/users/3171', 'pm_score': 4, 'selected': False, 'text': '<p>You may find it easier to set up your tabs as follows:</p>\n\n<pre><code>M-x customize-group\n</code></pre>\n\n<p>At the <code>Customize group:</code> prompt enter <code>indent</code>.</p>\n\n<p>You\'ll see a screen where you can set all you indenting options and set them for the current session or save them for all future sessions.</p>\n\n<p>If you do it this way you\'ll want to <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Saving-Customizations.html" rel="noreferrer">set up a customisations file</a>.</p>\n'}, {'answer_id': 471916, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': "<pre><code>(setq tab-width 4)\n(setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80))\n(setq indent-tabs-mode nil)\n</code></pre>\n"}, {'answer_id': 741020, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>This problem isn\'t caused by missing tab stops; it\'s that emacs has a (new?) tab method called indent-relative that seems designed to line up tabular data. The TAB key is mapped to the method indent-for-tab-command, which calls whatever method the variable indent-line-function is set to, which is indent-relative method for text mode. I havn\'t figured out a good way to override the indent-line-function variable (text mode hook isn\'t working, so maybe it is getting reset after the mode-hooks run?) but one simple way to get rid of this behavior is to just chuck the intent-for-tab-command method by setting TAB to the simpler tab-to-tab-stop method:</p>\n\n<p>(define-key text-mode-map (kbd "TAB") \'tab-to-tab-stop)</p>\n'}, {'answer_id': 1301711, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<pre><code>(setq-default tab-width 4)\n(setq-default indent-tabs-mode nil)\n</code></pre>\n'}, {'answer_id': 1309654, 'author': 'dividebyzero', 'author_id': 160466, 'author_profile': 'https://Stackoverflow.com/users/160466', 'pm_score': 2, 'selected': False, 'text': '<p>Just changing the style with c-set-style was enough for me.</p>\n'}, {'answer_id': 1819405, 'author': 'alcortes', 'author_id': 102532, 'author_profile': 'https://Stackoverflow.com/users/102532', 'pm_score': 7, 'selected': False, 'text': '<h2>Short answer:</h2>\n\n<p>The key point is to tell emacs to insert whatever you want when indenting, this is done by changing the indent-line-function. It is easier to change it to insert a tab and then change tabs into 4 spaces than change it to insert 4 spaces. The following configuration will solve your problem:</p>\n\n<pre><code>(setq-default indent-tabs-mode nil)\n(setq-default tab-width 4)\n(setq indent-line-function \'insert-tab)\n</code></pre>\n\n<h2>Explanation:</h2>\n\n<p>From <a href="http://www.gnu.org/s/emacs/manual/html_node/elisp/Mode_002dSpecific-Indent.html#Mode_002dSpecific-Indent" rel="noreferrer">Indentation Controlled by Major Mode @ emacs manual</a>:</p>\n\n<blockquote>\n  <p>An important function of each major\n  mode is to customize the  key to\n  indent properly for the language being\n  edited.</p>\n  \n  <p>[...]</p>\n  \n  <p>The indent-line-function variable is\n  the function to be used by  (and\n  various commands, like when calling\n  indent-region) to indent the current\n  line. The command\n  indent-according-to-mode does no more\n  than call this function.</p>\n  \n  <p>[...]</p>\n  \n  <p>The default value is indent-relative\n  for many modes.</p>\n</blockquote>\n\n<p>From indent-relative @ emacs manual:</p>\n\n<blockquote>\n  <p>Indent-relative Space out to under next\n  indent point in previous nonblank line.</p>\n  \n  <p>[...]</p>\n  \n  <p>If the previous nonblank line has no\n  indent points beyond the column point\n  starts at, `tab-to-tab-stop\' is done\n  instead.</p>\n</blockquote>\n\n<p>Just change the value of indent-line-function to the insert-tab function and configure tab insertion as 4 spaces.</p>\n'}, {'answer_id': 10438585, 'author': 'qwerty9967', 'author_id': 1305559, 'author_profile': 'https://Stackoverflow.com/users/1305559', 'pm_score': 2, 'selected': False, 'text': '<p>Add this to your .emacs file:</p>\n\n<p>This will set the width that a tab is displayed to 2 characters (change the number 2 to whatever you want)</p>\n\n<pre><code>(setq default-tab-width 2)\n</code></pre>\n\n<p>To make sure that emacs is actually using tabs instead of spaces:</p>\n\n<pre><code>(global-set-key (kbd "TAB") \'self-insert-command)\n</code></pre>\n\n<p>As an aside, the default for emacs when backspacing over a tab is to convert it to spaces and then delete a space.  This can be annoying.  If you want it to just delete the tab, you can do this:</p>\n\n<pre><code>(setq c-backspace-function \'backward-delete-char)\n</code></pre>\n\n<p>Enjoy!</p>\n'}, {'answer_id': 10439239, 'author': 'phils', 'author_id': 324105, 'author_profile': 'https://Stackoverflow.com/users/324105', 'pm_score': 6, 'selected': False, 'text': '<p><em>Update:</em> Since Emacs 24.4:</p>\n\n<blockquote>\n  <p><code>tab-stop-list</code> is now implicitly extended to infinity.  Its default value is changed to <code>nil</code> which means a tab stop every <code>tab-width</code> columns.</p>\n</blockquote>\n\n<p>which means that there\'s no longer any need to be setting <code>tab-stop-list</code> in the way shown below, as you can keep it set to <code>nil</code>.</p>\n\n<p><em>Original answer follows...</em></p>\n\n<hr/>\n\n<p>It always pains me slightly seeing things like <code>(setq tab-stop-list 4 8 12 ................)</code> when the <code>number-sequence</code> function is sitting there waiting to be used.</p>\n\n<pre class="lang-el prettyprint-override"><code>(setq tab-stop-list (number-sequence 4 200 4))\n</code></pre>\n\n<p>or</p>\n\n<pre class="lang-el prettyprint-override"><code>(defun my-generate-tab-stops (&amp;optional width max)\n  "Return a sequence suitable for `tab-stop-list\'."\n  (let* ((max-column (or max 200))\n         (tab-width (or width tab-width))\n         (count (/ max-column tab-width)))\n    (number-sequence tab-width (* tab-width count) tab-width)))\n\n(setq tab-width 4)\n(setq tab-stop-list (my-generate-tab-stops))\n</code></pre>\n'}, {'answer_id': 13114566, 'author': 'gigilibala', 'author_id': 1775441, 'author_profile': 'https://Stackoverflow.com/users/1775441', 'pm_score': 3, 'selected': False, 'text': "<p>You can add these lines of code to your .emacs file.\nIt adds a hook for text mode to use insert-tab instead of indent-relative.</p>\n\n<pre><code>(custom-set-variables\n '(indent-line-function 'insert-tab)\n '(indent-tabs-mode t)\n '(tab-width 4))\n(add-hook 'text-mode-hook\n      (lambda() (setq indent-line-function 'insert-tab)))\n</code></pre>\n\n<p>I hope it helps.</p>\n"}, {'answer_id': 16127986, 'author': 'lawlist', 'author_id': 2112489, 'author_profile': 'https://Stackoverflow.com/users/2112489', 'pm_score': 3, 'selected': False, 'text': '  <pre class="lang-lisp prettyprint-override"><code>(defun my-custom-settings-fn ()\n  (setq indent-tabs-mode t)\n  (setq tab-stop-list (number-sequence 2 200 2))\n  (setq tab-width 2)\n  (setq indent-line-function \'insert-tab))\n\n(add-hook \'text-mode-hook \'my-custom-settings-fn)\n</code></pre>\n'}, {'answer_id': 16210873, 'author': 'user2318996', 'author_id': 2318996, 'author_profile': 'https://Stackoverflow.com/users/2318996', 'pm_score': 3, 'selected': False, 'text': '<pre><code>(setq-default indent-tabs-mode nil)\n(setq-default tab-width 4)\n(setq indent-line-function \'insert-tab)\n(setq c-default-style "linux") \n(setq c-basic-offset 4) \n(c-set-offset \'comment-intro 0)\n</code></pre>\n\n<p>this works for C++ code and the comment inside too</p>\n'}, {'answer_id': 18887294, 'author': 'flyrain', 'author_id': 933856, 'author_profile': 'https://Stackoverflow.com/users/933856', 'pm_score': 0, 'selected': False, 'text': '<p>By the way, for <strong>C-mode</strong>, I add <strong><code>(setq-default c-basic-offset 4)</code></strong> to .emacs. See <a href="http://www.emacswiki.org/emacs/IndentingC" rel="nofollow">http://www.emacswiki.org/emacs/IndentingC</a> for details.</p>\n'}, {'answer_id': 26288992, 'author': 'forkandwait', 'author_id': 2554948, 'author_profile': 'https://Stackoverflow.com/users/2554948', 'pm_score': 0, 'selected': False, 'text': '<p>From my init file, different because I wanted spaces instead of tabs:</p>\n\n<pre>\n\n(add-hook \'sql-mode-hook\n          (lambda ()\n             (progn\n               (setq-default tab-width 4)\n               (setq indent-tabs-mode nil)\n               (setq indent-line-function \'tab-to-tab-stop) \n               (modify-syntax-entry ?_ "w")       ; now \'_\' is not considered a word-delimiter\n               (modify-syntax-entry ?- "w")       ; now \'-\' is not considered a word-delimiter \n               )))\n</pre>\n'}, {'answer_id': 31124271, 'author': 'user1009285', 'author_id': 1009285, 'author_profile': 'https://Stackoverflow.com/users/1009285', 'pm_score': 2, 'selected': False, 'text': '<p>The best answers did not work for until I wrote this in the .emacs file:</p>\n\n<pre><code>(global-set-key (kbd "TAB") \'self-insert-command)\n</code></pre>\n'}, {'answer_id': 42148697, 'author': 'ryanpcmcquen', 'author_id': 2662028, 'author_profile': 'https://Stackoverflow.com/users/2662028', 'pm_score': 2, 'selected': False, 'text': '<p>This is the only solution that keeps a tab from ever getting inserted for me, without a sequence or conversion of tabs to spaces. Both of those seemed adequate, but wasteful:</p>\n\n<pre><code>(setq-default\n    indent-tabs-mode nil\n    tab-width 4\n    tab-stop-list (quote (4 8))\n)\n</code></pre>\n\n<p>Note that <code>quote</code> needs two numbers to work (but not more!).</p>\n\n<p>Also, in most major modes (<code>Python</code> for instance), indentation is automatic in Emacs. If you need to indent outside of the auto indent, use:</p>\n\n<p><kbd>M</kbd>-<kbd>i</kbd></p>\n'}, {'answer_id': 42841185, 'author': 'Yary', 'author_id': 379333, 'author_profile': 'https://Stackoverflow.com/users/379333', 'pm_score': 2, 'selected': False, 'text': "<p>Customizations can shadow <code>(setq tab width 4)</code> so either use <code>setq-default</code> or let Customize know what you're doing. I also had issues similar to the OP and fixed it with this alone, did not need to adjust <code>tab-stop-list</code> or any <code>insert</code> functions:</p>\n\n<pre><code>(custom-set-variables\n '(tab-width 4 't)\n )\n</code></pre>\n\n<p>Found it useful to add this immediately after (a tip from emacsWiki):</p>\n\n<pre><code>(defvaralias 'c-basic-offset 'tab-width)\n(defvaralias 'cperl-indent-level 'tab-width)\n</code></pre>\n"}, {'answer_id': 61216979, 'author': 'Saurabh', 'author_id': 13055097, 'author_profile': 'https://Stackoverflow.com/users/13055097', 'pm_score': 0, 'selected': False, 'text': '<p>Modified <a href="https://stackoverflow.com/a/16127986/13055097">this answer</a> without any hook:</p>\n\n<pre><code>(setq-default\n  indent-tabs-mode t\n  tab-stop-list (number-sequence 4 200 4)\n  tab-width 4\n  indent-line-function \'insert-tab)\n</code></pre>\n'}, {'answer_id': 69657718, 'author': 'alex_1948511', 'author_id': 1948511, 'author_profile': 'https://Stackoverflow.com/users/1948511', 'pm_score': 0, 'selected': False, 'text': "<p>To make in text-mode pressing <code>Tab</code> does indent then tabbing/spacing by fixed values (NOT by previous line words)</p>\n<p>see also: indent-relative-first-indent-point, tab-width indent-tabs-mode</p>\n<pre><code>(add-hook 'text-mode-hook\n (lambda()\n   (progn\n     (setq tab-always-indent nil) \n     ;(setq electric-indent-mode nil)\n     (setq indent-line-function \n           (lambda()\n             (indent-relative 't)   \n             )\n           ) \n     (setq tab-always-indent nil)\n     )))\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/69934', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/680/']