Shell Tips: Difference between revisions
m (→cat) |
(→Color in Shell: color manpages) |
||
Line 3: | Line 3: | ||
* '''cat''' with color syntax highlighting |
* '''cat''' with color syntax highlighting |
||
⚫ | |||
* Color with '''git''' |
|||
* Preserve color with '''less''' |
* Preserve color with '''less''' |
||
* Colors in '''man''' pages (using ''less'' as pager) |
|||
* Vim-based color viewer '''view''' |
* Vim-based color viewer '''view''' |
||
⚫ | |||
=== <tt>cat</tt> === |
=== <tt>cat</tt> === |
||
The example below creates a script '''ccat''', similar to ''cat'' but with syntax highlighting (using ''source-highlight'', ref [http://superuser.com/questions/84426/is-there-any-option-let-cat-command-output-with-color]): |
The example below creates a script '''ccat''', similar to ''cat'' but with syntax highlighting (using ''source-highlight'', ref [http://superuser.com/questions/84426/is-there-any-option-let-cat-command-output-with-color]): |
||
* Define a script |
* Define a script '''<tt>ccat</tt>''': |
||
<source lang="bash"> |
<source lang="bash"> |
||
#!/bin/bash |
#!/bin/bash |
||
Line 33: | Line 35: | ||
* <tt>highlight</tt> |
* <tt>highlight</tt> |
||
* <tt>[http://www.gnu.org/software/src-highlite/ source-highlight]</tt> (a dependency of ''qgit'') |
* <tt>[http://www.gnu.org/software/src-highlite/ source-highlight]</tt> (a dependency of ''qgit'') |
||
⚫ | |||
⚫ | |||
<source lang="bash"> |
|||
⚫ | |||
⚫ | |||
</source> |
|||
⚫ | |||
<source lang="bash"> |
|||
⚫ | |||
<patchfile colordiff # Colorize stdin |
|||
</source> |
|||
=== <tt>git</tt> === |
|||
See [[Git#Configuration|Git (configuration section)]]. |
|||
=== <tt>less</tt> === |
=== <tt>less</tt> === |
||
Line 53: | Line 71: | ||
</source> |
</source> |
||
=== <tt> |
=== <tt>man</tt> === |
||
Reference: [http://nion.modprobe.de/blog/archives/569-colored-manpages.html] |
|||
⚫ | |||
Do the following to enable color in manpages with ''less'' as pager (not ''more'', which has some other limitations!): |
|||
<source lang="bash"> |
<source lang="bash"> |
||
mkdir ~/.terminfo/ |
|||
⚫ | |||
cd ~/.terminfo |
|||
⚫ | |||
wget http://nion.modprobe.de/mostlike.txt # Get the new terminfo description |
|||
svn diff | view - # Svn - idem |
|||
tic mostlike.txt # Compile it using terminfo entry-description compiler |
|||
</source> |
</source> |
||
Now just define the following alias in your <tt>~/.bashrc</tt>: |
|||
⚫ | |||
⚫ | |||
<source lang="bash"> |
<source lang="bash"> |
||
alias man="TERMINFO=~/.terminfo TERM=mostlike PAGER=LESS LESS=C man" # LESS=C can be skipped or adapted |
|||
⚫ | |||
⚫ | |||
</source> |
</source> |
||
=== <tt>view</tt> === |
|||
⚫ | |||
⚫ | |||
<source lang="bash"> |
<source lang="bash"> |
||
⚫ | |||
⚫ | |||
git diff HEAD^ | view - # Git - Will detect diff format from content |
|||
⚫ | |||
</source> |
</source> |
||
Revision as of 08:58, 3 March 2011
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 less as pager)
- Vim-based color viewer view
cat
The example below creates a script ccat, similar to cat but with syntax highlighting (using source-highlight, ref [1]):
- Define a script ccat:
#!/bin/bash
src-hilite-lesspipe.sh $1
- The included less script is also nice. Add to your .bashrc:
export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=FRX # At least R!
Some examples
ccat file.c
ccat file.c | less -FRX
There are some alternatives to source-highlight. For instance on Ubuntu:
- python-pygments
- code2html
- highlight
- source-highlight (a dependency of qgit)
colordiff
colordiff is a Perl wrapper around diff to show diff outputs in color:
colordiff file1 file2 # Diff in colors
alias diff="colordiff -W ${COLUMNS:-130}" # Handy alias that also adds auto-width
Note that colordiff output can be piped through less, or colordiff can also take a diff as standard input:
colordiff file1 file2 | less -FSRX # Page output
<patchfile colordiff # Colorize stdin
git
See Git (configuration section).
less
Use option -R to preserve ANSI color escape sequence in input:
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
In git, less is called with the following option:
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)
man
Reference: [2]
Do the following to enable color in manpages with less as pager (not more, which has some other limitations!):
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
Now just define the following alias in your ~/.bashrc:
alias man="TERMINFO=~/.terminfo TERM=mostlike PAGER=LESS LESS=C man" # LESS=C can be skipped or adapted
view
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 view -
to view standard input:
view file.c # Detect .c format from filename
git diff HEAD^ | view - # Git - Will detect diff format from content
svn diff | view - # Svn - idem
Pager
- Manpages
- By default uses pager -s
- /usr/bin/pager is a symlink to /etc/alternatives/pager.
- Overridden by env var $PAGER.
- Overridden by env var $MANPAGER.
- Overridden by cli option -P pager.