3

I press "Tab, Tab, Tab, Left, Delete" to get the required indentation. After saving the file I see "^I ". If I press "Space" 11 times instead I see " ".

How to make vim stop introducing new hard tab characters (without changing user-perceived indentation behaviour or mangling existing hard tabs)?

There are a lot of confusing smarttabs, shiftwidths, tabstops and other options. How to do it right?

1
  • How to do it right depends on what behavior you want. Vim offers a lot of flexibility in how it indents, but it has limits, too. It would help to have a specific example of something Vim does one way that you wish it would do a different way.
    – garyjohn
    Jul 12, 2011 at 20:07

2 Answers 2

5

If you want vim to translate any of your tabs to spaces use:

set expandtab                     " expand tabs!

You can set how many "spaces" a tab translates to with:

set softtabstop=4                 " if expandtab on, how many space a tab is

If vim finds existing tabs and you want it translated to spaces, set this to the number of spaces a tab will represent

set tabstop=4                     " tab spacing of 4 for real tabs

(note that set tabstop will not effect existing tabs, just the display of them to you whilst in the the editor).

EDIT I would also checkout garyjohn's answer, for a much more precise answer on treatment of existing tabbing, as well as the behavior when using the tabshifting commands << >>. (I actually learnt some things I didn't know about it's behaviour!) +1 to his answer from me!

2

Vim will not mangle existing tabs unless you

  • execute :retab or
  • change the indentation of a line using a command such as << or >>.

If you change the indentation of a line and 'expandtab' is set, then Vim will normally replace all the tabs with an appropriate number of spaces. If you change the indentation of a line and 'noexpandtab' is set, then Vim will normally replace as many leading spaces as it can with tabs.

That normal behavior can be changed to try to preserve the structure of the original indentation by setting the 'preserveindent' option.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .