75

How do I prevent vim from replacing spaces with tabs when autoindent is on?

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

6 Answers6

84

It is perhaps a good idea not to use tabs at all.

:set expandtab

If you want to replace all the tabs in your file to 3 spaces (which will look pretty similar to tabstop=3):

:%s/^I/   /

(where ^I is the TAB character)

From the VIM online help:

'tabstop' 'ts'      number  (default 8)
        local to buffer
Number of spaces that a <Tab> in the file counts for.  Also see
|:retab| command, and 'softtabstop' option.

Note: Setting 'tabstop' to any other value than 8 can make your file
appear wrong in many places (e.g., when printing it).

There are four main ways to use tabs in Vim:
1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
   (or 3 or whatever you prefer) and use 'noexpandtab'.  Then Vim
   will use a mix of tabs and spaces, but typing <Tab> and <BS> will
   behave like a tab appears every 4 (or 3) characters.
2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
   'expandtab'.  This way you will always insert spaces.  The
   formatting will never be messed up when 'tabstop' is changed.
3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
   |modeline| to set these values when editing the file again.  Only
   works when using Vim to edit the file.
4. Always set 'tabstop' and 'shiftwidth' to the same value, and
   'noexpandtab'.  This should then work (for initial indents only)
   for any tabstop setting that people use.  It might be nice to have
   tabs after the first non-blank inserted as spaces if you do this
   though.  Otherwise aligned comments will be wrong when 'tabstop' is
   changed.
Aaron Thoma
  • 3,820
  • 1
  • 37
  • 34
user11211
  • 1,237
  • 10
  • 7
  • 1
    Worth noting, tabs are still inserted in paste mode. While this makes sense, it took me forever to figure out where those pesky tabs were coming from. Pro Tip: :set list :set listchars=tab:>- – quartarian Mar 29 '17 at 20:50
  • FYI Golang uses tabs. – Vitaly Zdanevich Jan 27 '23 at 07:09
  • life would be so much easier if only tabs were used for indentation, if I ever meet that lobotomized idiot who came up with idea to use spaces instead... – lot May 09 '23 at 12:25
53

You can convert all TAB to SPACE

:set et
:ret!

or convert all SPACE to TAB

:set et!
:ret!
kev
  • 155,172
  • 47
  • 273
  • 272
  • 1
    Nice to know of this command. I used to fix my 'python woes' by using gedit while copy-pasting. – Nav Sep 05 '11 at 12:07
41

all I want is the autoindented line to have exactly the same indentation characters as the previous line.

:help copyindent

'copyindent' 'ci' boolean (default off); local to buffer; {not in Vi}

Copy the structure of the existing lines indent when autoindenting a new line. Normally the new indent is reconstructed by a series of tabs followed by spaces as required (unless 'expandtab' is enabled, in which case only spaces are used). Enabling this option makes the new line copy whatever characters were used for indenting on the existing line. If the new indent is greater than on the existing line, the remaining space is filled in the normal manner.

NOTE: 'copyindent' is reset when 'compatible' is set.
Also see 'preserveindent'.

:help preserveindent

'preserveindent' 'pi' boolean (default off); local to buffer; {not in Vi}

When changing the indent of the current line, preserve as much of the indent structure as possible. Normally the indent is replaced by a series of tabs followed by spaces as required (unless 'expandtab' is enabled, in which case only spaces are used). Enabling this option means the indent will preserve as many existing characters as possible for indenting, and only add additional tabs or spaces as required.

NOTE: When using ">>" multiple times the resulting indent is a mix of tabs and spaces. You might not like this.
NOTE: 'preserveindent' is reset when 'compatible' is set.
Also see 'copyindent'.
Use :retab to clean up white space.

Aristotle Pagaltzis
  • 112,955
  • 23
  • 98
  • 97
27

Here's part of my .vimrc:

set autoindent
set expandtab
set softtabstop=4
set shiftwidth=4

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

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.

graywh
  • 9,640
  • 2
  • 29
  • 27
  • 3
    "**Super retab**": `:retab` converts all sequences of tabs or spaces, even those that might be in a "quoted string like this". [This tip](http://vim.wikia.com/wiki/Super_retab) shows how to convert only the indents at the left margin. Any spaces or tabs after the first non-white character are not affected. – Aaron Thoma Jun 13 '11 at 18:23
1

Maybe the bottom of this can help you?

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:

set softtabstop=2
Community
  • 1
  • 1
svrist
  • 7,042
  • 7
  • 44
  • 67