How can I use VS Code's Convert Indentation To Spaces or Convert Indentation to Tabs commands on all the files in my workspace in a single action instead of using the command for each file?
-
Somewhat related but not duplicates: [Replace whitespaces with tabs in linux](/q/1424126). And on superuser: [How to replace multiple spaces by one tab](https://superuser.com/q/241018/1749748) – starball Feb 13 '23 at 18:27
1 Answers
If you're on a UNIX system, you can do this via command line using a modified version of this: git ls-files | command grep -E '*.ts$' | awk '{print "expand --tabs=4 --first-only", $0, " > /tmp/e; mv /tmp/e ", $0}' | sh
, which lists all files tracked in the git repo for the current working directory, filters for files with the .ts
extension, and then uses awk
and expand
to replace leading indentation of a tabs to a specified number of spaces.
To go from spaces to tabs, use the unexpand
command instead.
If you're not working with a git repo, you can replace git ls-files
with find -type f
(the advantage of git ls-files
is that it won't touch anything that's not tracked).
Just change the regular expression in the grep filter to whatever you need.
The command replaces leading groups of 4 spaces with tab characters. Just change the --tabs
argument to the unexpand
command with whatever number of spaces your indentation is.
If you want a VS Code way of doing it, you can use rioV8's extension: Command on All Files (I have no affiliation with this extension) (use the editor.action.indentationToTabs
or editor.action.indentationToSpaces
command). I found this from this Q&A: How to execute command across multiple files in VS Code?.

- 20,030
- 7
- 43
- 238