Replace leading spaces with Notepad++

16,284

Solution 1

As Tim's answer indicates, this can't be done in a single search/replace, however here is how you can accomplish the same task fairly quickly using multiple replacements:

Find: ^( *)[ ]

Replace with: \1@

Now just spam the "Replace All" button until it indicates that there were no matches to replace. This will replace a single space at the beginning of each line on each click, so it will require the same number of clicks as your most-indented line.

Make sure "Regular expression" is selected as the search mode.

Solution 2

You would need variable-length lookbehind assertions to do this in a single regex, and Notepad++ doesn't support these.

For the record, in EditPadPro you can search for (?<=^ *)\s and replace with @.

Share:
16,284
The Demigeek
Author by

The Demigeek

Updated on June 26, 2022

Comments

  • The Demigeek
    The Demigeek almost 2 years

    I'd like to use Notepad++ to replace all leading spaces on a line with a like number of given characters. So for instance, I want to change:

    zero
     one
      two
       three
    

    into:

    zero
    @one
    @@two
    @@@three
    

    I haven't been successful at getting this working. I did find Regex to replace html whitespace and leading whitespace in notepad++, but wasn't able to get the result I wanted.

    Is this possible with Notepad++? I'd rather not have to write code to do this...

  • The Demigeek
    The Demigeek over 11 years
    Thanks! That's what I needed; I guess I just didn't fully understand the answer to the original post.
  • salvu
    salvu about 6 years
    @Andrew Clark I used your regex to replace leading spaces in a large text file using just " " (one space) as the "replace with" character. Just for someone happening here: the Replace All button works ok and one does not need to use a number of clicks. It could be that this functionality has been added later on after all this post is 5 years old :-) (and still very useful)