Strike‑Through Text in a Console – Is It Possible?
In this article
- Background: why terminals have styling codes
- How the strike‑through code works
- How to enable strike‑through in popular languages
- Choosing the right approach for your environment
- Common problems and how to fix them
- Alternatives when ANSI strike‑through is unavailable
- Comparison of terminal families
- 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.
How to enable strike‑through in popular languages
Below are concrete examples for the most common scripting and programming environments. All of them ultimately write the same escape sequence to stdout.
- Python
print("\x1b[9mThis is crossed out\x1b[0m")For reusable code you can wrap it in a function or use the
coloramapackage, which abstracts the ESC handling on Windows. - Bash / POSIX shell
echo -e "\e[9mStruck text\e[0m"
The
-eflag enables interpretation of back‑slash escapes. If your shell does not support-e, useprintfinstead:printf "\e[9mStruck text\e[0m\n"
- 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.exedoes not. - 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 viaSetConsoleMode). - Node.js
process.stdout.write('\x1b[9mStruck text\x1b[0m\n');Libraries such as
chalkexpose a.strikethroughmethod 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
TERMenvironment variable (e.g.,xterm‑256color) or querytput colors. Many libraries provide anisTTYcheck; 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_PROCESSINGflag via the WinAPI. Libraries likecolorama(Python) oransi(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:
- Terminal does not support SGR 9
Older emulators (e.g., the default Windows
cmd.exeprior to Windows 10) ignore the code. Switch to Windows Terminal, PowerShell 7+, or enable Virtual Terminal Processing. - Output is being piped or redirected
When stdout is not a TTY (e.g., you pipe to
lessor a file), most terminals strip escape sequences. Usecat -vto verify the raw bytes, or add a--rawflag if the downstream tool supports it. - Incorrect reset sequence
If you reset with
[0mafter every line, you lose other attributes such as colour. Use[29mto turn off only strike‑through, or combine codes:\x1b[31;9mfor red struck text. - 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
| Terminal | Supports SGR 9 | Notes |
|---|---|---|
| Linux xterm, GNOME Terminal, Konsole | Yes | Full colour and style support. |
| macOS Terminal, iTerm2 | Yes | iTerm2 also allows custom colour schemes for strike‑through. |
| Windows Terminal | Yes | Requires Windows 10 1809 or later. |
| PowerShell 7 (hosted in Windows Terminal) | Yes | Native support; legacy cmd.exe does not. |
| Legacy cmd.exe | No | Only 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.