ls 自定义不同文件显示不同颜色

一种方法:linux

因为经常使用终端(iTerm|Terminal)ssh到服务器上去,所以整日盯着黑乎乎的屏幕很头痛,还不利于眼睛,所谓“工欲善其事,必先利其器”,所以今天就想让终端也色彩缤纷起来,让我们锻炼眼睛识别颜色的能力吧.在git上找到了SOLARIZED这个项目,可以配置很多很多程序的颜色主体哦,哈哈,很是激动,就配额了一下,下面具体讲讲配置过程。

我本机呢,主要是用到了iTerm2颜色配置,而服务器因为是CentOS,所以主要还是ls命令的颜色,那就是它的dircolors-solarized子项目啦。
首先讲一下,CentOS下的ls配置Solarized Color Theme for GNU ls (as setup by GNU dircolors).

Linux下的ls --color=auto
命令,其实是根据两个文件来显示颜色的,/etc/DIR_COLORS 或者 ~/.dir_colors,一个是系统级别的配置文件,一个用户级别的配置文件。

Solarized Color Theme for GNU ls项目就是提供了配置好了的颜色配置文件,并且可以根据文件类型来展示不同的颜色,可以看看我上面的实例图片,色彩缤纷吧.我就不讲Terminal的 16-色 和 256-色 的差别了,因为我也一知半解.不过这个项目的dircolors.ansi-universal配置文件可以工作在两种形式的终端下,而dircolors.256dark只能工作在265色的终端下,所以除非你知道自己的终端是支持265色的,不然还是下载dircolors.ansi-universal吧.
下面是具体命令

cd ~
wget --no-check-certificate https://github.com/seebi/dircolors-solarized/raw/master/dircolors.ansi-universal
#or
#wget http://download.bbkanba.com/dircolors
mv dircolors.ansi-universal ~/.dir_colors
eval `dircolors ~/.dir_colors`
cat >> ~/.bashrc
<<ALLEND########################################
if [ -x /usr/bin/dircolors ]; then 
alias ls='ls --color=auto' 
alias dir='dir --color=auto' 
alias vdir='vdir --color=auto' 
alias grep='grep --color=auto' 
alias fgrep='fgrep --color=auto' 
alias egrep='egrep --color=auto' 
alias tree='tree -C'
fi
########################################ALLEND
source ~/.bashrcls

该项目的ReadMe文件还有很多信息,比如怎样配置265色的终端,感兴趣的可以仔细看看.

iTerm2的配置

iTerm2的配置配置更简单一点,进入到iTerm2的配置子项目下,下载配置文件Solarized Dark.itermcolors或者Solarized Light.itermcolors,双击导入,然后更改首选项选择主题就可以。具体可以查看ReadMe文件。

这里需要补充一点的是,mac 下的ls命令不是gun ls,所以即使iterm2安装了颜色主题,敲入ls命令以后,显示的文件颜色还是有问题的,这里有一个补救办法,运行下面这行,之后再看ls命令的颜色是不是变了
export LSCOLORS=gxfxbEaEBxxEhEhBaDaCaD
但是,这种补救办法也不完美,比如不能根据文件类型来显示颜色。
如果你想追求完美的话,还有另外一种办法,就是用 brew install coreutils
,安装 gun ls命令到mac下,到时你还需要跟上面在linux下操作的一样,下载配置文件,放入到~/.dir_colors

二另一种方法:mac系统

Mac终端terminal颜色实在太单调了,安装Linux使用的GNU Coreutils替换Mac的ls命令:


1、使用 Homebrew 工具安装Coreutils。
Homebrew类似于Centos下的Yum工具,安装软件自动解决依赖关系,非常不错。Homebrew工具的安装见Homebrew官方首页安装说明。
由于Coreutils安装包是xz压缩格式,因此需要同时安装xz工具:
brew install xz coreutils

 
2、生成颜色配置文件:
gdircolors --print-database > ~/.dir_colors


3、在~/.bash_profile配置文件中加入以下代码:
if brew list | grep coreutils > /dev/null ; then
  PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
  alias ls='ls --show-control-chars --color=auto'
  eval `gdircolors -b $HOME/.dir_colors`
fi
gdircolor的作用就是设置ls命令使用的环境变量LS_COLORS(BSD是LSCOLORS),我们可以修改~/.dir_colors自定义文件的颜色,此文件中的注释已经包含各种颜色取值的说明。

Mac到手有一段时间了,看着那默认的单调的Terminal,实在是很难看,便着手美化了一下终端的配色方案,主要分为两步:

一是在终端的设置偏好里设置,可以设置字体的大小,颜色,背景颜色,透明度等等,一般基本的美化操作在这里都可以设置。但是不能设置ls命令针对不同文件显示不同的颜色。

