How to set cursor color in bash shell [duplicate] - bash

This question already has answers here:
How to change the output color of echo in Linux
(33 answers)
Closed 6 years ago.
I have found that I can get my cursor to blink by including the following instruction in my .bashrc file:
echo -ne "\x1b[1 q"
But I also want to change the color of the blinking cursor. I know that my terminal supports color because I can set the prompt colors and print text in color, but I just can't change the cursor color. Any suggestions?
I'm adding the following comment, that I'm aware of how to change the color of text that is displayed on the terminal, but that is not the same as changing the color of the the cursor. So my question is not addressed in that other question.
But I did find a workaround in my terminal emulator software, provided below.
Thanks for the feedback, especially the part about making the selection of the proper escape codes portable across terminal types.

I found afterwards that I can change the cursor color, not in bash, but in the terminal emulator program. In my case that program is MobaXTerm. I discovered the following sequence: Settings - Terminal - Cursor. At that point, selecting a cursor color causes the cursor in the bash shell to be displayed in the desired color. So now in the files that I edit using vim in the bash environment in my xterm window I see a blinking green block cursor, which is what I needed.
Please check the following short demonstration clip: Blinking Green Block Cursor in bash and vim
Attaining that was what my question was about, not about how to display colored text on the screen, which as was pointed out is already answered elsewhere. So my question is not a duplicate. Anyway, it turned out that my xterm emulation software Mobaxterm allowed me to set the cursor color whereas the escape sequence in my .bashrc file allowed me to get it to blink.

Don't use echo escape characters. Use printf like so:
printf '%b' '\e]12;red\a'

Related

How do programs like man, screen, and vim create temporary overlays?

