17

Is there any way to convert all spaces to tabs, not file by file?

If I open a file and go through View => Indentation => Convert Indentation to Tabs, it only changes this file. I want to convert indentations to tabs in a whole project.

megawac
  • 10,953
  • 5
  • 40
  • 61
Sodbileg Gansukh
  • 1,730
  • 2
  • 13
  • 19

1 Answers1

24

Use search and replace in multiple files to convert n spaces to tabs in select files.

First open find in files panel, cmd + shift + f, by default to find and replace in multiple files. Next define a regular expression to match spaces as tabs eg {4} (make sure you set Regular Expressions in the panel) for 4 spaces and replace with \t in desired files. Change {4} to however many spaces are being used for indentation.

As mentioned in comments to match spaces at the start of a line you can use the regexp ^( {4})+

megawac
  • 10,953
  • 5
  • 40
  • 61
  • @moss what part of it doesnt work? Its a simple enough method just make sure you change ` {4}` to however many spaces youre using per indent – megawac Feb 03 '14 at 18:22
  • For me ST wants a confirm save on every file, it opened every one of them in tabs (thousands of them) during the operation. Had to use taks manager and kill ST because couldn't close it without confirming thousands of times. Also forgot to omit the quotes around " {4}" (I'm trying the opposite, tabs to spaces) so basically hosed the project but am just testing now so no harm no foul. Maybe should include that in the answer about not literally putting the quotes. – isimmons Apr 13 '14 at 16:42
  • Ok I found under 'file' in the menu "save all" then follow that with "close all" Also fixed the above mentioned 'hosed project' by turning off regex and replacing the literal " {4}" with a literal 4 spaces and all is good now. As for converting tabs to spaces it took me a while to find. \s in the 'replace' box puts a literal "\s". I found when regex is turned on the replace should be 4 spaces followed by $1 to replace the matched \t with 4 spaces. Also this method doesn't allow choosing file types so it will convert all files under the 'where' box. Something you might not want to do. – isimmons Apr 13 '14 at 17:37
  • 3
    @isimmons you can specify an include filter in the find replace via `*.{js,txt}` (for instance) in the where which will only include javascript and text files. Probably should of mentioned save all in the answer good point – megawac Apr 13 '14 at 18:09
  • Yep, just found it at the link Thanks – isimmons Apr 13 '14 at 18:47
  • 4
    Works just as well the other way around, eg. (regex) search for `\t` and replace with two spaces – cabgfx Feb 17 '15 at 12:39
  • 2
    But doesn't this match 4 spaces anywhere, instead of just the beginning of the line of code? For example if you have a string that has 4 spaces, this will replace that too, no? Is there a regex that matches only the beginning spaces? – laggingreflex Jan 19 '16 at 17:22
  • @laggingreflex use `^( {4})+` – megawac Jan 28 '16 at 17:46
  • `^( {4})+` will replace _all_ leading sets of four spaces with _one_ tab. Repeatedly replacing `^(\t*)( {2})` with `\1\t` until there are no matches works, but it's not great. – aidan Nov 08 '18 at 06:06
  • @isimmons you can save them all in File / Save All – DarkSide Mar 07 '21 at 12:28