Changkun's Blog欧长坤的博客

Science and art, life in between.科学与艺术,生活在其间。

  • Home首页
  • Ideas想法
  • Posts文章
  • Tags标签
  • Bio关于
  • TOC目录
  • Overview概览
Changkun Ou

Changkun Ou

Human-AI interaction researcher, engineer, and writer.人机交互研究者、工程师、写作者。

Bridging HCI, AI, and systems programming. Building intelligent human-in-the-loop optimization systems. Informed by psychology, sociology, cognitive science, and philosophy.连接人机交互、AI 与系统编程。构建智能的人在环优化系统。融合心理学、社会学、认知科学与哲学。

Science and art, life in between.科学与艺术,生活在其间。

276 Blogs博客
165 Tags标签
Changkun's Blog欧长坤的博客

Put color on Mac terminal

Published at发布于:: 2014-04-22   |   Reading阅读:: 1 min   |   PV/UV: /

Mac中的ls命令可以使用-G参数彩色化输出的文件列表,需要配置LSCOLORS环境变量定义颜色,具体配置方法可以输入man ls查看。

不过,我查到这篇文章:http://linfan.info/blog/2012/02/27/colorful-terminal-in-mac/ 这篇文章的博主推荐安装Linux使用的GNU Coreutils替换Mac的ls命令,因为: Coreutils提供了配置工具,定义颜色代码更加方便; Coreutils包含的不仅仅是ls,同时作为Linux用户,他更习惯于使用GNU的各种shell工具。 (好吧,其实我也是一样)

Coreutils的安装与配置方法如下: 通过Homebrew安装Coreutils

1
brew install xz coreutils 
注:Coreutils并不依赖于xz,但它的源码是用xz格式压缩的,安装xz才能解压。

生成颜色定义文件

1
gdircolors --print-database > ~/.dir_colors

在~/.bash_profile配置文件中加入以下代码

1
2
3
4
5
if brew list | grep coreutils > /dev/null ; then
  PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
  alias ls='ls -F --show-control-chars --color=auto'
  eval `gdircolors -b $HOME/.dir_colors`
fi

gdircolor的作用就是设置ls命令使用的环境变量LS_COLORS(BSD是LSCOLORS),我们可以修改~/.dir_colors自定义文件的颜色,此文件中的注释已经包含各种颜色取值的说明。

grep高亮显示关键字: 这个很简单,加上–color参数就可以了,为了使用方便,可以在~/.bash_profile配置文件中加上alias定义。

1
2
3
alias grep='grep --color'
alias egrep='egrep --color'
alias fgrep='fgrep --color'

Vim语法高亮: 在Vim中输入命令:syntax on激活语法高亮,若需要Vim启动时自动激活,在~/.vimrc中添加一行syntax on即可。 不过,我还再.vimrc里面加了其他参数:

1
2
3
4
5
6
set tabstop=4
set softtabstop=4
set ts=4
set expandtab
set autoindent
set nu
#Vim# #Mac#
  • Author:作者: Changkun Ou
  • Link:链接: https://changkun.de/blog/posts/put-color-on-mac-terminal/
  • All articles in this blog are licensed under本博客所有文章均采用 CC BY-NC-ND 4.0 unless stating additionally.许可协议,除非另有声明。
Matlab 2013a for Mac 帮助文档卡死解决方案
论文都是逼出来的

Have thoughts on this?有想法?

I'd love to hear from you — questions, corrections, disagreements, or anything else.欢迎来信交流——问题、勘误、不同看法,或任何想说的。

hi@changkun.de
© 2008 - 2026 Changkun Ou. All rights reserved.保留所有权利。 | PV/UV: /
0%