2

I have a large amount of tab-indented (outlined) text that is missing a leading bullet character:

        some point
            a sub-point

I want to replace each tab by 2 spaces, and add a hyphen in front. This is the target format I need:

  - some point
    - a sub-point

Find:
I can use the regex \t+\S to find "any number of tabs, followed by any non-whitespace". This works just fine, and the "find" function highlights the start of every line, up to and including the first non-tab character.

Replace:
How can I use regex to specify "replace every tab with 2 spaces, followed by one hyphen and one space, followed by the original non-tab character"? Specifically:

  1. How do I specify the replacement to happen "as many times as there were matches in the find"? (So three tabs would become six spaces, etc.)
  2. How do I specify the literal "one hyphen then one space"?
  3. How do I specify "keep the non-tab character unchanged"?

My google-fu has led me to the concept of \1 but I don't see how I can use that. My trial-and-error testing only produced errors.
Update:
I discovered a thing about \1: I must use parentheses in my find string: (\t+)(\S) and then use that in the replace part: \1- \2. This helps me to solve item #2 and #3 above, but I am still at a loss about #1!
Up-Update:
Duh - #1 can be a simple non-regex find/replace action as long as there are no tab characters elsewhere in the text. That's something I need to investigate now!

I am trying with Ubuntu's Gedit but I could also use Notepad++ or Sublime if you have editor-specific suggestions.

  • I see you made some updates while I posted my answer. I hope you got it sorted out. If so, let us know. – LPChip Jan 27 '18 at 14:57
1

You could do it with Notepad++ in two steps:

First step:

  • Ctrl+H
  • Find what: \t
  • Replace with:    (2 spaces)
  • check Wrap around
  • check Regular expression
  • Replace all

Second step:

  • Find what: ^\h+
  • Replace with: $0-
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

^       : beginning of line
\h+     : 1 or more horizontal spaces

Replacement:

$0      : the whole match (ie. all the spaces at the beginning of each line)
-       : a dash followed by a space
  • 1
    I particularly like this answer for its focus on "beginning of the line" which eliminates my need to handle the first non-space character -- as well as its clear and precise explanation. – Torben Gundtofte-Bruun Jan 27 '18 at 22:34
1

There are two methods that you can use.

You can search for the entire string + what comes next, and use groupings to replace it with one or more groups but not all. This is where $1 or \1 comes in play.

You can also only search for the part you want to replace, and just replace it with something else.

First, build the regex that you want to explicitly replace and search for that. If your search searches for multiple parts of a string and you want to keep something in the middle, then grouping is required. Example: search for [b]text[/b] and replace it with <b>text</b>

Your regex would be something like \[b\](.+)\]\/b\] and replace it with <b>\1</b>

If you only need to change every text occurrence that is one continuous string, you can search specifically for that and replace it. For example, searching for          -My example and replace it with  - My example (which basically replaces any tabs for two spaces, and adds a space after the -, you would search for \t+- and replace it with  -

The regex here means: search for any tab that occurs 1 or more times, followed by a hypen. If you search for this string without actually replacing it, you will see that it will select the text you want to replace.

If you want to substitute any tab with two spaces, you would simply search for \t and replace it with  

Do note, I used a space followed by an alt+255 everywhere in this post to type 2 spaces in a row, otherwise SuperUser condenses everything to 1 space.

  • Thank you for clarifying the use of \1. In my case, each line begins with arbitrary words so I can't follow your My example idea. – Torben Gundtofte-Bruun Jan 27 '18 at 22:36
  • Yes you can. Those words are simply skipped in the search. – LPChip Jan 28 '18 at 11:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.