I have many software projects, in different languages. They are all in git repos. I normally use vim to program.

Recently, I enabled the vim spacehi plugin, and started noticing just how much bad whitespace my projects have.

It's a massive pain to go through every single file, and manually remove bad whitespace in all my projects. Is there any kind of tool, or perhaps sed line, that can be run project-wide to delete all bad whitespace?

Obviously, letting a tool modify source code isn't ideal, but git difftool can easily tell you exactly what the tool changed, so I can review the changes it makes to my various source files before doing a git commit, which is still easier than making the changes manually.

  • What is bad whitespace? – Arkadiusz Drabczyk Feb 23 at 18:36
  • @ArkadiuszDrabczyk leading tabs, blank spaces alone on lines, trailing tabs or spaces, etc. I actually don't know the full list, but it seems to be pretty standard. Lots of code editors, like VS Code, vim, etc, have plugins to highlight bad whitespace. If there's no simple tool to already do this, or an existing sed line, I might find out everything considered bad whitespace, and create the tool myself – John Feb 23 at 18:41
  • Depending on the programming language and the context, can't those whitespaces have meaning. I often use tabs for indentation, for example, and trailing whitespaces makes sense in, for example, certain markup languages. How can you detect what's "bad" in one place while the same construct is meaningful in another? – Kusalananda Feb 23 at 18:45
  • @Kusalananda it won't get it right every time. It would be insane to let it modify your code without reviewing the changes with a diff tool afterwards. Luckily, git makes it pretty easy to do just that. The languages that use whitespace as indentation, like Python, don't generally care what whitespace you use, as long as you're consistent. If it changes all leading tabs to a set number of spaces, most languages shouldn't have a problem with it, and if the language WILL have a problem with it, it should be caught during the diff review process – John Feb 23 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.