Shell Tips: Difference between revisions
Jump to navigation
Jump to search
(→cat) |
(Move to Linux tips) |
||
Line 1: | Line 1: | ||
#REDIRECT[[Linux Tips]] |
|||
== Color in Shell == |
|||
Some tips to provide a more colorful shell experience. |
|||
* '''cat''' with color syntax highlighting |
|||
* Colored diff '''colordiff''' |
|||
* Color with '''git''' |
|||
* Preserve color with '''less''' |
|||
* Colors in '''man''' pages (using either ''less'' or ''most'' as pager) |
|||
* Vim-based color viewer '''view''' |
|||
=== <code>cat</code> with highlighting === |
|||
The examples below create a script '''ccat''', similar to ''cat'' but with syntax highlighting. |
|||
;Using <code>source-highlight</code> [http://superuser.com/questions/84426/is-there-any-option-let-cat-command-output-with-color] |
|||
Define a script {{file|ccat}}: |
|||
<source lang="bash"> |
|||
#!/bin/bash |
|||
src-hilite-lesspipe.sh $1 |
|||
</source> |
|||
The included ''less'' script is also nice. Add to your '''.bashrc''': |
|||
<source lang="bash"> |
|||
export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s" |
|||
export LESS=FRX # At least R! |
|||
</source> |
|||
Some examples |
|||
<source lang="bash"> |
|||
ccat file.c |
|||
ccat file.c | less -FRX |
|||
</source> |
|||
;Using <code>highlight</code> [http://www.andre-simon.de/doku/highlight/en/regex.php] |
|||
<code>highlight</code> comes with package {{deb|highlight}}. Example of script: |
|||
<source lang="bash"> |
|||
#!/bin/bash |
|||
highlight -O ANSI "$1" |
|||
</source> |
|||
Note that highlight can produce output in many different format (e.g. html) |
|||
;Alternatives |
|||
On Ubuntu: |
|||
* <tt>python-pygments</tt> |
|||
* <tt>[http://www.palfrader.org/code2html/ code2html]</tt> |
|||
* <tt>[http://www.gnu.org/software/src-highlite/ source-highlight]</tt> (a dependency of ''qgit'') |
|||
=== <tt>colordiff</tt> === |
|||
'''colordiff''' is a Perl wrapper around ''diff'' to show diff outputs in color: |
|||
<source lang="bash"> |
|||
colordiff file1 file2 # Diff in colors |
|||
alias diff="colordiff -W ${COLUMNS:-130}" # Handy alias that also adds auto-width |
|||
</source> |
|||
Note that ''colordiff'' output can be piped through ''less'', or ''colordiff'' can also take a diff as standard input: |
|||
<source lang="bash"> |
|||
colordiff file1 file2 | less -FSRX # Page output |
|||
<patchfile colordiff # Colorize stdin |
|||
</source> |
|||
=== <tt>git</tt> === |
|||
See [[Git#Configuration|Git (configuration section)]]. |
|||
=== <tt>less</tt> === |
|||
Use option '''-R''' to preserve ANSI color escape sequence in input: |
|||
<source lang="bash"> |
|||
colordiff file1 file2 | less -R # As command-line option |
|||
export LESS=R # Better use env. var $LESS |
|||
colordiff file1 file2 | less # ... no need to give -R anymore |
|||
export LESS=FRX # Even better |
|||
colordiff file1 file2 | less # ... quit asap if less than 1-page |
|||
</source> |
|||
In ''git'', ''less'' is called with the following option: |
|||
<source lang="bash"> |
|||
export lESS=FSRX # Default git options |
|||
# ... F - quit if less than 1-page |
|||
# ... S - truncate long lines |
|||
# ... R - preserve color |
|||
# ... X - don't initialize term (no clear) |
|||
</source> |
|||
=== <tt>man</tt> === |
|||
To have colors in ''manpages'', the simplest is to install the package ''most'': |
|||
<source lang="bash"> |
|||
sudo apt-get install most |
|||
</source> |
|||
And then tell how you want to use it: |
|||
<source lang="bash"> |
|||
# SYSTEM-WIDE |
|||
sudo update-alternatives --config pager # To select 'most' as default pager system-wide |
|||
# per USER - in ~/.bashrc |
|||
export PAGER=most # To select 'most' as default pager for that user |
|||
alias man="PAGER=most man" # To select 'most' only for manpages |
|||
</source> |
|||
The drawback is that ''most'' does not support the same keyboard shortcuts as ''less''. If you still want colors in your man page, you can get the same effect with ''less'' thanks to some bash script-fu magic (see [http://nion.modprobe.de/blog/archives/569-colored-manpages.html]) |
|||
<source lang="bash"> |
|||
mkdir ~/.terminfo/ |
|||
cd ~/.terminfo |
|||
wget http://nion.modprobe.de/mostlike.txt # Get the new terminfo description |
|||
tic mostlike.txt # Compile it using terminfo entry-description compiler |
|||
</source> |
|||
Now just define the following alias in <tt>~/.bashrc</tt>: |
|||
<source lang="bash"> |
|||
alias man="TERMINFO=~/.terminfo TERM=mostlike PAGER=LESS LESS=C man" # LESS=C can be skipped or adapted |
|||
</source> |
|||
Unfortunately the supplied terminfo does not support scrolling the pages with the mousewheel as it used to. Another alternative is to give ''less'' some explicit termcap commands (see [http://tips4linux.com/color-man-pages-in-linux/]): |
|||
<source lang="bash"> |
|||
export LESS_TERMCAP_mb=$'\E[01;31m' |
|||
export LESS_TERMCAP_md=$'\E[01;37m' |
|||
export LESS_TERMCAP_me=$'\E[0m' |
|||
export LESS_TERMCAP_se=$'\E[0m' |
|||
export LESS_TERMCAP_so=$'\E[01;44;33m' |
|||
export LESS_TERMCAP_ue=$'\E[0m' |
|||
export LESS_TERMCAP_us=$'\E[01;32m' |
|||
</source> |
|||
=== <tt>view</tt> === |
|||
''view'' is ''vim'' front-end document reader. ''view'' can highlight syntax of files based on their filename, but also based on their content (unlike ''source-highlight''). Use <code>view -</code> to view standard input: |
|||
<source lang="bash"> |
|||
view file.c # Detect .c format from filename |
|||
git diff HEAD^ | view - # Git - Will detect diff format from content |
|||
svn diff | view - # Svn - idem |
|||
</source> |
|||
== Pager == |
|||
;Manpages |
|||
* By default uses <tt>pager -s</tt> |
|||
** <tt>/usr/bin/pager</tt> is a symlink to <tt>/etc/alternatives/pager</tt>. |
|||
* Overridden by env var '''$PAGER'''. |
|||
* Overridden by env var '''$MANPAGER'''. |
|||
* Overridden by cli option '''-P pager'''. |
Latest revision as of 11:45, 21 May 2019
Redirect to: