MacOS终端终极美化方案

配置安装

  • 安装Homebrew
    参考大佬的安装脚本,直接执行下面的命令。
1
2
3
4
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

# 下面是卸载脚本
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/HomebrewUninstall.sh)"

brew常用到的命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
查看brew的帮助

brew –help
安装软件

brew install git

卸载软件

brew uninstall git
搜索软件

brew search git
显示已经安装软件列表

brew list
更新软件,把所有的Formula目录更新,并且会对本机已经安装并有更新的软件用*标明。

brew update
更新某具体软件

brew upgrade git
显示软件内容信息

brew info git
用浏览器打开

brew home
显示包依赖

brew deps
显示包的依赖树

brew deps --installed --tree
启动web服务器,可以通过浏览器访问
http://localhost:4567/
来同网页来管理包

brew server
删除程序,和upgrade一样,单个软件删除和所有程序老版删除。

brew cleanup git
brew cleanup
查看那些已安装的程序需要更新

brew outdated

安装oh-my-zsh

  • 更改shell
    Mac默认的终端是zsh,这里需要更改为bash
1
2
3
4
chsh -s /bin/zsh

# 改回命令
chsh -s /bin/zsh
  • curl 安装方式
1
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  • wget 安装方式
1
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

有得同学可能无法访问该链接,可以在浏览起上打开该链接,将网页上的脚本代码复制保存到Mac Home目录下,命名为oh-my-zsh-install.sh,执行sh -c oh-my-zsh-install.sh

主题和插件设置

基础设置

1
2
3
4
5
6
7
8
9
10
11
12
#一些常见的命令别名
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ll='ls -la'

#CLICOLOR是否开启命令行颜色的显示。CLI是Command Line Interface的缩写。
export CLICOLOR=1

#LSCOLORS是用来设置当CLICOLOR被启用后,各种文件类型的颜色。LSCOLORS的值中每两个字母为一组,
#分别设置某个文件类型的文字颜色和背景颜色。LSCOLORS中一共11组颜色设置,按照先后顺序,分别对以下的文件类型进行设置
export LSCOLORS=ExDxFxDaCxDaDahbadacec

配置主题

oh-my-zsh官方提供了一些主题,其主题文件在${HOME}/.oh-my-zsh/themes目录下,共有145个主题,下面我只截了部分主题。
各个主题的详细信息说明:https://github.com/robbyrussell/oh-my-zsh/wiki/Themes,可在里面寻找你喜欢的主题。
选好主题后,在.zshrc文件中增加下面配置

1
2
ZSH_THEME="ys" #ys
ZSH_THEME="random" #表示每次登陆shell随机主题,按照个人喜好配置

配置插件

默认已有插件在${HOME}/.oh-my-zsh/plugins目录下,下面几款插件是我常用到的,有几个需要手动安装一下。

  • git
    自带插件,可以使用缩写命令, 比如 gaa -> git add --all, 通过alias | grep git查看所有支持缩写命令
    激活: 添加到~/.zshrc的plugins列表
  • extract
    自带插件,不用再使用复杂的tar来解压压缩包了
    激活: 添加 extract 到 ~/.zshrc 的plugins列表
  • autojump
    使用j命令直接快速进入某个目录, 比如 j Downloads -> cd ~/Downloads
    安装: brew install autojump
    激活: 添加 autojump 至 ~/.zshrc 配置文件的插件列表。
  • zsh-syntax-highlighting
    命令高亮插件,命令不再只是同一个颜色了
    安装: brew install zsh-syntax-highlighting
    激活: 添加 zsh-syntax-highlighting 至 ~/.zshrc 配置文件的插件列表。
  • zsh-autosuggestions
    输入时按右方向键→自动补全命令
    安装: brew install zsh-autosuggestions
    激活: 添加 zsh-autosuggestions 至 ~/.zshrc 配置文件的插件列表。

在.zshrc中增加下面插件配置

1
2
3
4
5
6
7
plugins=(
git
extract
autojump
zsh-autosuggestions
zsh-syntax-highlighting
)

最终我的文件内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#一些常见的命令别名
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ll='ls -la'

#CLICOLOR是否开启命令行颜色的显示。CLI是Command Line Interface的缩写。
export CLICOLOR=1

#LSCOLORS是用来设置当CLICOLOR被启用后,各种文件类型的颜色。LSCOLORS的值中每两个字母为一组,
#分别设置某个文件类型的文字颜色和背景颜色。LSCOLORS中一共11组颜色设置,按照先后顺序,分别对以下的文件类型进行设置
export LSCOLORS=ExDxFxDaCxDaDahbadacec

