1

We're doing some code clean-up on a project I'm on that's grown rather large. We've decided that site wide all tabs (which are soft tabs of four spaces) should be two-spaces standard (there's been a lot of mixing). Is there an easy way to accomplish this? We're talking hundreds of JS files here. I'd just like to change any instance of four spaces to two spaces. Is there a tool I don't know of or some Sublime Text or VIM way of doing this?

1
  • Thank you all for your answers. It seems to be that there is no easy way to do this project-wide. Like I said, there are some files with 2 space tab widths, some with 4 space tab widths, some mixed, and even some mixed in hard-tabs. Wanted to mass convert all files to 2 space tab width. But all of the given suggestions do not handle a case where the file is already set to two-space tab width. It appears that I will have to do it on a file-by-file basis. But these answers all helped a lot. Thank you. Apr 9, 2013 at 13:51

3 Answers 3

1

Open up your project in sublime text 2, click cmd + shift + f on Mac or ctrl + shift + f on Windows. Enter the value you want to find in the first input, and in the third input, enter the value you want to change the original value to.

It will open each file and change the desired text. Just save and have a good day.

1

You can do this in vim. Something like this:

:set expandtab    // Changes all tabs to spaces (soft-tab)
:set tabstop=2    // Tabs should be two spaces
:retab            // Changes existing tabs in document to this new format

In order to do this on hundreds of files, you could write a bash script that loops through the files and executes this vim command from the command script; perhaps vim -c "retab|wq" <file> or something similar. Alternatively, you could load all the files into vim at once and use :bufdo

Also see http://vim.wikia.com/wiki/Converting_tabs_to_spaces

1
  • I'd actually tried this already, but :retab isn't working. I tried setting the :shiftwidth=2 too. No dice. Apr 8, 2013 at 22:02
1

Why not something like:

find . -name "*.java" -exec sed -i '' 's/    /\  /g' {} \;
0

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.