Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
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 '11 at 20:07
add comment

2 Answers 2

up vote 3 down vote accepted

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 behaviour when using the tabshiting commands << >>. (I actually learnt some things I didn't know about it's behaviour!) +1 to his answer from me!

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.