Linux 印出有顏色的程式碼

在 linux 環境只要印出程式碼來看,可以用 cat or less 這些檔案輸出的指令,

但是都沒有像 vim 一樣有顏色,可以先裝好 source-highlight 這個套件,yum 或 apt-get 應該都可以~

他原始碼裡面就有附一個用來 less 的 shell

01
02
03
04
05
06
07
08
09
10
11
#! @SHELL@
 
for source in $1; do
    case $source in
    *ChangeLog|*changelog)
        source-highlight --failsafe -f esc --lang-def=changelog.lang --style-file=esc.style -i $source ;;
    *Makefile|*makefile)
        source-highlight --failsafe -f esc --lang-def=makefile.lang --style-file=esc.style -i $source ;;
        *) source-highlight --failsafe --infer-lang -f esc --style-file=esc.style -i $source ;;
    esac
done

可以再整理成一個 shell 用來呼叫

ccat
1
2
3
4
5
6
7
8
9
#!/bin/bash
 
if [ "$1" = -h ]; then
    echo "usage : ccat $file"
elif [ $1 ] && [ -f $1 ]; then
    src-hilite-lesspipe.sh $1 |less -R
else
    echo 'no input '
fi

然後就能用 ccat $file 印有顏色的程式出來看哩

參考資源:

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *