To get colors to Less in Ubuntu's Zsh - bash

How can you get similar highlightings to Zsh's Less than Bash's Less in Ubuntu?
I switched from OS X to Ubuntu. My Less do not work as expected in Zsh.
Manuals in my Less are green and black with or without the following code.
# comment these out in Ubuntu
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;33;146m' # begin underline is now yellow
# | | |
# | |----------------- yellow
# |-------------------- underline
# to have the indication of cursor's location and line numbers, and R
export LESS="-mNR"
# |--------- only ASCII color
The code makes manuals readable in OS X, but it does not work for Ubuntu in Zsh.
Ubuntu has excellent highlightings in Bash's Less. My manuals have the colors yellow, green and black in Bash without my code. Both Zsh and Bash use the same Less at /usr/bin/less. This suggests me that Ubuntu's Bash has some dot-files which configure it somewhere.
Where are highlightings for Ubuntu's Less in Bash?

This works for me in zsh on archlinux:
$ mkdir ~/.terminfo/ && cd ~/.terminfo
Now get the terminfo description:
$ wget http://nion.modprobe.de/mostlike.txt
Now compile it using tic (the terminfo entry-description compiler)
$ tic mostlike.txt
(you may want to delete the mostlike.txt file after compiling)
mostlike.txt is this
# Reconstructed via infocmp from file: /usr/share/terminfo/x/xterm-pcolor
mostlike|manpages with color looking like most,
am, hs, km, mir, msgr, xenl,
cols#80, it#8, lines#24, wsl#40,
acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
bel=^G, bold=\E[1m\E[31m, clear=\E[H\E[2J, cr=^M,
csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,
cud=\E[%p1%dB, cud1=^J, cuf=\E[%p1%dC, cuf1=\E[C,
cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
dch=\E[%p1%dP, dch1=\E[P, dl=\E[%p1%dM, dl1=\E[M,
dsl=\E]0;\007, ed=\E[J, el=\E[K, enacs=\E)0, fsl=^G,
home=\E[H, ht=^I, hts=\EH, il=\E[%p1%dL, il1=\E[L, ind=^J,
is2=\E7\E[r\E[m\E[?7h\E[?1;3;4;6l\E[4l\E8\E>, kbs=^H,
kcub1=\EOD, kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA,
kdch1=\E[3~, kf1=\E[11~, kf10=\E[21~, kf11=\E[23~,
kf12=\E[24~, kf13=\E[25~, kf14=\E[26~, kf15=\E[28~,
kf16=\E[29~, kf17=\E[31~, kf18=\E[32~, kf19=\E[33~,
kf2=\E[12~, kf20=\E[34~, kf3=\E[13~, kf4=\E[14~,
kf5=\E[15~, kf6=\E[17~, kf7=\E[18~, kf8=\E[19~, kf9=\E[20~,
kfnd=\E[1~, kich1=\E[2~, kmous=\E[M, knp=\E[6~, kpp=\E[5~,
kslt=\E[4~, rc=\E8, rev=\E[7m\E[34m, ri=\EM, rmacs=^O,
rmcup=\E[2J\E[?47l\E8, rmir=\E[4l, rmkx=\E[?1l\E>,
rmso=\E[m, rmul=\E[m,
rs2=\E7\E[r\E8\E[m\E[?7h\E[?1;3;4;6l\E[4l\E>, sc=\E7,
sgr0=\E[m, smacs=^N, smcup=\E7\E[?47h, smir=\E[4h,
smkx=\E[?1h\E=, smso=\E[1;30m\E[47m, smul=\E[32m,
tbc=\E[3g, tsl=\E]0;, u6=\E[%i%d;%dR, u7=\E[6n,
u8=\E[?1;2c, u9=\E[c,
And then just define an alias in the rc file of your favorite shell:
alias man="TERMINFO=~/.terminfo/ LESS=C TERM=mostlike PAGER=less man"

My default shell is bash so take this with a grain of salt. Start with /etc/profile and see how it sources bash-specific files. You need to re-create that logic for zsh. Maybe the zsh-lovers package can help, at least its title of tips, tricks and examples for the zsh
is suggestive.

Related

GNU Screen tab titles all just 'zsh'

OK, so I want to start by first explaining my setup. I have a windows desktop at home, and I am SSH'ing into an Amazon EC2 instance (via PuTTY) running Amazon Linux. I have zsh as my default shell, and oh-my-zsh installed as well. This "cloud developer desktop" model works well for me, but I am having one problem that I have poured more time into than I care to admit: GNU screen only shows 'zsh' as the title of every tab. This is despite using oh-my-zsh's screen plugin (which I think isn't doing anything). Anyone able to help me out? I'd love to have something more descriptive in the tab, perhaps just the last x characters of the current directory (or an open file name if one is open in vim).
Like many screen users, I've had what I'm asking for before, but on a new rig now and don't fully understand everything in my .screenrc:
# Many settings from https://gist.github.com/azitabh/7427682 and
# https://gist.github.com/joaopizani/2718397 and
# https://gist.github.com/ChrisWills/1337178
# Allow bold colors - necessary for some reason
attrcolor b ".I"
# Tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm "Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm"
# Erase background with current bg color
defbce "on"
# Cache 30000 lines for scroll back
defscrollback 30000
# add tabs on bottom
caption always "%{= bb}%{+b w}%n %t %h %=%l %H %c"
# Very nice tabbed colored hardstatus line
#hardstatus string '%{= Kd} %{= Kd}%-w%{= Kr}[%{= KW}%n %t%{= Kr}]%{= Kd}%+w %-= %{KG} %H%{KW}|%{KY}%101`%{KW}|%D %M %d %Y%{= Kc} %C%A%{-}'
hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"
#Remove vim buffer from scrollback history after quitting
altscreen on
# special xterm hardstatus: use the window title.
#termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
#termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]1;screen\007'
# Enable 256 color term
term xterm-256color
# Enables use of shift-PgUp and shift-PgDn
termcapinfo xterm|xterms|xs|rxvt ti#:te#
# tell screen that xterm can switch to dark background and has function keys.
termcapinfo xterm 'VR=\E[?5h:VN=\E[?5l'
termcapinfo xterm 'k1=\E[11~:k2=\E[12~:k3=\E[13~:k4=\E[14~'
termcapinfo xterm 'kh=\E[1~:kI=\E[2~:kD=\E[3~:kH=\E[4~:kP=\E[H:kN=\E[6~'
# window numbering starts at 1 not 0
bind c screen 1
bind 0 select 10
screen 1
#allow mouse scrolling in screen
termcapinfo xterm* ti#:te#
# Automatically detach on hangup.
autodetach on
I also tried adding this to my .zshrc, and it helps, but isn't quite what I want, as if you run ls, now your title is ls. Ie, not very informative. But maybe editing here is actually the right way to go:
# So screen tabs receive running process title
# preexec () {
# echo -ne "\ek${1%% *}\e\\"
# }
Thanks in advance for your help!
Answering my own question a bit here:
Replacing the line in the preexec () function above with this helps:
echo -ne "\ek$(pwd)\e\\"
Would still be best to only grab the characters from the x-to-last '/' to the end though, replacing the initial string with '...'

change color of prompt in bash profile

I am trying to change the color of the prompt in my terminal to green text for the user at the start and white ~
Currently my .bash_profile file is configured the following way to provide a yellow color for the user name and a pink color for the ~
PS1='\e[33;1m\u#\h: \e[31m\W\e[0m\$'
Does anyone know how to modify the above to change to the colors to green and white?
Thanks!
PS1='\e[32;1m\u#\h: \e[37m\W\e[0m\$'
The numbers after the [ are the color codes. See this reference.
You are looking to change the ANSI escape sequences, specifically the colors.
\e[...m takes a semicolon-separate list of codes to manipulate how the following text is displayed. 33 represents yellow foreground text, 1 represents bold text, 31 represents red foreground text, and 0 resets all values (foreground and background colors, styles, etc) to their terminal defaults.
# Make text yellow and bold, then make it red (keeping it bold)
# and finally restore the default
PS1='\e[33;1m\u#\h: \e[31m\W\e[0m\$'
To use green/white instead of yellow/red, change 33 to 32 and 31 to 37. Also, be sure to enclose characters that do not take up any space on screen inside \[...\] so that the shell can properly determine the length of your prompt.
PS1='\[\e[32;1m\]\u#\h: \[\e[37m\]\W\[\e[0m\]\$'
This assumes that your terminal understands ANSI escape sequences; a more portable method is to use tput to output the codes your actual terminal uses:
PS1='\[$(tput bold; tput setaf 2)\u#\h: \[$(tput setaf y)\]\W$(tput sgr0)\$ '
zsh, incidentally, makes this much easier; it has built-in escapes for changing the color in a terminal-independent way:
# 1. Everything between %B and %b is in bold
# 2. Everything between %F{x} and %f is in a different color;
# x can be a color name, and you can switch from one
# color to another without using %f
# 3. zsh is smart enough to account for built-in escapes when
# computing the prompt lenght, so no equivalent of \[...\]
# is needed
# 4. %n is the same as \u
# 5. %m is the same as \h
# 6. %~ is roughly the same as \W
# 7. %# is roughly the same as \$
PS1='%B%F{green}%n#%m: %F{white}%~%b%f%# '
This approach has two benefits over the others:
it brackets the escape sequences using \[ and ``]to prevent character count problems withCTRL-A` editing of wrapped lines
it uses tput to change the colors so that it is a little more self-documenting what is being done:
PS1='\[$(tput setaf 2)\]\u#\h: \[$(tput setaf 7)\]\W\[$(tput sgr0)\]\$'
To change color of prompt in each section, try to use this simple repo:
https://github.com/joenmarz/bashrc-alias
You can change the color of each prompt section like:
username
'#' sign
hostname
time
bracket sign '[' and ']'
root indicator (# for root user), ($ for non-root user)
The look of your prompt will be like:
[username#hostname 00:00 AM ~/working/directory $]
Go to line 33 of this file (bashrc-alias/.bashrc) to customize each prompt section color variables:
open_brk_color to change open bracket color
closed_brk_color to change closed bracket color
at_color to change '#' sign color
username_color to change username color
hostname_color to change hostname color
time_color to change time prompt color
wd_color to change working directory prompt color
ri_color to change root indicator color
How To Install
Clone this repository at your home directory
git clone https://github.com/joenmarz/bashrc-alias
Add the file path inside your existing ~/.bash file:
. /home/$USER/bashrc-alias/aliases
refesh your .bashrc source by typing:
source ~/.bashrc
I made it easier by adding some variables to each prompt section.

Process the bash output with color code

I have a Mac App (CodeRunner) that executes a script in login mode and shows the output in a window.
In error condition, it returns the code with escape characters to make the output hard to read.
Is there a way to process the color code? Is there a filter to remove the color code?
With Mac OS X, I used stderred library.
#export DYLD_INSERT_LIBRARIES="/LOCATION_OF_THE_LIB/libstderred.dylib${DYLD_INSERT_LIBRARIES:+:$DYLD_INSERT_LIBRARIES}"
Removing this library setup shows the strings without the code.
If I understand your question, the easiest way to control color in bash (or any terminal that supports them) is with ANSI escape sequences. Example:
#!/bin/bash
blue='\e[0;34m' # ${blue}
green='\e[0;32m' # ${green}
nc='\e[0m' # ${nc} (no color - disables previous color selection)
printf "${blue}This text is blue, ${green}this is green${nc}\n"
exit 0
There are a number of complete references for the ANSI sequences available on the web, but for basic colors, the following is generally sufficient:
black='\e[0;30m' # ${black}
blue='\e[0;34m' # ${blue}
green='\e[0;32m' # ${green}
cyan='\e[0;36m' # ${cyan}
red='\e[0;31m' # ${red}
purple='\e[0;35m' # ${purple}
brown='\e[0;33m' # ${brown}
lightgray='\e[0;37m' # ${lightgray}
darkgray='\e[1;30m' # ${darkgray}
lightblue='\e[1;34m' # ${lightblue}
lightgreen='\e[1;32m' # ${lightgreen}
lightcyan='\e[1;36m' # ${lightcyan}
lightred='\e[1;31m' # ${lightred}
lightpurple='\e[1;35m' # ${lightpurple}
yellow='\e[1;33m' # ${yellow}
white='\e[1;37m' # ${white}
nc='\e[0m' # ${nc} (no color - disables previous color selection)
Note: always reset the color to default at the end of a string with ${nc} ('\e[0m') to prevent continuation of the color after your script finishes. Lastly, if using echo, you must use echo -e to process the ANSI codes.
Note2: since you are seeing the codes and not the color, you have several possibilities (1) you are using echo without the -e option to allow proper interpretation of the codes (or something similar); or (2) the terminal you are using lacks color capability (though that is doubtful, as just about all modern terminals are VT based with default color handling available).
As suggesting by the comments below, you can also set color with tput (setab # for background and setaf # for foreground).

Discovering remote Terminal for Terminal Escape Codes? (DECDHL in this case)

I am trying to determine WHAT console I am running in. (Exceptionally hard based on the research I have done so far.) The latest feature that I discovered that would be useful is Double High, Double Wide for a couple of scenarios.
The setup is a Kubuntu 15.04 machine with native (lower) and remote access via Terminal.app on OS X 10.10.4.
Based on vt100.net Apple is doing the right thing.
#!/bin/bash
# Cool effect with OS X Terminal.app
# Not as much on others (Like Konsole)
function embiggen()
{
# Yellow (Darker) foreground
# | Black backround
# | |
printf "\x1b[38;5;226m\x1b[48;5;0m"
# Double high 'top anchor'
# | line down
# | | Start of line
# | | |
printf "\x1b#3$1\x1b[B\x1b[G"
# Yellow (Bright) foreground
# | Red background
# | (Bright) |
printf "\x1b[38;5;229m\x1b[48;5;196m"
# Double high 'bottom anchor'
# | line down
# | | Start of line
# | | |
printf "\x1b#4$1\x1b[B\x1b[G\n\n"
}
clear
embiggen "Hello, World"
With Konsole
With Konsole the rendering seems to be controlled from bottom to top. i.e. each line is drawn bottom to top basically topmost line wins. However, repaints are less than predictable.
Is it remotely possible to use some of the extended features in a reasonably gracefully degraded way when the terminal doesn't support extended formating?
Best 'solution' I have thought of is using a custom entry point with
ssh -i ... usr#svr.dom bash --init-file osx_remote -i
The short answer, is "no" - this is not possible using just escape sequences.
You cannot tell whether the terminal actually displays double-sized characters. Using the cursor-position report, you can tell (except for buggy implementations) if the terminal used two cells on the screen to represent a double-width character. xterm did that long ago; other terminals do that.
Double-sized characters are a VT100 feature, so they are not listed in a device attributes response either. That would be of limited use, since some terminal emulator developers simply cut/paste responses to make them satisfy applications which check for specific features.
If you were running locally (rather than via ssh), conceivably you could write a program which did an X Window dump and analyzed that picture.

How do you automatically colorize program outputs in a bash shell?

I want to take any program that outputs to the screen, catch the output, and colorize certain keywords before they are output to the screen. For example, here's the normal program output:
bash# <program>
blah blah blah <-- this output has no color
vs.
bash# <program>
blah blah blah <-- this output is colorful
Ideally it doesn't matter what the program is. I'm just looking for a good way to incorporate more color into my konsoles.
Edit: Sorry, should've been clear. I'm not trying to just colorize shell script outputs.
#!/bin/sh
redf=$(tput setaf 1)
redb=$(tput setab 1)
reset=$(tput op)
echo "${redf}red${reset} in front, ${redb}red${reset} in back"
See terminfo for a long listing of terminal capabilities. A $TERM with suffix -m (e.g. ansi-m) means the screen is monochrome, but as long as color works, the following string capabilities should be non-empty:
enter_bold_mode bold md turn on bold (extra
bright) mode
enter_italics_mode sitm ZH Enter italic mode
enter_reverse_mode rev mr turn on reverse
video mode
orig_pair op op Set default pair to
its original value
set_a_background setab AB Set background
color to #1, using
ANSI escape
set_a_foreground setaf AF Set foreground
color to #1, using
ANSI escape
Colors 0-7 are pretty much standard: black, red, green, yellow, blue, magenta, cyan, white. Beyond that may not exist or may be more variable.
The ack program is a version of grep that does color highlighting of regular expression matches in its output. You could use it to do the coloring for you, or you could study its Perl code.
Another option would be to pipe to GNU's grep, with a --color=always or --color=auto argument.
You can write a colorizing script. There's an excellent guide here
http://www.faqs.org/docs/abs/HTML/colorizing.html
You might want to look at something like colorex, or the suggestions at a similar question on unix.SE.
Try the simple and brilliant generic colouriser (homepage):
Generic Colouriser is yet another colouriser (written in python) for beautifying your logfiles or output of commands.
It's available in Debian and is preconfigured for many tools:
$ grc traceroute www.linux.org
$ grc tail -25 /var/log/syslog

Resources