<p>How do I prevent vim from replacing spaces with tabs when autoindent is on?</p>

<p>An example: if I have two tabs and 7 spaces in the beginning of the line, and <code>tabstop=3</code>, and I press Enter, the next line has four tabs and 1 space in the beginning, but I don't want that...</p>

[{'answer_id': 70034, 'author': 'svrist', 'author_id': 86, 'author_profile': 'https://Stackoverflow.com/users/86', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://www.jwz.org/doc/tabs-vs-spaces.html" rel="nofollow noreferrer">Maybe the bottom of this can help you?</a></p>\n<blockquote>\n<p>Standard vi interprets the tab key literally, but there are popular vi-derived alternatives that are smarter, like vim. To get vim to interpret tab as an ``indent\'\' command instead of an insert-a-tab command, do this:</p>\n<pre><code>set softtabstop=2\n</code></pre>\n</blockquote>\n'}, {'answer_id': 70049, 'author': 'user11211', 'author_id': 11211, 'author_profile': 'https://Stackoverflow.com/users/11211', 'pm_score': 7, 'selected': True, 'text': "<p>It is perhaps a good idea not to use tabs at all.</p>\n\n<pre><code>:set expandtab\n</code></pre>\n\n<p>If you want to replace all the tabs in your file to 3 spaces (which will look pretty similar to <code>tabstop=3</code>):</p>\n\n<pre><code>:%s/^I/   /\n</code></pre>\n\n<p>(where <code>^I</code> is the <kbd>TAB</kbd> character)</p>\n\n<p>From the VIM online help:</p>\n\n<pre><code>'tabstop' 'ts'      number  (default 8)\n        local to buffer\nNumber of spaces that a &lt;Tab&gt; in the file counts for.  Also see\n|:retab| command, and 'softtabstop' option.\n\nNote: Setting 'tabstop' to any other value than 8 can make your file\nappear wrong in many places (e.g., when printing it).\n\nThere are four main ways to use tabs in Vim:\n1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4\n   (or 3 or whatever you prefer) and use 'noexpandtab'.  Then Vim\n   will use a mix of tabs and spaces, but typing &lt;Tab&gt; and &lt;BS&gt; will\n   behave like a tab appears every 4 (or 3) characters.\n2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use\n   'expandtab'.  This way you will always insert spaces.  The\n   formatting will never be messed up when 'tabstop' is changed.\n3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a\n   |modeline| to set these values when editing the file again.  Only\n   works when using Vim to edit the file.\n4. Always set 'tabstop' and 'shiftwidth' to the same value, and\n   'noexpandtab'.  This should then work (for initial indents only)\n   for any tabstop setting that people use.  It might be nice to have\n   tabs after the first non-blank inserted as spaces if you do this\n   though.  Otherwise aligned comments will be wrong when 'tabstop' is\n   changed.\n</code></pre>\n"}, {'answer_id': 70054, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 5, 'selected': False, 'text': "<p>Here's part of my <code>.vimrc</code>:</p>\n\n<pre><code>set autoindent\nset expandtab\nset softtabstop=4\nset shiftwidth=4\n</code></pre>\n\n<p>This works well for me because I absolutely do not want tabs in my source code. It seems from your question that you do want to keep two tabs and seven spaces on the next line, and I'm not sure there's a way to teach vim to accommodate that style.</p>\n"}, {'answer_id': 71422, 'author': 'Aristotle Pagaltzis', 'author_id': 9410, 'author_profile': 'https://Stackoverflow.com/users/9410', 'pm_score': 5, 'selected': False, 'text': '<blockquote>\n  <p>all I want is the autoindented line to have exactly the same indentation characters as the previous line.</p>\n</blockquote>\n\n<h1><a href="http://www.vim.org/htmldoc/options.html#%27copyindent%27" rel="noreferrer"><code>:help copyindent</code></a></h1>\n\n<blockquote>\n  <h2><a href="http://www.vim.org/htmldoc/options.html#%27copyindent%27" rel="noreferrer">\'copyindent\'</a> <a href="http://www.vim.org/htmldoc/options.html#%27ci%27" rel="noreferrer">\'ci\'</a>  <a href="http://www.vim.org/htmldoc/options.html#boolean" rel="noreferrer">boolean</a>  (default off); local to buffer; {not in Vi}</h2>\n  \n  <p>Copy the structure of the existing lines indent when autoindenting a\n  new line. Normally the new indent is reconstructed by a series of\n  tabs followed by spaces as required (unless <a href="http://www.vim.org/htmldoc/options.html#%27expandtab%27" rel="noreferrer">\'expandtab\'</a> is enabled,\n  in which case only spaces are used).  Enabling this option makes the\n  new line copy whatever characters were used for indenting on the\n  existing line.  If the new indent is greater than on the existing\n  line, the remaining space is filled in the normal manner.</p>\n  \n  <p>NOTE: <a href="http://www.vim.org/htmldoc/options.html#%27copyindent%27" rel="noreferrer">\'copyindent\'</a> is reset when <a href="http://www.vim.org/htmldoc/options.html#%27compatible%27" rel="noreferrer">\'compatible\'</a> is set.<br>\n  Also see <a href="http://www.vim.org/htmldoc/options.html#%27preserveindent%27" rel="noreferrer">\'preserveindent\'</a>.</p>\n</blockquote>\n\n<h1><a href="http://www.vim.org/htmldoc/options.html#%27preserveindent%27" rel="noreferrer"><code>:help preserveindent</code></a></h1>\n\n<blockquote>\n  <h2><a href="http://www.vim.org/htmldoc/options.html#%27preserveindent%27" rel="noreferrer">\'preserveindent\'</a> <a href="http://www.vim.org/htmldoc/options.html#%27pi%27" rel="noreferrer">\'pi\'</a>  <a href="http://www.vim.org/htmldoc/options.html#boolean" rel="noreferrer">boolean</a>  (default off); local to buffer; {not in Vi}</h2>\n  \n  <p>When changing the indent of the current line, preserve as much of the\n  indent structure as possible.  Normally the indent is replaced by a\n  series of tabs followed by spaces as required (unless <a href="http://www.vim.org/htmldoc/options.html#%27expandtab%27" rel="noreferrer">\'expandtab\'</a> is\n  enabled, in which case only spaces are used).  Enabling this option\n  means the indent will preserve as many existing characters as possible\n  for indenting, and only add additional tabs or spaces as required.</p>\n  \n  <p>NOTE: When using "&gt;&gt;" multiple times the resulting indent is a mix of\n  tabs and spaces.  You might not like this.<br>\n  NOTE: <a href="http://www.vim.org/htmldoc/options.html#%27preserveindent%27" rel="noreferrer">\'preserveindent\'</a> is reset when <a href="http://www.vim.org/htmldoc/options.html#%27compatible%27" rel="noreferrer">\'compatible\'</a> is set.<br>\n  Also see <a href="http://www.vim.org/htmldoc/options.html#%27copyindent%27" rel="noreferrer">\'copyindent\'</a>.<br>\n  Use :retab to clean up white space.</p>\n</blockquote>\n'}, {'answer_id': 159130, 'author': 'graywh', 'author_id': 18038, 'author_profile': 'https://Stackoverflow.com/users/18038', 'pm_score': 1, 'selected': False, 'text': "<p>If you want to replace all the tabs with spaces based on the setting of 'ts', you can use :retab.  It can also do the reverse.</p>\n"}, {'answer_id': 5790967, 'author': 'kev', 'author_id': 348785, 'author_profile': 'https://Stackoverflow.com/users/348785', 'pm_score': 6, 'selected': False, 'text': '<p>You can convert all <code>TAB</code> to <code>SPACE</code></p>\n\n<pre><code>:set et\n:ret!\n</code></pre>\n\n<p>or convert all <code>SPACE</code> to <code>TAB</code></p>\n\n<pre><code>:set et!\n:ret!\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/69998', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']