Several *NIX commands, such as screen, man, vim and others, create a temporary canvas/screen/overlay in a shell environment. When such programs execute, they cover or hide whatever content was displayed in the terminal before — almost like a "full screen" mode, within the terminal window. When they terminate, however, they reveal or restore whatever had been on the terminal before.
In the example below, I create some filler text on the screen, then invoke man bash. The man page opens up and covers all other characters on the terminal display. When I close the man page, the characters that had been covered up are again shown.
Before
While an example full-screen program is running
After
I would expect that programs writing to stdout/stderr could accomplish the first step (replacing the content of the terminal with program-specific content), but then it would produce a ton of text that I could scroll through, and therefore couldn't do the second step: restoring the contents of the terminal. That means that somehow either the program memorizes the previous contents of the screen and re-outputs them (I doubt it?), or it creates some sort of sub-window within a terminal and something else keeps track of the previous contents of the terminal.
My Question
How can I accomplish that behavior in my own program and/or script?
Perhaps I should use curses/ncurses, tput, termcap/terminfo, or ANSI escape sequences?
Update:
This revised question is essentially the same as https://unix.stackexchange.com/questions/27941/show-output-on-another-screen-and-return-to-normal-when-done. (I hadn't found it when I had written this question despite lots of searching.) The difference is that my question is more general (any language) whereas that question is specific to Bash. The answers to both questions are essentially the same. If it's too similar to a question on another site, feel free to close it here for that reason.
How do these programs accomplish that behavior?
ANSI escape sequences. Try running this script:
#/bin/bash -
tput smcup
echo 'Hello world!'
sleep 3
tput rmcup
Using infocmp, you can see underlying sequences that create this overlaying effect, e.g:
$ infocmp -1 | grep 'rmcup\|smcup'
rmcup=\E[?1049l\E[23;0;0t,
smcup=\E[?1049h\E[22;0;0t,
is this behavior shell-dependent or system-dependent?
None, it depends on whether the terminal emulator supports save/restore operations.

Clear terminal screen with a two line prompt and keep both lines visible

I have a two line prompt like below. Is there a command like clear that will clear the screen but keep both lines of the prompt visible?
~/current/directory git-branch*
$ echo 'hello...
short: no, there's no such command.
long: you could write a script. Here's a brief introduction to the topic.
The clear program clears the whole screen. Your shell will draw a new prompt (at the top of the newly cleared screen). But suppose that you wanted to clear parts of the screen before your command completes, and returns to the shell.
Most terminals that you would use support the ANSI escape sequence which clears from the current cursor location to the end of the screen, which is the terminfo capability ed:
clr_eos ed cd clear to end of
screen (P*)
shown by infocmp as ed=\E[J.
You can use it in a script, e.g., using tput:
tput ed
This is one of the areas where "ansi.sys" differs from the ANSI standard (actually ECMA-48, see 8.3.29 ERASE IN PAGE). ansi.sys clears the entire screen when it receives
printf '\033]J'
Some people hard-code this into scripts, and assume that "ansi.sys" matches the standard. See for example How do I get color with VT100? in the ncurses FAQ.
Noting a comment about how to test this: likely there is nothing on your screen below the prompt. So typing tput ed may appear to do nothing. As noted above, it clears below the cursor. If you want to clear above the (2-line) prompt, that's a little more complicated:
save the cursor position
move the cursor up two lines
clear before the cursor
restore the cursor position.
If your prompt happens to be on the first line of the screen, this could be detected using the cursor position report. But that's more complicated to do than the question as phrased would anticipate. Assume that there's space above:
tput sc
tput cuu 2
tput cub 999
printf '\033[1J'
tput rc
A regular printf rather than tput is used here, because there's no predefined terminfo (or termcap) capability defined for that type of erase.
If you wanted to handle the case where the prompt starts in the top line of the terminal, you'd have to find the current line number and decide whether to do the clearing above (or not).
Further reading:
Re: cursor position in a variable

Change Color of Text Being Currently Used (Terminal - OSX)

For reference purposes: I am using a Macbook Pro Retina (2014) and all software is up to date as of the time I am posting this question. (Using OSX 10.10.2 Yosemite)
I have been struggling for the past few days as I attempted to customized the Terminal window. Everything was going fine... until I decided I only wanted to change the color for the text that you are currently typing (text input) which will then be executed.
I have no problems getting into my .bash_profile and adjusting anything. I simply cannot seem to grasp the color concept well enough such that it only does this one thing for me. Currently my .bash_profile looks like this:
export PS1="\n\n\njboned$ "
export PATH="/usr/local/mysql/bin:$PATH"
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
Any assistance would be greatly appreciated. As a sidenote: I understand the 8-bit snippets used to represent the individual colors, however it seems I cannot figure out how to use them to address only the text input only.
To do what you are asking, these steps would be needed:
at the end of the prompt, turn on the text color which you want to show
when you press return (to complete editing) turn the text-color off.
bash does not reset attributes while you edit, so the color "should" work — while editing. As you edit, bash is likely to use escape sequences which clear the current line (which may fill it with whatever background color you have selected).
The real problem is how to reset the colors when you press Enter. That does not appear to have a straightforward solution: I do not see a way to rebind the Enter key to add features—no distinction is made between levels of interpretation, and you may not find it possible to enhance the Enter key. The key binding feature in bash talks mainly to the readline library; leftovers are sent to bash. In a binding you may be able to do these things:
send the name of a macro to bash, or a full echo command which resets colors (since readline has no echo of its own, it seems)
the Enter key (i.e., ^M), and
to readline directly, the accept-line function
Alternatively, what you could do is bind another key, say control/L to do the bash accept-line function as well as resetting color. Here are a couple of links which you would find useful to investigate how to do this:
how to bind the 'Enter key'
Complex keybinding in bash
In bash, how do I bind a function key to a command?
Smart preparsing with the bash shell

how to get current terminal color pair in bash

I would like to query and store the current terminal color pair in BASH e.g.:
#!/bin/bash
#some ANSI colour escape sequences
red="\033[0;31m"
grn="\033[0;32m"
blu="\033[0;34m"
def="\033[0;00m" # default
echo -e "Change to ${red} red to ${def} default to ${blu} blue."
# now store the current color (which happens to be blue) e.g.:
cur=????
echo -e "Change to ${grn} green and back to what I had before ${cur}"
echo -e "This would be in blue if variable cur contained e.g.: 0;34m."
echo -e "Back to default${def}"
exit 0
The answer that eludes me is how to capture the current color
cur=????
The question was about the current color, not the cursor position.
Both are "nonstandard" (though the latter, cursor position report is implemented by anything which has a valid claim to "VT100 emulator").
However, xterm implements a set of escape sequences referred to as dynamic colors, which predate the ANSI color functionality. Those set the working colors including text foreground and background. I modified this in 2002 to allow an application to send the sequence with a "?" rather than a color to tell xterm to return the color value, e.g.,
OSC 1 1 ? ST
using the notation given in XTerm Control Sequences
You can't; there is no standard control sequence to report the current cursor attributes.
What does exist, however, is a sequence to save and restore the current cursor position and attributes:
\e7 (DECSC) will save the cursor position and attributes.
\e8 (DECRC) will restore the saved cursor position and attributes.
There is no standard way to restore only the cursor attributes; however, as rici mentioned, you can get a report of the current position using \e[6n (DSR), then use the response to manually "un-restore" the cursor position after restoring its position and attributes.
Again, though, it's probably easier (and better) to just keep track of the colors in your application, rather than making the terminal responsible for that.
It's important to understand that the terminal state has nothing to do with bash. Bash doesn't care. It simply reads from stdin and writes to stdout and stderr. (See Note 1)
All terminal effects are implemented by the terminal emulator you happen to be using, of which there are many. In a graphical environment, you might be using, for example, xterm or konsole. You'll need to search the documentation for those emulators for specific terminal control codes which they interpret.
As far as I know, there is no standard code to get a report of the current terminal state, other than the cursor position (ESC[6n).
So your best bet is to remember the changes you made when you make them.
You can find a list of the standard codes implemented by the Linux console using man console_codes (although few people use the Linux console these days); most of those are also interpreted by xterm and other graphical consoles. There's an list of xterm sequences in Thomas Dickey's xterm site; it's a more or less de facto standard for terminal emulators but, as I said, you'll need to search in each emulator's documentation for idiosyncratic control sequences.
Notes
In interactive mode, bash uses a library called readline to help it handle some terminal effects. In particular, readline tries to maintain the current console cursor position, although it is easy to fool it. In PS1 you need to surround console control sequences with \[ and \] precisely because readline does not know that they are control sequences.
What I understood, is that u are asking to to get the default profile color of the user, of which u have change color of!
Eg: User is using 'Bright Green font color on Black background', you change it to 'Red font color on White background'. Now how to get/know the default colors and set them back to it!
You can use the sequence \033[0m in bash [also for command prompt (batch scripting) / powershell (ps scripting)]

Is it possible to replicate the effect of vim's `zz` in bash?

In Vim, executing zz in normal mode will take the line the cursor is currently on, and move that line, together with the cursor, to the vertical center of the current window.
Is there a way to replicate this behavior in bash, to move the current command prompt to the vertical center of the screen, and scroll the command buffer along with it?
I am posting on StackOverflow instead of Unix/Linux because I am open to solutions that require writing custom code if bash does not natively support this.
Vim controls the entire area of the screen. Bash is only responsible for the input line. It has no idea about the contents of the rest of the screen, which is/was under control of other programs, and cannot reposition it.
Note that the contents of the screen nornally cannot be read by a program running on it. The only way to know what's on the screen is to start from a clean state and account for every single character and terminal command printed. Vim does just that; Bash does not and can not.
n.m. has already explained that bash itself cannot do it. However, you still might be able to send commands directly to the underlying terminal to achieve an effect similar to what you want. For example, assuming that your bash input line is somewhere below the middle of the screen, the following will scroll down by sending a couple of empty lines and then putting you into the middle of the screen:
declare -i L; declare -i M; L=`tput lines`/2; M=$L-1; for (( c=1; c<=$L; c++ )); do echo; done; tput cup $M
Note that this doesn't work when your somewhere in the upper half of the screen (it will put you into the middle of the screen, but will not scroll back).
For more info see man tput and man terminfo.

Resources