By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Request: @STRIP_ANSI[] function

This one is killing me because TCC doesn't have regex substitution that I know about, and I'd rather not rely on sed.

I just want to be able to do:

   set CLEAN_TITLE=%@STRIP_ANSI[%title_with_ansi]
I'd also like this function to be created inside of a bat file, as it's going to be part of my set-colors.bat that defines all my ansi characters.

Just having trouble making it myself because of a lack of experience with functions in general.

Adding to the complication is I'm doing this within a BAT file, so it has to be a oneliner? What I've currently got, which doesn't return the value correctly (how do you return values?), but which seems to maybe work internally, is:

function strip_ansi_fail=`set string=%1$ %+ unset /q stripped %+ :loop %+  for /f "tokens=1,* delims=" %%a in ("%string%") do ( set "stripped=!stripped!%%a" %+ set "string=%%b" ) %+ if not "%string%"=="" goto :loop %+  return %stripped%"`

I just need to get my example working. I am 100% positive that "return %stripped%" at the end is nonsense that doesn't do anything
Also, could we create a %@HAS_ANSI[] function , that returns a "1" if ANSI codes are detected, and a "0" if none are?
Have a look at @REREPLACE (regex substitution with back-references).
Thanks, I will!
Hmm. @REREPLACE doesn't seem to allow you to actually change a string to a value not already in it as a match. There's no way to change "hello" to "goodbye", for example.
Hmm. @REREPLACE doesn't seem to allow you to actually change a string to a value not already in it as a match. There's no way to change "hello" to "goodbye", for example.
I must be misunderdtanding you. That's simple.

echo %@rereplace[Hello,Goodbye,Hello world!]
Goodbye world!
Oh i don't think you were misunderstanding me, I think I was misunderstanding the @REPLACE help page hahahaha.

IMO, this is a very poor help page that doesn't provide a simple example, and the explanation is more specific than the actual functionality. But that's just my opinion.

Last edited:
Ugh, i just can't get this to work.

There's multiple issues.

1) it seems that %@CHAR[27] gives an error when used in REREPLACE context. So I tried using my %ESCAPE environment variable which is set to %@CHAR[27] in order to avoid using the %@CHAR function that was throwing errors.

2) The regex to remove ansi that i googled used "0-?" as a character range, which doesn't seem to be supported. I changed it to 0-9a-z

3) The whole thing just fails and I don't understand the error code. I could do this in perl in 1 minute, but in 45 minutes I can't do it in TCC and i'm driven to the brink of madness haha

4) It only fails when given a string with ANSI codes in it, which is perplexing to me.

if not defined      ESCAPE           (set                ESCAPE=%@CHAR[27])
if not defined ANSI_ESCAPE           (set           ANSI_ESCAPE=%@CHAR[27][)
function STRIP_ANSI_fail=`%@REREPLACE[%@CHAR[27]\[[0-9a-z] *[ -\/]*[@-~],,%1$]`     %+ rem Char[27] gives an error, huh???
function STRIP_ANSI_TEST=`%@REREPLACE[%ESCAPE\[[0-9a-z] *[ -\/]*[@-~],,%1$]`     %+ rem %ESCAPE% in place of %@CHAR[27] does not give same error

What exactly do you want it to do (in words, no code)?
Strip all ANSI codes from a string.

Because my messaging system titles the window to match the message, but window tab titles don't actually render ansi color (that'd be neat if they did, though).

Also, i have a huge file set-colors.bat which defines every ANSI function i've ever found and used with several aliases to evade my own bad memory. It seems like a reasonable addition to that file, to also add the ability to strip.

If you only want to get rid of color sequences (^e[...m) you can roll your own regex.

Unfortuantely, i also need to remove stylization ansi too. Drat. This removes color but not underlines/italics.

It also doesn't seem to work for removing color in my real world test, unfortunately.

Underline/italics are both SGR (m) sequences. They're removed here.

Hmmm....It may be that your solution has been correct the entire time, actually!

...and that my implementation issue is something with not defining the function right or mistranslating your code when i made the function...

Because currently, i still can't get it to work ...

Escaping (^) in TCC has always thrown me for a loop. This works. If you can explain it, please do.

Further complicating that is that I use ^ as my command separator, but it's not hard to setdos /x-5 to turn that off, though I don't have to do that when it appears in quotes (like with grep).

Still. I tried with two ^^, both with setdos /x-5 and without, and it didn't work! Argh.

Me too. It just seems like REREGEX has quoting issues inside a function that it doesn't have outside of a function.

I wonder if this is a bug.
This kind of thing I'm always tempted to do with a plugin — I'm not a regex maven. How exactly do you define an ANSI sequence? ESC [ followed by a series of printables, ending with a letter?
The regex is actually came from working examples on the internet. I'm positive it's valid. We just can't get TCC to understand it inside of a function.

But to answer your question:

They start with ESC followed by left bracket
The middle contains a mix of single/multiple numbers and semicolons
The end is one of these characters: [fhlmsunABCDEGHK]

There are also some special ones that start with ESC# (no left bracket!) and end in "0", "5", or "6", but these are an edge case that don't come up often. [Unless you're me, but it won't come up for the situation I'm using this for.]
Here's a quick hack. No documentation; I trust you can figure it out on your own.


  • stripansi.zip
    19.9 KB · Views: 8
Here's a quick hack. No documentation; I trust you can figure it out on your own.

It worked! OMG THANK YOU!!!!!!!!!

I hadn't used plugins before today. It was nigh time to learn how!

Loving the plugins you both provided me today! Truly, TCC is best command line! :)
I just came here to say thank you a second time. I'm really happy about this.
No problem. I'd rather do this, than the actual useful work for which I get paid....
No problem. I'd rather do this, than the actual useful work for which I get paid....

HAHAHAHaHaHaHAHHAHAHAH! May I suggest /r/maliciouscompliance and /r/antiwork? lol