配置一个好看的命令行——macOS下的zsh

7,486 阅读1分钟

前言

macOS命令行默认是bash,切换一个B格更高的zsh来试试吧。

我们看下系统内置了几种 shell

YuhandeMacBook-Pro:blog yuhanliu$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

如果是 linux 系统的话,想要使用 zsh ,需要自己安装。

macOS 预装了zsh ,但是很少有人直接切换过来使用此shell ,因为 zsh 的默认配置及其复杂繁琐,让人望而却步,直到有了oh-my-zsh这个开源项目,让zsh配置降到0门槛。

安装 oh-my-zsh

oh,my Zsh是一个令人愉悦的,开源的,社区驱动的框架,用于管理您的Zsh配置。它捆绑了成千上万的有用功能,助手,插件,主题以及一些让您大喊大叫的东西...

# Via curl:
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
# Via wget
sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

示例:

YuhandeMacBook-Pro:blog yuhanliu$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Cloning Oh My Zsh...
Cloning into '/Users/yuhanliu/.oh-my-zsh'...
remote: Enumerating objects: 1066, done.
remote: Counting objects: 100% (1066/1066), done.
remote: Compressing objects: 100% (988/988), done.
remote: Total 1066 (delta 23), reused 857 (delta 19), pack-reused 0
Receiving objects: 100% (1066/1066), 705.78 KiB | 640.00 KiB/s, done.
Resolving deltas: 100% (23/23), done.

Looking for an existing zsh config...
Using the Oh My Zsh template file and adding it to ~/.zshrc.

Time to change your default shell to zsh:
Do you want to change your default shell to zsh? [Y/n] Y
Changing the shell...
Changing shell for yuhanliu.
Password for yuhanliu: 
Shell successfully changed to '/bin/zsh'.

         __                                     __
  ____  / /_     ____ ___  __  __   ____  _____/ /_
 / __ \/ __ \   / __ `__ \/ / / /  /_  / / ___/ __ \
/ /_/ / / / /  / / / / / / /_/ /    / /_(__  ) / / /
\____/_/ /_/  /_/ /_/ /_/\__, /    /___/____/_/ /_/
                        /____/                       ....is now installed!


Please look over the ~/.zshrc file to select plugins, themes, and options.

p.s. Follow us on https://twitter.com/ohmyzsh

p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh

拓展,如何更改shell

chsh -s /usr/local/bin/zsh

安装完成后的效果:

配置 zsh

zsh的配置几乎都在~/.zshrc下面。

主题

~/.oh-my-zsh/themes存在各式各样的主题文件,每个主题的样子你可以通过此网址进行查看,选好样式后,在.zshrc中的ZSH_THEME设置成你所选用的主题。

修改主题为:

ZSH_THEME="af-magic"

想让终端看起来透明,需要在偏好设置中切换为Pro主题。

插件

插件部分,在此网站有详细介绍。

要使用插件,你只需要改写 ~/.zshrc 文件。

例如:

plugins=(git autojump osx node npm nvm vscode)  //zsh 使用的插件

需要安装一些插件:

brew install autojump  # 之后便可以使用 j <dirspec> 、j --stat

autojump

j <dirspec>:跳到最常使用的匹配路径

j --stat:打印存储的路径及权重

  • 支持 tab-completion.
  • 兼容 Bash 4 and zsh.

OSX

命令 描述
tab 在新tab页打开此路径
ofd 在Finder窗口打开此路径
pfd 打印最前面的Finder窗口的路径
cdf cd到当前Finder窗口的路径
showfiles Finder中显示隐藏的文件
hidefiles 隐藏Finder中的隐藏文件
itunes 控制iTunes. User itunes -h for usage details
spotify 控制Spotify and search by artist, album, track and etc.

vscode

别名 命令 描述
vsc code . 在 VS code 中打开当前文件夹
vsca dir code --add dir 增加文件夹到最后的活跃窗口
vscd file file code --diff file file 互相比较两个文件
vscn code --new-window 强制开启一个新窗口
vscr code --reuse-window 强制在最后活跃窗口中开启一个文件或文件夹

后记

从 bash 迁移到 zsh

首先需要把配置复制一下:

cat .bash_profile  >> .zshrc 
source .zshrc 

# For NVM
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles

关于主题中的各种颜色

一般文件夹是蓝色,但如果是777的权限就会有黄底,所以一般都用644的权限也即rw-r--r--

写文不易,感谢赞赏!


本文地址:blog.dfface.com/2019/a36c87… dfface 的版权声明:所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处,严禁商业用途!