TechBuzzPro

Strike‑Through Text in a Console – Is It Possible?

· 5 min read · Apps & Browsers

In this article
  1. Background: why terminals have styling codes
  2. How the strike‑through code works
  3. How to enable strike‑through in popular languages
  4. Choosing the right approach for your environment
  5. Common problems and how to fix them
  6. Alternatives when ANSI strike‑through is unavailable
  7. Comparison of terminal families
  8. Frequently Asked Questions

Yes – most modern terminal emulators support a strike‑through effect by emitting the ANSI SGR code 9 (or the newer 53 for double strike) before the text you want to cross out.

Background: why terminals have styling codes

Command‑line interfaces originally displayed plain ASCII characters, but as developers needed to convey more information (errors, prompts, progress) they adopted the ANSI escape sequence standard defined in the ECMA‑48 specification. The standard includes a set of “Select Graphic Rendition” (SGR) parameters that control colour, intensity, underline, and also strike‑through. Early Unix terminals like VT100 only supported a limited subset (colour, bold, underline), but later terminals such as xterm, GNOME Terminal, Windows Terminal, and iTerm2 added support for the full SGR range, including code 9 (single strike) and 53 (double strike).

How the strike‑through code works

An ANSI escape sequence starts with the ESC character (ASCII 27) followed by ‘[’ and a list of numeric parameters separated by semicolons, ending with ‘m’. For strike‑through you send:

\x1b[9mYour text\x1b[0m

‘\x1b’ is the ESC byte, ‘[9m’ turns on the effect, and ‘[0m’ resets all attributes so subsequent output returns to normal. Some terminals also recognise ‘53’ for a double line, and ‘29’ to turn off strike‑through without resetting other attributes.

Below are concrete examples for the most common scripting and programming environments. All of them ultimately write the same escape sequence to stdout.

  1. Python
    print("\x1b[9mThis is crossed out\x1b[0m")

    For reusable code you can wrap it in a function or use the colorama package, which abstracts the ESC handling on Windows.

  2. Bash / POSIX shell
    echo -e "\e[9mStruck text\e[0m"

    The -e flag enables interpretation of back‑slash escapes. If your shell does not support -e, use printf instead:

    printf "\e[9mStruck text\e[0m\n"
  3. PowerShell
    Write-Host "`e[9mStruck text`e[0m"

    PowerShell uses the back‑tick as the escape character. Recent versions of Windows Terminal and the built‑in console honour SGR 9, but the legacy cmd.exe does not.

  4. C / C++ (POSIX)
    #include <stdio.h>
    int main() {
        printf("\x1b[9mStruck text\x1b[0m\n");
        return 0;
    }
    

    Compile with gcc example.c -o example. On Windows you need a terminal that recognises ANSI codes (Windows Terminal, ConEmu, or enable Virtual Terminal Processing via SetConsoleMode).

  5. Node.js
    process.stdout.write('\x1b[9mStruck text\x1b[0m\n');

    Libraries such as chalk expose a .strikethrough method that handles the escape codes for you.

Choosing the right approach for your environment

  • Native support vs. library – If you are writing a quick script, emitting the raw escape sequence is fine. For larger projects, a library abstracts differences between Windows and Unix and automatically disables the effect when the output is not a TTY.
  • Detecting a capable terminal – Use the TERM environment variable (e.g., xterm‑256color) or query tput colors. Many libraries provide an isTTY check; if false, fallback to plain text.
  • Cross‑platform considerations – Windows 10 build 1511 introduced basic ANSI support, but older versions require the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag via the WinAPI. Libraries like colorama (Python) or ansi (Node) set this flag automatically.

Common problems and how to fix them

Even when you send the correct sequence, you may not see a strike‑through. Typical reasons are:

  1. Terminal does not support SGR 9

    Older emulators (e.g., the default Windows cmd.exe prior to Windows 10) ignore the code. Switch to Windows Terminal, PowerShell 7+, or enable Virtual Terminal Processing.

  2. Output is being piped or redirected

    When stdout is not a TTY (e.g., you pipe to less or a file), most terminals strip escape sequences. Use cat -v to verify the raw bytes, or add a --raw flag if the downstream tool supports it.

  3. Incorrect reset sequence

    If you reset with [0m after every line, you lose other attributes such as colour. Use [29m to turn off only strike‑through, or combine codes: \x1b[31;9m for red struck text.

  4. Font or rendering limitation

    Some bitmap fonts do not include a strike‑through glyph, causing the terminal to fall back to a normal rendering. Switch to a TrueType font like Cascadia Code, Fira Code, or DejaVu Sans Mono.

Alternatives when ANSI strike‑through is unavailable

If you cannot rely on SGR 9, there are two practical work‑arounds:

  • Overstrike using backspace – Print the character, a backspace (\b), and the Unicode combining long‑stroke overlay (U+0336). Example in Bash:
    printf "Hello\b\u0336\n"

    This works in many terminals because the combining character is rendered on top of the previous glyph.

  • Unicode strikethrough characters – Use the “strikethrough” block characters such as U+0335 (short overlay) or U+0336 (long overlay) after each character. It is more verbose but works in plain text files and editors that support Unicode.

Comparison of terminal families

TerminalSupports SGR 9Notes
Linux xterm, GNOME Terminal, KonsoleYesFull colour and style support.
macOS Terminal, iTerm2YesiTerm2 also allows custom colour schemes for strike‑through.
Windows TerminalYesRequires Windows 10 1809 or later.
PowerShell 7 (hosted in Windows Terminal)YesNative support; legacy cmd.exe does not.
Legacy cmd.exeNoOnly basic colour via color command.

Frequently Asked Questions

Can I make strike‑through work in the classic Windows command prompt?

No. The classic cmd.exe console does not interpret ANSI SGR 9. You must use Windows Terminal, PowerShell 7, or enable Virtual Terminal Processing via a custom program.

Is there a way to toggle strike‑through on and off without resetting other styles?

Yes. Use \x1b[9m to enable and \x1b[29m to disable only the strike‑through attribute, leaving colours and boldness intact.

Do all fonts display the strike‑through line correctly?

Most modern monospaced TrueType fonts render the line as part of the glyph. Bitmap or very old fonts may omit it, resulting in invisible styling. Switching to a recent font solves the issue.

Can I use strike‑through in a log file that will later be viewed in a terminal?

Yes, but only if the log viewer respects ANSI codes. Tools like less -R or cat in a capable terminal will show the effect; plain editors will display the raw escape characters.

What is the difference between SGR 9 and SGR 53?

SGR 9 draws a single horizontal line through the text. SGR 53 draws a double line (often called “double strike”). Both are defined in ECMA‑48, but double strike is less widely supported.

Is there any security risk in printing ANSI escape codes?

Printing escape codes is safe when the output is confined to a trusted terminal. However, malicious sequences can manipulate cursor position or clear the screen, so never render untrusted data without sanitising or stripping ESC characters.