Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
0
votes
1
answers
72
views
Need Help Understanding script that reads output of cursor position ANSI escape code
I have this code, which does what I want, but I don't entirely understand it: ``` #!/usr/bin/env bash echo -ne "\033[6n" # Ask the terminal to print out the cursor's position # The response looks like ^[[n;mR - where n = row, m = col read -s -d\[ garbage # Read the response silently, discarding the...
I have this code, which does what I want, but I don't entirely understand it:
#!/usr/bin/env bash
echo -ne "\033[6n" # Ask the terminal to print out the cursor's position
# The response looks like ^[[n;mR - where n = row, m = col
read -s -d\[ garbage # Read the response silently, discarding the first part of the response, leaving n;mR
read -s -d ';' row # Read some more, silently, until we get to the character ';', storing what we read in the variable "row"
read -s -d R col # Read some more, silently, until we get to the character 'R', storing what we read in the variable "col"
echo "The (row,col) coords are ($row,$col)"
If I run echo -ne "\033[6n"
at a shell prompt, I get kind of what the comments say I'll get, but not exactly:
$ echo -ne "\033[6n"
^[[11;1R$ ;1R
If I put that in a script (I'll call it "splurt.sh"), and run the script, the results are the same:
#!/usr/bin/env bash
echo -ne "\033[6n"
echo
$ ./splurt.sh
^[[16;1R$ ;1R
So at this point, I just accept that the line works, even if it's not displaying as I would expect it to.
But then comes the next line:
read -s -d\[ garbage # Read the response silently, discarding the first part of the response, leaving n;mR
I kind of understand that "read" reads input from the console (keyboard, usually, I would think), but somehow it seems to be reading from the console's monitor?
I understand the "-s" means to read the console silently, but I don't quite comprehend that. I put this into my short script:
#!/usr/bin/env bash
echo -ne "\033[6n"
read -s -d\[ garbage
echo
and I get:
$ ./splurt.sh
$ 25;1R
If I leave out the "-s", I get:
$ ./splurt.sh
^[[27;1R
$ 27;1R
So I can see that the "-s" does prevent the echo of "^[[27;1R" to the screen, but I don't grok that, because I thought the echo -n e "\033[6n"
had already printed that out to the screen (although apparently not, because I don't see it until the "read" reads it?).
I do understand (finally! I understand something!) that the "-d" only reads up to the specified delimiter (usually a newline, as I understand it, unless specified by the "-d"), and that what gets read is placed into the variable "garbage" (which is henceforth ignored), but I don't understand how "\[" causes the read to go all the way through "^[[", instead of stopping at "^[", to leave "[27:1R" instead of what it actually leaves of "27:1R".
And then the rest of the script I have a fairly good grip on, methinks.
Anyone want to take a stab at helping me to understand better what's going on? Thanks!
user153064
(13 rep)
Jul 8, 2025, 01:28 PM
• Last activity: Jul 8, 2025, 08:05 PM
6
votes
2
answers
184
views
How can I pipe text that contains ANSI movement characters ESC[xC and ESC[xD and output the text formatted accordingly
I have text files that include the ANSI movement codes `ESC[xC` and `ESC[xD`. I want to filter these out but with each line output with those codes taken into account. Consider the line: this cat is greenESC[12DdogESC[4Cwhite I would like this piped out as this dog is white Above `ESC` means the esc...
I have text files that include the ANSI movement codes
ESC[xC
and ESC[xD
. I want to filter these out but with each line output with those codes taken into account.
Consider the line:
this cat is greenESC[12DdogESC[4Cwhite
I would like this piped out as
this dog is white
Above ESC
means the escape code \0x1b
(or \033
).
Scott
(103 rep)
Oct 24, 2023, 07:54 AM
• Last activity: Jun 10, 2025, 07:55 PM
0
votes
0
answers
50
views
Strip ansi characters on stringified number
I wrote function to strip the ansi invisible characters from my string. ``` strip_ansi() { pattern='s/\x1B\[[0-9;]*[a-zA-Z]//g' echo -e "$1" | sed "$pattern" } ``` After (supposedly) stripping with above function, I applied a number filter to see if there was still something left `var_name="${var_na...
I wrote function to strip the ansi invisible characters from my string.
strip_ansi() {
pattern='s/\x1B\[[0-9;]*[a-zA-Z]//g'
echo -e "$1" | sed "$pattern"
}
After (supposedly) stripping with above function, I applied a number filter to see if there was still something left var_name="${var_name//[!0-9]/}"
, and there was still.
- **Innocent input**: '17'
- **command output "$var_name" | cat -A"**: '^[[?25l^[[9;1H^[[10;1H^[[11;1H^[[12;1H^[[13;1H^[[14;1H^[[9;1H^[[10;1H^[[11;1H17$'
- **Output of function 'strip_ansi' and '"${var_name//[!0-9]/}"'**: '2517'
Do you have any suggestion to strip ansi characters in a definite way?
Bruno Lobo
(201 rep)
Feb 22, 2025, 06:20 PM
• Last activity: Feb 22, 2025, 06:45 PM
9
votes
3
answers
4343
views
How to convert escape sequences to text while preserving display format?
I have a text file that contains (ANSI ?) escape sequences: [![raw file][1]][1] When I `cat` the file I get formatted output: [![formatted output][2]][2] How do I save / pipe the output of the text file to a new file so that the control codes are removed but the formatting is preserved? In other wor...
I have a text file that contains (ANSI ?) escape sequences:
When I
How do I save / pipe the output of the text file to a new file so that the control codes are removed but the formatting is preserved?
In other words, I want to convert / export the contents of the file to a new file while retaining the intended display format (e.g. like one would get when using

cat
the file I get formatted output:

less -r
).
I need an automated way to do this so that the output can be logged and sent as an email notification.
Here is the base64 output of the file in the above screenshots (use base64 -d
to decode it):
G1tIG1syShtbMTsxSA0KG1s0OzRIICAgICAgICAgICAgICAgICAgICAgICAgICBDT05GSURFTlRJ
QUwgSU5GT1JNQVRJT04bW0sbWzU7NEgbW0sbWzY7NEgbW0sbWzc7NEhUaGlzIHdvcmsgY29udGFp
bnMgdmFsdWFibGUsIGNvbmZpZGVudGlhbCwgYW5kIHByb3ByaWV0YXJ5IGluZm9ybWF0aW9uLhtb
SxtbODs0SERpc2Nsb3N1cmUsIHVzZSwgb3IgcmVwcm9kdWN0aW9uIGlzIGdvdmVybmVkIGJ5IHlv
dXIgTGljZW5zZSBBZ3JlZW1lbnQuG1tLG1sxMDs0SBtbSxtbMTE7NEhUaGlzIHVucHVibGlzaGVk
IHdvcmsgaXMgcHJvdGVjdGVkIGJ5IHRoZSBsYXdzIG9mIHRoZSBVbml0ZWQgU3RhdGVzIGFuZBtb
SxtbMTI7NEhvdGhlciBDb3VudHJpZXMuICBUaGUgd29yayB3YXMgY3JlYXRlZCBpbiAxOTg4IGFu
ZCByZXZpc2VkIGluIDE5OTQuICAbW0sbWzEzOzRISWYgcHVibGljYXRpb24gb2NjdXJzLCB0aGUg
Zm9sbG93aW5nIG5vdGljZSBzaGFsbCBhcHBseTobW0sbWzE0OzRIG1tLG1sxNTs0SBtbSxtbMTY7
NEggICBDb3B5cmlnaHQgMTk4OCwxOTk0LiBBbGwgcmlnaHRzIHJlc2VydmVkLhtbSxtbMTc7NEgb
W0sbWzE4OzRIG1tLG1sxOTs0SFRoaXMgQ29weXJpZ2h0IG5vdGljZSBhbmQgb3RoZXIgY29weXJp
Z2h0IG5vdGljZXMgaW5jbHVkZWQgaW4gdGhlIG1hY2hpbmUbW0sbWzIwOzRIcmVhZGFibGUgY29w
aWVzIG11c3QgYmUgcmVwcm9kdWNlZCBvbiBhbGwgYXV0aG9yaXplZCBjb3BpZXMuG1tLG1syMTs0
SBtbSxtbMjI7NEhUaGlzIGlzIGEgcmVnaXN0ZXJlZCB0cmFkZW1hcmsuG1tLDQo=
mvanle
(385 rep)
Jan 13, 2021, 04:28 AM
• Last activity: Sep 23, 2024, 03:43 PM
1
votes
2
answers
2635
views
Bash - ANSI color code for purple background with bold black text
What is the ANSI color code for a purple (or dark purple) background with black bold text (foreground). As an example, here is cyan with black bold text: `echo -e "\e[30;1;46mExample\e[0m"` [![Cyan background with bold black text][1]][1] After consulting a number of ANSI color code lists and example...
What is the ANSI color code for a purple (or dark purple) background with black bold text (foreground).
As an example, here is cyan with black bold text:
After consulting a number of ANSI color code lists and examples, I can't figure out how to do the same with a purple background. The closest I can come up with is a magenta background with black, bold text, by using
echo -e "\e[30;1;46mExample\e[0m"

30;1;45m
:

philosophie
(875 rep)
May 6, 2019, 12:50 AM
• Last activity: Aug 2, 2024, 01:32 PM
2
votes
2
answers
2024
views
How to display rgb hex colors on terminal using python?
Using ANSI escape sequence, I can change the color of text that python prints. The example below prints "Hi World" in light blue. ```python #!/usr/bin/python3 print(f'\033[38;5;117mHi World') ``` While this works I am limited to just 256 colors. Are there any terminals or extensions that allow me to...
Using ANSI escape sequence, I can change the color of text that python prints. The example below prints "Hi World" in light blue.
#!/usr/bin/python3
print(f'\033[38;5;117mHi World')
While this works I am limited to just 256 colors. Are there any terminals or extensions that allow me to print colors in hex triplets #000000-#FFFFFF
using python?
un31389
(21 rep)
Aug 12, 2023, 10:44 PM
• Last activity: Jul 30, 2024, 11:16 PM
0
votes
0
answers
372
views
Color Code Specific Words/Phrases in a Unix/Linux Terminal Regardless of the Command Used that Prints that Word/Phrase
Is there a way in Unix/Linux to always color-code specific words/phrases no matter what made those words/phrases appear as output on the terminal? I know you can write scripts/programs to print out any colors you want, but I was wondering if it could happen no matter how that word/phrase got printed...
Is there a way in Unix/Linux to always color-code specific words/phrases no matter what made those words/phrases appear as output on the terminal? I know you can write scripts/programs to print out any colors you want, but I was wondering if it could happen no matter how that word/phrase got printed to the terminal. You can use commands to print and add colors that way too, but is it possible to color code them no matter how they get printed to the terminal?
Sacred
(1 rep)
Jan 13, 2023, 05:30 AM
• Last activity: Jul 10, 2024, 01:57 AM
0
votes
0
answers
132
views
Node.js recently started emitting all stderr in red text. How to handle this cleanly?
I use ANSI colorized text output in logs and other output that I emit on stderr in some of my programs. Since I've been doing this kind of thing for years, I am pretty familiar with the capabilities. The trouble with the change in behavior that node.js introduced is that now all stderr output is get...
I use ANSI colorized text output in logs and other output that I emit on stderr in some of my programs. Since I've been doing this kind of thing for years, I am pretty familiar with the capabilities.
The trouble with the change in behavior that node.js introduced is that now all stderr output is getting wrapped in red foreground (text) color codes, and it's generally not practical or possible to refactor entire applications to become aware of whether this thing is going to happen to the stderr stream or not. If I am emitting some output with some colored text, e.g.
Error:\e[33m message\e[39m at position
That writes "Error: message at position"
where "message" is in yellow. When node.js causes this whole thing to be then wrapped in \e[31m
\e[m
(or \e39m
), what I will end up with on the terminal is "Error:" in red, "message" in yellow, and "at position" in the normal text color. This is unexpected and looks wrong, but basically it's impossible to fix unless I change the behavior of my code to know it is emitting this on stderr and if it knows that the version of node running the code is new enough it should change to red text color instead of resetting the text color.
One way to express this issue is the fact that ANSI escapes are a stream protocol instead of being a tree/stack like HTML or something, whereby the yellow color could be popped leaving the base red color there.
One approach I could take is to change to not use text colors in stderr output, as the ansi states for background color, boldedness, italicness, etc. are not going to mess with the red text color when they get reset with their appropriate reset codes.
This is still not ideal though, because I definitely have situations where I want the colorized util.inspect strings in certain error outputs...
Steven Lu
(2422 rep)
Jun 19, 2024, 04:47 PM
4
votes
2
answers
3954
views
In this \033[01;32m VT100 style ANSI Escape Sequences what is the 01; and the m?
This came from ~/.bashrc PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' Notice the `\033[01;32m` I know `\033[` is a Control Sequence Introducer. I know `32` is the color code for green. But, what are the `01;` and `m`? Which part of [ANSI e...
This came from ~/.bashrc
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
Notice the
\03301;32m
I know \033[
is a Control Sequence Introducer.
I know 32
is the color code for green.
But, what are the 01;
and m
?
Which part of [ANSI escape code does \033[01;32m
belongs to.
Sameh Ramzy Labib
(133 rep)
May 24, 2020, 04:32 AM
• Last activity: Mar 3, 2024, 10:46 PM
0
votes
3
answers
793
views
How to interpret \e[H\e[2J ANSI escapes sequence from Linux terminal?
I'm asking again the question that was asked on StackOverflow with no answers I event added a bounty without an effect (I've just deleted that one). I think that people here may have better knowledge about what is going on. I'm working on a better ANSI escape code interpreter for my JavaScript libra...
I'm asking again the question that was asked on StackOverflow with no answers I event added a bounty without an effect (I've just deleted that one). I think that people here may have better knowledge about what is going on.
I'm working on a better ANSI escape code interpreter for my JavaScript library but having a problem with processing the
top
command (using a script
command that dumps terminal output into a file).
I have a sample file from a MacOS that use alternative screen ESC [ ? 1049 h
and l
as [documented here](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h4-Functions-using-CSI-_-ordered-by-the-final-character-lparen-s-rparen:CSI-?-Pm-h.1D0E) .
The problem is that the Linux version of the top
command doesn't use an alternative screen only: \e[H\e[2J
sequence which is: goto 1,1 and clear the screen. From my understanding, this should literally wipe everything that was printed on the terminal before that command. This is what I do in my parser/interpreter. But this is not what the Linux terminal is doing (I use an XFce terminal). It somehow clears part of the screen and scrolls down the page.
**So my question is what are the exact steps (algorithm) to process the top command (\e[H\e[2J
sequence) to render it like on a Linux terminal? I need to have a single string as the output of the session as it looks on my terminal.**
Here is a sample output of the script command: https://jcubic.pl/screen_dump_linux_top.txt
I have testing code on [CodePen](https://codepen.io/jcubic/pen/vYjbqwr?editors=0010) :
that use this code:
$.terminal.format($.terminal.overtyping($.terminal.from_ansi(text)));
if you look at the output of the top command on Linux. You will see that there is no output before the command. In comparison to the macOS file that prints everything (I know that the real terminal doesn't print an alternative screen when switching back to the primary screen, but I decided that I want that output there, maybe I add an option to toggle that).
I know that I can just check the terminal emulator source code, but it will probably take a lot more time, than just asking this.
jcubic
(10310 rep)
Nov 8, 2022, 09:26 PM
• Last activity: Nov 9, 2023, 11:40 AM
1
votes
1
answers
1014
views
How to configure ANSI text colors for NixOS console?
I'm on NixOS-unstable (as of 2023 Oct 1st). How can I configure the text colors for the default NixOS console, as the terminal supports ANSI? I tested what the colors look like by installing [`text-ansi`][1] on Haskell (GHC) and then putting some code snippets on GHCi, like `putStrLn $ red "Hello, w...
I'm on NixOS-unstable (as of 2023 Oct 1st). How can I configure the text colors for the default NixOS console, as the terminal supports ANSI?
I tested what the colors look like by installing
text-ansi
on Haskell (GHC) and then putting some code snippets on GHCi, like putStrLn $ red "Hello, world!"
.
I want to configure the colors because it's slightly hard to visually distinguish between some colors, red
and brightRed
for example. I'm gonna be annoyed when, for example, playing NetHack. Here's how I want to configure the colors:
* black
= #18270f
* red
= #ff0111
* green
= #2e8011
* yellow
= #b25e3a
* blue
= #0f87fe
* magenta
= #c900ff
* cyan
= #41ff1a
* white
= #eca2ff
* brightBlack
= #000074
* brightRed
= #ff9700
* brightGreen
= #b6ff00
* brightYellow
= #fff86b
* brightBlue
= #00e2ff
* brightMagenta
= #ff0084
* brightCyan
= #00ffa5
* brightWhite
= #e4fffd
(Yes, I know, some colors don't quite fit literally, but believe me, I acquired them from savage computation.)
Dannyu NDos
(113 rep)
Oct 1, 2023, 06:28 AM
• Last activity: Oct 1, 2023, 07:31 AM
0
votes
1
answers
213
views
Why do terminals report their background color as rgb:xxxx/yyyy/zzzz — what format is that?
If I set my terminal background to the color [`#1a2b3c`](https://www.colorhexa.com/1a2b3c) and then query it with `printf '\e]11;?\a'` I get back `]11;rgb:1a74/2b98/3cb6` What encoding are these supposed values of red `1a74` , green `2b98` , and blue `3cb6`? If I just take the first two characters o...
If I set my terminal background to the color [
#1a2b3c
](https://www.colorhexa.com/1a2b3c) and then query it with printf '\e]11;?\a'
I get back ]11;rgb:1a74/2b98/3cb6
What encoding are these supposed values of red 1a74
, green 2b98
, and blue 3cb6
?
If I just take the first two characters of each segment (1a
74/2b
98/3c
b6) then I get back the original color of 1a2b3c
— so what are the 74
, 98
, and b6
for?
balupton
(634 rep)
Aug 25, 2023, 09:40 PM
• Last activity: Aug 26, 2023, 08:12 AM
6
votes
1
answers
7018
views
What is the ANSI escape equivalent of `tput sgr0`?
To avoid a subshell, what are the ANSI escape equivalents of `tput sgr0` for an ANSI-compatible terminal?
To avoid a subshell, what are the ANSI escape equivalents of
tput sgr0
for an ANSI-compatible terminal?
Tom Hale
(32892 rep)
Sep 21, 2018, 09:38 AM
• Last activity: Aug 13, 2023, 01:44 PM
3
votes
2
answers
1816
views
How can I save a text file with all command line inputs and outputs without saving the ANSI characters?
I am running the latest mac OS and using zshell, and having trouble writing a shell script. I do most of my work from the command line, and wanted to have a bash script automatically save and log all of my input commands and output. My most obvious choice was the bash "script" command, generally ini...
I am running the latest mac OS and using zshell, and having trouble writing a shell script. I do most of my work from the command line, and wanted to have a bash script automatically save and log all of my input commands and output.
My most obvious choice was the bash "script" command, generally initiated on the command line as
script -a .txt
I wrote a shell script, log.sh
, whose contents are below:
#! /bin/sh
echo "Welcome to the jungle."
# get date
now=$(date +%Y_%m_%d)
# create filename
fname='log_'$now'.txt'
# file save location
floc=~/path/to/directory/$fname
# start script
script -a -q $floc
This starts a script where the filename is today's date and saved in a designated directory. This works fine, except, however, the contents that are written to the file. Instead of just the plan text input/outputs I would expect, I get what I believe to be ANSI characters that make the file difficult to read.
Here is what my command line looks like:
~$ bash log.sh
Welcome to the jungle.
~$ echo "Hello world"
Hello world
~$ exit
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
And how it is recorded in the log file:
[1m[7m%[27m[1m[0m
]7;file:///Users/
[0m[27m[24m[J[36m [38;5;246m~[39m$ [K[?2004heecho "Hello world! "[?2004l
Hello world
[1m[7m%[27m[1m[0m
]7;file:///Users/
[0m[27m[24m[J[36m [38;5;246m~[39m$ [K[?2004heexit[?2004l
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
I am aware there are a few ways to handle the ANSI keys after creating the file, such as using the cat
command to display the text, or a perl script to remove the keys; However, using the cat
command is annoying when I am trying to look through a large file, and having to cleanup the file manually is tedious. Is there any way I can record my command line inputs and outputs **without** the ANSI keys?
Thank you!
Nicole I.
(31 rep)
Apr 4, 2023, 08:26 PM
• Last activity: Apr 6, 2023, 02:12 PM
-1
votes
1
answers
141
views
ircII ansi escape sequences
REHASH from my original question. I'm trying to get VT100/ANSI escape codes working in an irc client (ircII). What facilitates this, is the ```screen.scr``` file. The apparent problem, is that I cannot get a working terminal working with the pseudo terminal. ```stty``` yields this: ```stty: standard...
REHASH from my original question.
I'm trying to get VT100/ANSI escape codes working in an irc client (ircII).
What facilitates this, is the
.scr
file.
The apparent problem, is that I cannot get a working terminal working with the pseudo terminal.
yields this:
: standard input: Inappropriate ioctl for device
So, I believe if I solve this,and get a working terminal, I solve my initial problem.
Thank you.
ignatius
(411 rep)
Mar 23, 2023, 08:27 PM
• Last activity: Apr 6, 2023, 01:49 AM
0
votes
1
answers
224
views
i3 status bar with ANSI *input*
I want to show a text having ANSI escape sequences in a status bar. The status text is generated by third party tools, that manage themselves the ANSI colors (and hyperlinks, albeit these may be left apart). The status bar is to be displayed in the i3 windows manager (Linux). I saw at least three ap...
I want to show a text having ANSI escape sequences in a status bar.
The status text is generated by third party tools, that manage themselves the ANSI colors (and hyperlinks, albeit these may be left apart). The status bar is to be displayed in the i3 windows manager (Linux).
I saw at least three approaches to solve this problem, but I failed at each of them:
1) Find a status bar that renders the ANSI colors in its *input*.
2) Find a way to convert ANSI colors to whatever markup language the status bar uses.
3) Display a terminal window as status bar.
# ANSI input
I searched for status bars around i3 and found many (i3status, dzen2, xmobar, polybar, lemonbar, i3cat, conky, barmaid, sbar, yabar).
To the extent of my understanding, none of them allow any formating in a text input, apart from conky *in text mode* (which does not support unicode and crashes regularly).
dwm seems to mention some patch allowing ANSI input related to a status bar, but I failed understanding how to build them up.
# ANSI to markup
Reading the docs, all of the status bar I found define their own markup language at the configuration level. (I confess I find that a bit weird, why not use the ubiquitous HTML? Just a subset of it would already allow for leveraging powerful tools, like pandoc. Plus, there are tools to convert ANSI colors to HTML spans with a color tag.)
All of them see input as text (or numerical values) waiting to be formatted at the configuration step. In my case, the tool(s) generating the input is deciding itself whether to show (or not) information, and how to color it. Implementing the complex features it provides in the status bar itself would be cumbersome.
Because they use their own markup language, I found no existing tool able to convert into it. Because it is at the configuration level and not the input, it seems complicated to setup such a pipeline.
# Terminal as bar
This may be the most promising approach.
It is very easy to call a command every second, thanks to
watch --no-wrap --no-title --colors
, providing the "status update" feature.
Technically, it seems feasible to play with *xdotool* to make a X11 terminal window behave as a status bar, as mentioned in this answer , but I failed to make it work under i3.
Edit: **How to show a text having ANSI escape colors in an i3 status bar (with one or the other method)?**
nojhan
(109 rep)
Mar 26, 2023, 09:43 AM
• Last activity: Mar 27, 2023, 09:11 AM
1
votes
1
answers
554
views
ANSI escape sequences in PS1 cause incorrect length calculation
I have a `PS1` set up in my `.zshrc` which includes multiple ANSI escape sequences. An equivalent definition works nicely in Bash, but in Zsh (v5.8.1) it seemingly causes the shell to calculate the width of the prompt incorrectly. As a consequence, when entering longer commands, the command line *su...
I have a
PS1
set up in my .zshrc
which includes multiple ANSI escape sequences. An equivalent definition works nicely in Bash, but in Zsh (v5.8.1) it seemingly causes the shell to calculate the width of the prompt incorrectly. As a consequence, when entering longer commands, the command line *suddenly vanishes*, and I am typing blind. And in cases it leads to the cursor being placed on the next line after the prompt, even though the PS1
does not include a line break.
I am already using the [\001…\002
escape sequences](https://superuser.com/a/301355/2269) that are used by readline to adjust the length calculation around each ANSI escape sequence. Unfortunately this seems to be insufficient for Zsh (in the example below they don’t seem to have *any* effect, but in my real, more complex PS1
, they seem to improve the situation *somewhat*, at least).
Here’s an example to demonstrate the issue (the comment underlines the ANSI escape sequence parts that I believe need to be bracketed by \001…\002
):
PS1=$'\001\e[38;2;1;1;1m\e[48;5;250m\002}\001\e[38;5;250m\e[48;2;1;1;1m\002} '
# \001--------------------------\002 \001--------------------------\002
With this prompt, and using an 80 column terminal, after I type 25 characters, the entire command including prompt vanishes. Using backspace does not make the characters reappear, but instead makes the cursor go to the previous line.
[Here’s an Asciinema recording of this behaviour.](https://asciinema.org/a/2GjmnytLQ01UvoqCNbdqwVSxr)
For this demo I have disabled all other customisation of my shell.
(In reality I am using the nf-pl-left_hard_divider
, U+E0B0, from [NerdFont](https://www.nerdfonts.com/cheat-sheet) instead of the }
s, but this does not impact the issue.)
What am I doing wrong? How are ANSI escape sequences supposed to be used inside a Zsh prompt definition?
Konrad Rudolph
(3839 rep)
Feb 4, 2023, 04:48 PM
• Last activity: Feb 4, 2023, 05:05 PM
0
votes
3
answers
419
views
Linux 9 commands send ANSI/color sequences to monochrome terminal
I'm using a Wyse50 Terminal emulation on Rocky Linux 9. Man pages, help screens, etc., are unusable because the system outputs attribute and color codes that the terminal emulator doesn't understand; so, for example, `man cp` reads like this: ``` 1mSYNOPSIS0mm 1mcp 22m[4mOPTION24m]... [4m-T24m]...
I'm using a Wyse50 Terminal emulation on Rocky Linux 9. Man pages, help screens, etc., are unusable because the system outputs attribute and color codes that the terminal emulator doesn't understand; so, for example,
man cp
reads like this:
1mSYNOPSIS0mm
1mcp 22m[4mOPTION24m]... [4m-T24m] 4mSOURCE24m 4mDEST0mm
1mcp 22m[4mOPTION24m]... 4mSOURCE24m... 4mDIRECTORY0mm
1mcp 22m[4mOPTION24m]... 4m-t24m 4mDIRECTORY24m 4mSOURCE24m...m m
1mDESCRIPTION0mm
I've updated the latest terminfo packages and did an infocmp between the latest wy50 and the wy50 from a Linux 4 where everything worked fine.
I see nothing in profile, bash_profile, bashrc or .bashrc that would set any color parameters.
What am I missing? (other than a legacy application that belongs in the prior century)
Darrel Barton
(1 rep)
Dec 20, 2022, 06:23 PM
• Last activity: Jan 11, 2023, 01:45 AM
0
votes
1
answers
478
views
How does `tput` detect color support
I am trying to make my shell script supporting as much terminals as possible, that is, adding ANSI colors, bold and dim only when supported. However, I want to detect the number of colors supported. We can use `tput colors`. However, I find that some systems that are stripped down to minimal does no...
I am trying to make my shell script supporting as much terminals as possible, that is, adding ANSI colors, bold and dim only when supported. However, I want to detect the number of colors supported. We can use
tput colors
.
However, I find that some systems that are stripped down to minimal does not have it installed. So, I want to implement it in shell.
How does tput colors
work? Any equivalent of tput colors
with POSIX commands? Please help and answer.
sudoer
(65 rep)
Jan 6, 2023, 12:44 PM
• Last activity: Jan 6, 2023, 01:33 PM
4
votes
0
answers
607
views
What is the meaning of ESC ( B ANSI escape code?
I'm working on the ANSI escape codes interpreter and I have this escape code from the `top` command. ``` [K [7m PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND (B[m[39;49m[K (B[m 23805 kuba 20 0 41,1g 247308 70936 S 22,2 1,5 22:40.52 slack (B[m[39;49m[K (B[m[1m 213109 kuba 20 0 226580 4244 335...
I'm working on the ANSI escape codes interpreter and I have this escape code from the
top
command.
[K
[7m PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND (B[m[39;49m[K
(B[m 23805 kuba 20 0 41,1g 247308 70936 S 22,2 1,5 22:40.52 slack (B[m[39;49m[K
(B[m[1m 213109 kuba 20 0 226580 4244 3352 R 16,7 0,0 0:00.06 top (B[m[39;49m[K
(B[m 1541 root 20 0 2636312 102624 47028 S 11,1 0,6 407:36.60 Xorg (B[m[39;49m[K
(B[m 8465 kuba 20 0 849744 46016 27724 S 11,1 0,3 171:40.22 pavucontrol (B[m[39;49m[K
(B[m 2084 kuba 20 0 1426852 74796 51248 S 5,6 0,5 182:40.85 xfwm4 (B[m[39;49m[K
(B[m 86899 kuba 20 0 1129,7g 525864 132432 S 5,6 3,3 119:14.01 chrome (B[m[39;49m[K
(B[m 1 root 20 0 173284 16232 9628 S 0,0 0,1 0:22.51 systemd (B[m[39;49m[K
I was searching this document [XTerm Control Sequences](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Controls-beginning-with-ESC) and found that it's USASCII character encoding. But why it's only the output of a top command and why it's on every single line?
Can this escape code be ignored when parsing and displaying on the web? Or is it something different and used for a different purpose?
I've also found this document [Line Draw Characters without VGA Fonts](https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x622.html) but here (B is used as the ending of line drawing, in my case, it's used for a different purpose
jcubic
(10310 rep)
Oct 24, 2022, 03:41 PM
• Last activity: Oct 24, 2022, 04:12 PM
Showing page 1 of 20 total questions