DEV Community


Posted on • Originally published at on

Re-indenting with Notepad++ regex replace

The other day I came up with a ridiculously inefficient regex replace for Notepad++ to re-indent some code from 3 spaces per indent (my team's standard for a certain codebase -- no idea how they settled on 3) to 1 (for tight display on this blog).

  • Search -> Replace (ctrl+H)
  • Search Mode: Regular expression
  • Start with Find what of:
^   ([^ ])
  • Start with Replace with of:
  • Repeat, adding 3 spaces at a time after the ^ to Find what and 1 space at a time to the beginning of Replace with, until the following replace (and the one before it), at 6 & 7 levels deep of indenting, both had 0 results

  • Stopped at Find what of:

^                     ([^ ])
  • Stopped at Replace with of:

I'm sure I could have done better, but it got the job done.

What do you do to quickly change the indentation of code?

Discussion (0)