Mac终端神器zsh

背景介绍

在unix 内核的操做系统中,固然如今衍生出好多分支,linux、OS X都算。 shell 就算和上面这些系统内核指令打交道的一座桥梁,咱们经过键盘输入一种本身容易记忆识别的符号标识(shell 命令),而后shell解析这种命令再反馈给内核去执行一系列操做。linux

那么zshshell有什么关系呢?git

其实zsh也是一种shell,可是并非咱们系统默认的shell,unix衍生系统的默认shell都是bash。github

查看已安装shell

cat /etc/shells查看Mac上已有的shell,一共有6种shell

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
复制代码

chsh -s /bin/zsh更改使用的shell种类,输入Mac密码来确认更改shell种类,退出终端,从新进入便可。vim

注意:由bash切换到zsh.bash_profile里面的环境变量的配置便不生效了,须要在~/.zshrc中从新配置。bash

下载iTerm2

官网下载:www.iterm2.com/app

安装完成后,在/bin目录下会多出一个zsh的文件。字体

Mac系统默认使用dash做为终端,可使用命令修改默认使用zsh:ui

chsh -s /bin/zsh
复制代码

shell的配色

// 在用户目录下执行git clone命令,clone到本地
git clone https://github.com/altercation/solarized
// 安装目录:/Users/用户/solarized/iterm2-colors-solarized
cd solarized/iterm2-colors-solarized/
open .
// 执行
solarized/osx-terminal.app-colors-solarized/Solarized Dark ansi.terminal
复制代码

在打开的finder窗口中,双击Solarized Dark.itermcolors和Solarized Light.itermcolors便可安装明暗两种配色spa

再次进入 iTerm2 -> Preferences -> Profiles -> Colors -> Color Presets中根据我的喜爱选择。

安装oh my zsh

Oh My Zsh 是对主题的进一步扩展

git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
复制代码

详细信息能够参考oh-my-zsh的GitHub页面

配置zsh

vi ~/.zshrc
复制代码
  • 修改主题

zsh 主题列表:github.com/robbyrussel…

# ZSH_THEME="robbyrussell"
# ZSH_THEME="agnoster"
# 我的喜欢avit主题
ZSH_THEME="avit" 
复制代码
  • 设置别名
alias zshconfig='vi ~/.zshrc'
alias vimconfig='vi ~/.vimrc'
alias ll='ls -l'
alias vi='vim'
alias subl='open -a "Sublime Text"'
复制代码
  • 配置 Meslo 字体

使用上面的主题,须要 Meslo 字体支持,要否则会出现乱码的状况,字体下载地址:Meslo LG M Regular for Powerline.ttf,下载好以后,直接在 Mac OS 中安装便可。而后打开 iTerm2,按cmd + ,,打开 Preferences 配置界面,而后Profiles -> Text -> Font -> Chanage Font,选择Meslo LG M Regular for Powerline字体。

另外,VS Code 的终端字体,也须要进行配置,打开 VS Code,按cmd + ,,打开用户配置,增长一项

"terminal.integrated.fontFamily": "Meslo LG M for Powerline"
复制代码
  • 声明高亮

特殊命令和错误命令,会有高亮显示。使用 Homebrew 安装

brew install zsh-syntax-highlighting
复制代码

安装成功以后,编辑vim ~/.zshrc文件,在最后一行增长下面配置

source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
复制代码
  • 自动建议填充

这个功能是很是实用的,能够方便咱们快速的敲命令。 配置步骤,先克隆zsh-autosuggestions项目,到指定目录:

cd ~
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
复制代码

而后编辑vim ~/.zshrc文件,找到plugins配置,增长zsh-autosuggestions插件

plugins=(git osx zsh-autosuggestions)
复制代码

注:上面声明高亮,若是配置不生效的话,在plugins配置,再增长zsh-syntax-highlighting插件试试。

有时候由于自动填充的颜色和背景颜色很类似,以致于自动填充没有效果,咱们能够手动更改下自动填充的颜色配置,我修改的颜色值为:586e75,示例

  • 左右键跳转

主要是按住option + → or ←键,在命令的开始和结尾跳转切换,本来是不生效的,须要手动开启下。

打开 iTerm2,按cmd + ,键,打开 Preferences 配置界面,而后Profiles → Keys → Load Preset... → Natural Text Editing,就能够了。

  • iTerm2 隐藏用户名和主机名

有时候咱们的用户名和主机名太长,终端显示的时候会很很差看(上面图片中能够看到),咱们能够手动去除。编辑vim ~/.zshrc文件,增长DEFAULT_USER="xishuai"配置,示例:

  • 启用命令纠错功能
# Uncomment the following line to enable command auto-correction.
ENABLE_CORRECTION="true"
复制代码
  • 插件
  1. git
  2. osx
  3. autojump
plugins=(git osx autojump)
复制代码
相关文章
相关标签/搜索