ZSH_THEME="random" #配置每次登陆shell随机主题
plugins=(
git
extract
autojump
zsh-autosuggestions
zsh-syntax-highlighting
)

# Path to your oh-my-zsh installation.
export ZSH=/Users/tonxu/.oh-my-zsh

source $ZSH/oh-my-zsh.sh

# set language
export LANG=en_US.UTF-8

# default editor
export EDITOR='vim'

最后source .zshrc 一下,如果对主题都不敢兴趣,可以自行配置主机名、用户名格式,参考如下。

1
PROMPT="[ %{$fg[yellow]%}%n %{$fg[green]%}@ %{$fg[red]%}%m%{$reset_color%} %{$fg[blue]%}%1~ %{$reset_color%}]%{$fg[magenta]%}%# %{$reset_color%}"

root用户配置

安装步骤相同,只需在/root下创建.zshrc文件添加内容即可。
需要特别注意的是:root用户终端和vim插入中文可能会出现中文乱码问题,出现很多"?",这是因为Mac系统的环境变量没有作用在root用户下,此时需要在root用户的.zshrc文件中添加如下两行第二、第三行配置,重新source就没问题了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#解决root用户终端中文乱码问题设置
export LC_ALL=en_US.UTF-8  
export LANG=en_US.UTF-8

#一些常见的命令别名
alias ll="ls -la"

#这两行决定命令输出文本的颜色,可执行文件为绿色,文件夹为蓝色……
export CLICOLOR=1   
export LSCOLORS=ExDxFxDaCxDaDahbadacec

#配置终端主机名和用户名的显示格式和颜色
autoload -U colors && colors
PROMPT="[ %{$fg[yellow]%}%n %{$fg[green]%}@ %{$fg[red]%}%m%{$reset_color%} %{$fg[blue]%}%1~ %{$reset_color%}]%{$fg[magenta]%}%# %{$reset_color%}"

vim个性化配置

在用户的home目录下新建一个.vimrc文件,增加下面设置即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
colorscheme default     " 设置颜色主题

syntax on " 语法高亮

filetype on " 检测文件的类型

set number " 显示行号
set cursorline " 用浅色高亮当前行
"autocmd InsertLeave * se nocul
"autocmd InsertEnter * se cul

set ruler " 在编辑过程中,在右下角显示光标位置的状态行
set laststatus=2 " 显示状态栏 (默认值为 1, 无法显示状态栏)
set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
" 设置在状态行显示的信息

set tabstop=4 " Tab键的宽度
set softtabstop=4
set shiftwidth=4 " 统一缩进为4

set autoindent " vim使用自动对齐,也就是把当前行的对齐格式应用到下一行(自动缩进)
set cindent " (cindent是特别针对 C语言语法自动缩进)
set smartindent " 依据上面的对齐格式,智能的选择对齐方式,对于类似C语言编写上有用

set scrolloff=3 " 光标移动到buffer的顶部和底部时保持3行距离

set incsearch " 输入搜索内容时就显示搜索结果
set hlsearch " 搜索时高亮显示被找到的文本

set foldmethod=indent " 设置缩进折叠
set foldlevel=99 " 设置折叠层数
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
" 用空格键来开关折叠

" 自动跳转到上次退出的位置
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

Bash shell配置

给爱好Bash的同学参考
新建./bash_profile文件
用户的home目录下创建一个新文件命名为./bash_profile , 在这个文件中添加如下几行代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#alias for some common cmds
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ll='ls -la'

#enables colorin the terminal bash shell export
export CLICOLOR=1
export LSCOLORS=gxfxaxdxcaegedabagacad #洋红色链接文件
export LSCOLORS=ExDxFxDaCxDaDaHbAdAcEc #棕色链接文件
#enables colorfor iTerm
export TERM=xterm-256color

#setup var PS1 for the hostname and username color
PS1="\[\033[01;37m\][\[\033[01;32m\]\u \[\033[01;33m\]@ \[\033[01;31m\]\h \[\033[01;36m\]\w \[\033[01;37m\]]\[\033[01;35m\]\\$ \[\033[0m\]"
  • 更换shell为bash

MAC默认的是zsh shell, 上面的配置是只针对bash,只有bash shell才会加载./bash_profile文件,需要将当前的shell 更改成bash。

1
2
3
4
chsh -s /bin/bash

# 另外,如果您将系统shell默认设置为/bin/sh,则在脚本或则配置文件中将无法使用source命令,因为/bin/sh 相当于 /bin/bash --posix。你需要在配置文件中声明如下提示:
#!/bin/bash --posix