二是调整ls命令显示的文件类型颜色。这步也是最复杂的,主要原理是用dircolors命令读取 .dir_color文件中的配色方案,设置LS_COLORS变量,让ls针对不同文件显示不同的颜色。主要分为以下三步骤(参考TroyCheng同学方法):

主要分以下三步:

  1. 手动下载并编译安装coreutils,可以在这里下载:下载地址。下载完毕之后解压并且安装(安装需要使用gcc,make等命令,可以通过安装Xcode获得):
    tar xzvf coreutils-8.9.tar.gz;
    cd coreuiils-8.9;
    ./configure --prefix=/usr;
    sudo make;
    sudo make install
  2. 安装完毕之后需要配置终端的profile:

    export PS1="\[\e[0;32m\][\e[0;32m\]\u@\e[0;31m\]\h \[\e[0;34m\]\w\e[0;32m\]] $ \[\e[0m\]"

    test -r /sw/bin/init.sh && . /sw/bin/init.sh

    if [ "$TERM" != "dumb" ]; then

    export LS_OPTIONS='--color=auto'

    test -r ~/.dir_color && eval "$(dircolors -b ~/.dir_color)" || eval "$(dircolors -b)"

    eval `dircolors ~/.dir_color`

    fi

    # Useful aliases

    alias ls='ls $LS_OPTIONS'

    alias ll='ls -al'

    alias grep='grep $LS_OPTIONS'

    alias fgrep='fgrep $LS_OPTIONS'

    alias egrep='egrep $LS_OPTIONS'

  3. 如果需要自己设置颜色文件,那么可以编辑如下文件:
    vim ~/.dir_color;

    添加如下的内容即可:

    # Configuration file for dircolors, a utility to help you set the

    # LS_COLORS environment variable used by GNU ls with the --color option.

    # The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the

    # slackware version of dircolors) are recognized but ignored.

    # Below, there should be one TERM entry for each termtype that is colorizable

    TERM linux

    TERM linux-c

    TERM mach-color

    TERM console

    TERM con132x25

    TERM con132x30

    TERM con132x43

    TERM con132x60

    TERM con80x25

    TERM con80x28

    TERM con80x30

    TERM con80x43

    TERM con80x50

    TERM con80x60

    TERM xterm

    TERM xterm-color

    TERM xterm-debian

    TERM rxvt

    TERM screen

    TERM screen-w

    TERM vt100

    # Below are the color init strings for the basic file types. A color init

    # string consists of one or more of the following numeric codes:

    # Attribute codes:

    # 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed

    # Text color codes:

    # 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white

    # Background color codes:

    # 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white

    NORMAL 00   # global default, although everything should be something.

    FILE 00     # normal file

    DIR 01;36   # directory

    LINK 01;37  # symbolic link.  (If you set this to 'target' instead of a

    # numerical value, the color is as for the file pointed to.)

    FIFO 40;33  # pipe

    SOCK 01;35  # socket

    DOOR 01;35  # door

    BLK 40;33;01    # block device driver

    CHR 40;33;01    # character device driver

    ORPHAN 40;31;01 # symlink to nonexistent file

    # This is for files with execute permission:

    EXEC 00;35

    # List any file extensions like '.gz' or '.tar' that you would like ls

    # to colorize below. Put the extension, a space, and the color init string.

    # (and any comments you want to add after a '#')

    # If you use DOS-style suffixes, you may want to uncomment the following:

    #.cmd 01;32 # executables (bright green)

    #.exe 01;32

    #.com 01;32

    #.btm 01;32

    #.bat 01;32

    .tar 01;31 # archives or compressed (bright red)

    .tgz 01;31

    .arj 01;31

    .taz 01;31

    .lzh 01;31

    .zip 01;31

    .z   01;31

    .Z   01;31

    .gz  01;31

    .bz2 01;31

    .deb 01;31

    .rpm 01;31

    .jar 01;31

    .dmg 01;31

    # image formats

    .jpg 01;35

    .png 01;35

    .gif 01;35

    .bmp 01;35

    .ppm 01;35

    .tga 01;35

    .xbm 01;35

    .xpm 01;35

    .tif 01;35

    .png 01;35

    .mpg 01;35

    .avi 01;35

    .fli 01;35

    .gl 01;35

    .dl 01;35

    # source code files

    .pl 00;33

    .PL 00;33

    .pm 00;33

    .tt 00;33

    .yml 00;33

    .sql 00;33

    .html 00;33

    .css 00;33

    .js 00;33

猜你喜欢

转载自blog.csdn.net/qq_35843543/article/details/81385747
ls