I would like your your help writing some code:
I would like Ubuntu 12.04.1 Terminal to color "/" character opposite as other text.
This could be very important for pretty much everyone, who writes bash/python directly in console...
Any ideas where to start?
I'm thinking of a custom plugin fo terminal, that could parse text right before it is printed?
The tput command is your friend (assuming you want to stay in shell-script land).
$ tput smso; echo Bold text; tput rmso
Q: What will highlighting / characters give you?
Related
Background
I need to port a Perl script from Linux to Windows. The script outputs to stdout and highlights and underlines specific words as needed. In Linux, this can be accomplished by surrounding the word(s) with system calls to tput:
tput smso and tput rmso for highlighting
tput smul and tput rmul for underlining
Question
Are there any system calls on Windows that can easily accomplish this functionality? If not, does anyone know a workaround that would accomplish similar results?
If you're using Perl to output stuff, at least Win32::Console can do underline on Windows (10 onwards) as well:
my $win32_console Win32::Console->new();
# Rendering is flakey under Windows 10
my $attr = 0x8000 # COMMON_LVB_UNDERSCORE, Windows 10 onwards
| 0x4000 # COMMON_LVB_REVERSE_VIDEO, Windows 10 onwards
;
$console->Attr($attr);
$console->Write("Hello World");
But if you're just looking for a Really Quick porting fix, Win32::Console::ANSI will "magically" convert all ANSI sequences in your output to the appropriate console calls.
I'm modifying my MOTD in Terminal via "$ sudo nano /etc/motd", and I'm wondering if there's a way to bold text and/or change its colour. I've checked the help documentation within nano, which would usually lead me to believe such formatting to be impossible – but I've found evidence of Linux users being able to change colors in their MOTD's via nano.
Any help is appreciated.
Create a temporary shell script
touch /tmp/mtod.sh && chmod +x /tmp/mtod.sh
Edit the shell script with the message you want, for instance
echo "Welcome to \e[38;5;196mXXX\e[0m"
Output the message to the real motd file, then delete the temorary script
sudo sh -c '/tmp/mtod.sh > /etc/motd' && rm /tmp/mtod.sh
The /etc/motd file is simply cat'd to the terminal, and more than likely the terminal supports ANSI color escape sequences. You can embed that using (not quite any) many text editors. However the essential part is inserting (and keeping track) of the escape character (usually entered as a control[ on the keyboard). According to nano's documentation,
All keys, with the exception of Control and Meta key sequences, will enter text into the file being edited.
That exception seems to apply to the escape character. Reading further, it says that any character can be entered by typing escape twice, then entering its decimal value (27 for escape). But that does not work for the machine where I am typing. Perhaps it works for you, perhaps not.
You could work around that by reserving some relatively useless character (such as #) to act as the escape and then using tr to translate to/from, e.g.,
tr '\033' '#' /etc/motd >/tmp/motd
nano /tmp/motd
tr '#' '\033' /tmp/motd >/etc/motd
Doing that, bold text would be something like
#[1mBOLD#[0m
(\033 is the escape character, of course - some people prefer \x1b).
You can do this straight from the command line, by appending to the file.
First su to root...
Mac:~ user$ su root
Then use the following commands, substituting your ANSI color codes and text...
Mac:~ root# echo "\033[1;34m" >> /etc/motd
Mac:~ root# echo "Hello." >> /etc/motd
Mac:~ root# echo "\033[0m" >> /etc/motd
I realize this doesn't use nano as the question asks, but I thought some might find it helpful.
I'm encountering a strange issue in the Terminal app in Mac OS X Lion. When I enter in a long line of text that should wrap to the next line when it reaches the edge of the Terminal window, it continues to type on top of the text from the line above it.
Here are some screenshots to help illustrate the issue:
Before my text reaches the window edge:
After the text reaches the window edge:
I've also supplied screenshots of my text and window settings in case those might be helpful.
Text settings:
Window settings:
Thanks in advance for any assistance offered. I've had this issue for a while and just never got around to it. It's now really becoming a pain in the ass when I get into things that require big grep commands and long path names.
PS1 environment variable determines what shell's prompt will look like. man bash gives full documentation on it. (There are actually several of them, for different modes).
There are number of files that may be setting it, usually one of ~/.profile, ~/.bashrc, /etc/profile or /etc/bashrc.
If you're going to have color codes or other control sequences inside it, you must wrap them with \[ and \] properly (and NOT wrap normal text), otherwise line editing may become messed up like in your case. I suggest resetting PS1 to the default value then carefully adding coloring back item by item.
For example:
PS1='\[\033[1m\033[32m\]\u#\h \w\[\033[0m\]\$ '
^^^^^^^^^^^^^^^ ^^^^^^^
Coloring commands are underlined. Note how they are surrounded with \[ \].
I have the same problem, i found if you change
Advanced > Emulation > Declare terminal as: ANSI.
This solves colored PS1 problem. With Mac Terminal
BUT creates a strange behavior:
I found a solution to my problem with #koiyu answer.
https://apple.stackexchange.com/questions/37001/strange-behavior-in-terminal-with-custom-bash-profile/37036#37036
I used to have the same issue due to incorrectly using color codes. Here is my PS1 which solved the issue. Also if you use GIT, then this will be also helpful to show git branch you are working on and if your working tree is dirty or not. Put this in your .profile or .bash_profile
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
parse_git_dirty() {
st=$(git status 2>/dev/null | tail -n 1)
if [[ $st == "" ]]; then
echo ''
elif [[ $st == "nothing to commit (working directory clean)" ]]; then
echo ''
elif [[ $st == 'nothing added to commit but untracked files present (use "git add" to track)' ]]; then
echo '?'
else
echo '*'
fi
}
# coloring the terminal comman line
SB_GREEN="\[\033[1;32m\]"
SB_BLUE="\[\033[1;34m\]"
SB_RED="\[\033[1;31m\]"
SB_NOCOLOR="\[\033[0m\]"
export PS1="$SB_GREEN\u#\h$SB_NOCOLOR: $SB_BLUE\w$SB_GREEN\$(parse_git_branch)$SB_RED\$(parse_git_dirty)$SB_NOCOLOR $ "
Hope this helps.
With the guidance of hamstergene I was able to figure out how to make it play nice. Using this Geek Stuff guide and this It's Me Tommy tutorial, I was able to define how I wanted my PS1 text to display. Changing this to something much more simplified eliminated the weird overlapping text issue I was running into.
Before:
After:
I simply edited my .bash_profile and added the following line:
export PS1="[\u#\h] > ";
Then I went and changed the window colors for good measure because I can.
The original issue was that there was a new line in the PS1 (FYI for anybody running into this)
Typically its improperly escaped color codes, but if its not that its that you have a new line in your ps1
As others have said, you have to properly wrap your color commands in escaped square brackets. However, for me, this makes the string really, really confusing to look at!
As such, here's a trick I use to always get it right and also make it much more readable.
I first create a shell function called setColor like so...
setColor(){
echo "\[\033[${1}m\]"
}
Then I use it like this...
PS1="$(setColor 92)\u$(setColor 37):$(setColor 96)\w $(setColor)\$ "
That's the same as writing this...
\[\033[92m\]\u\[\033[37m\]:\[\033[96m\]\w \[\033[m\]$
...but as you can see, the former is much clearer and also guarantees everything's properly escaped.
Note, you can specify multiple colors too by using the ; character. The only thing is you have to explicitly escape it, so 92;41 becomes 92\;41, like so...
PS1="$(setColor 92\;41)\u$(setColor 37):$(setColor 96)\w $(setColor)\$ "
Again, still easier to read than this...
\[\033[92;41m\]\u\[\033[37m\]:\[\033[96m\]\w \[\033[m\]$
You can take this a step further by defining constants for the colors, or even 'wrapper' functions with the color names you use most, so you can write this...
PS1="$(setRed)\u$(setBlue):$(setGreen)\w $(resetColor)\$ "
Hope this helps!
How do you clear the entire terminal in BASH, like the command prompt's cls command?
clear doesn't work because it doesn't actually clear anything, it just scrolls down.
As far as I know, there isn't a way to do this any better than what clear does with bash.
I think it's a feature that could be built into the terminal you're using though. I know the Mac Terminal app has a 'Clear Scrollback' menu option (command + k) that does what you're asking for.
Why don't you try Ctrl+l (control, lowercase "L"). This works in most shells (err terminals)...
In OSX terminal -
Command ⌘+l (command, l) leads to removing last typed command from display.
Command ⌘+k (command, k) leads to removing/clearing all display buffer.
reset (type this in terminal) leads to reset of terminal in case display becomes garbled.
not sure of equivalent in other unix flavors.
You're probably looking for the reset command.
However, the scroll-back buffer is not a feature of bash but of the terminal program. You didn't say what terminal program you were using.
xterm will allow the escape sequence ESC [3J to clear the scroll back, so you could do:
alias cls="clear; printf '\033[3J'"
Short Answer
clear && clear
or
tput reset
Other Ways
Here are all the ways you can clear the terminal screen in Unix:
clear # only clear visible screen
clear && clear # clear buffer as well
tput clear # same as clear but by sending escape seq
reset # clear + reset internal terminal state + 1sec delay
tput reset # same as reset but without 1sec delay
stty sane # don't clear screen but reset some terminal options
echo -e "\033c" # same as tput reset but hardcoded escape seq
printf "\033c" # same as tput reset but hardcoded escape seq
setterm -reset # same as tput reset, setterm has friendlier commands
Long Answer
The clear command only clears the visible screen but not the buffer so you can do Shift+PageUp to scroll up in the terminal and still view previous outputs. If you want to get same result as cls then do clear twice like clear && clear.
Another related command is reset which (I believe) resets the internal state of the terminal program. Unfortunately, this command includes 1 second of delay to support really old terminals. So if you are not ok with that kind of delay then use tput reset which seems to do same thing as reset minus the delay.
But what does tput do? In Unix, you can send terminal all kinds of ASCII character sequences which are interpreted as commands by the terminal. This allows you to do funky things like blink or color the text or turn off echo (during password typing) or set terminal options or do clear or reset. This you can send by tput clear or tput reset. The clear and reset command are equivalent but they run from the binaries that comes with your distro and may do additional stuff. The setterm -reset is similar to tput reset. Setting terminal using setterm is usually better because unlike tput it has more readable options in general case however we here use tput because it's smaller in length :).
You might have also seen people using things like echo -e "\033c" or printf "\033c" which is equivalent to tput reset but the escape sequence is now hard coded. The tput looks up terminal properties and uses the right escape sequence.
Another related command is stty sane which actually doesn't do any screen clearing but it sets many of the terminal options to defaults so if your terminal looks garbled or if terminal stays blank when you type (for example, because you printed binary file to terminal with escape sequence to turn off echo) then this command might help. For extreme garbled terminal cases, you can use all of the available resetting techniques in the sequence. I've alias like this for such occasions:
alias cls='tput reset'
alias clshard='reset; stty sane; tput rs1; setterm -reset; tput reset'
Related
What's the equivalent of the “cls” command from Windows/DOS?
What commands can I use to reset and clear my terminal?
Use ⌘+K. It removes the entries so I can't scroll up anymore.
So ⌘+K to clear everything including scrolling.
Ctrl+L to clear terminal window but still be able to see everything when scrolling up.
In ~/.bashrc, the perfect cls is:
cls () {
printf -- '%b' '\033c'
return $?
}
The clear command works for me.
But I personally find it impractical, because, for me, it clears the scrollback permanently and irreversibly. However, often I want just to insert some "marker"/"separator" into the scrollback, in order to be able to visually distinguish the "recent scrollback" from the "too old scrollback" (but sometimes ability to see the "too old scrollback" would be still useful). So I use something like:
yes '' | head -n100
That inserts 100 empty lines into the scrollback. (Inspired by this answer. You can vary the number of lines, of course.)
I have a program that I run through the command line and I wanted to print out bold or styled text similar to how the man pages are bold (I can't think of a styled example offhand).
How do I style text sent to the terminal?
If it makes a difference, I'm running a MacOSX terminal.
I believe you want to use the ncurses library to accomplish this.
You can have a look at this SO question: Colored grep? which shows a simple way to color output for VT100 terminals (works great on MacOSX)
Another useful SO Question is: Apply formatting to unix shell, with a link to ANSI escape codes, and examples from a shell.
You can do this from any shell script using the tput program to output terminfo codes. Oddly, there's a code to turn vold on but not off---you have to turn everything off. Reverse video can be turned on and off with tput smso and tput rmso.
Here's an example for bold (/bin/ksh):
print -n "This word is "; tput bold; print -n "bold"; tput sgr0; print "!"
In most programming languages it is easier to fork a process and call tput than it is to bother with the ncurses library (to which tput is a command-line interface).