3

So I've written some code of an assignment and i forgot the universities policy of indent with 2spaces.

Normally I'ld have put a: //vim: ts=2:tw=78: et:

at the top of my files, but this time I forgot.

How should I go about replacing all tabs with 2 space? would s/[TAB]/[SPACE][SPACE] work? (replacing [TAB] and [SPACE] with the respective key presses for the characters)

3 Answers 3

9

How should I go about replacing all tabs with 2 space?

You can do

:%s/\t/  /g
8

You should have a look at retab. First set tabstop, shiftwidth and expandtab, then use the retab command: it will reformat all your file with the desired format.

2
  • 1
    Watch for literal tabs that are not indenting, that is, are not at the beginning of line. From :h :retab "Careful: This command modifies any <Tab> characters inside of strings". It means that "\tFoo\tBar" becomes " Foo Bar" Apr 26, 2010 at 9:38
  • I fround oput that if i just add the vim setting stuff the the to, the when i reopen the file, vim will automatically call retab even. Thankyou May 8, 2010 at 3:45
2

Run the following commands:

:set expandtab tabstop=2 shiftwidth=2 softtabstop=2
:retab!

Check out my screencast on tidying whitespace here: http://vimcasts.org/episodes/tidying-whitespace/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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