ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # oh-my-zsh 🆓 开源:https://github.com/robbyrussell/oh-my-zsh 默认的 Bash 是黑白的,没有色彩。而 Oh My Zsh 可以带你进入彩色时代。Oh My Zsh 同时提供一套插件和工具,可以简化命令行操作。后面我们会看到很多介绍,你会看到我爱死这家伙了。 **Oh My Zsh 使用了 Z shell(zsh),一个和 Bash 相似的 Shell,而非 Bash。** 在 Z shell 中,`~/.zshrc`是最重要的配置文件。 # 安装 **curl** 方式: ``` sh -c "$(curl -fsSL https://raw.fastgit.org/ohmyzsh/ohmyzsh/master/tools/install.sh)" ``` Set Zsh as your default shell: ~~~ chsh -s $(which zsh) ~~~ > [oh-my-zsh 手动安装方法](https://github.com/ohmyzsh/ohmyzsh#manual-installation) ## 系统内置的 shell unix 衍生系统的默认 shell 都是 bash 以mac 为例,我们看下系统内置了几种shell ~~~ ➜ /Users/XXX > 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 ~~~ ## `~/.zshrc`重置 ~~~ cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc ~~~ # antigen(个人觉得没必要😊) oh-my-zsh 的管理工具:[antigen](https://github.com/zsh-users/antigen) ## oh-my-zsh 主题设置 1. 可以到~/oh-my-zsh/themes/中查看现有的主题。推荐agnoster。 2. 设置主题为agnoster:编辑`~/.zshrc`,将 ZSH-THEME 项替换为 agnoster 3. 注意agnoster主题需要几个特殊字体的支持(要不然会显示.叉叉),可以从这里下载:https://gist.github.com/qrush/1595572 ## 设置 Powerline 效果 下载 https://github.com/powerline/fonts 里面的一种字体即可,然后安装字体! [Source Code Pro for Powerline](https://github.com/powerline/fonts/tree/master/SourceCodePro) 我自己喜欢自备的 Operator Mono Lig 字体,选中即可; # powerlevel10k Powerlevel10k 是 Zsh 的一个主题。它强调速度、灵活性和开箱即用的体验。 ~~~ brew install romkatv/powerlevel10k/powerlevel10k echo "source $(brew --prefix)/opt/powerlevel10k/powerlevel10k.zsh-theme" >>~/.zshrc ~~~ ## 字体图标 ``` brew tap homebrew/cask-fonts ``` # 常用插件 ## 配置 ``` vim ~/.zshrc // 启用插件,多插件空格分开 plugins=( git z sudo zsh-autosuggestions zsh-syntax-highlighting zsh-better-npm-completion ) source $ZSH/oh-my-zsh.sh // 查看配置的 .oh-my-zsh 插件 ls ~/.oh-my-zsh/custom/plugins ``` ## 推荐插件 * [zsh-history-substring-search](https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/history-substring-search) * [zsh-syntax-highlighting](https://github.com/zsh-users/zsh-syntax-highlighting) * [zsh-autosuggestions](https://github.com/zsh-users/zsh-autosuggestions) * [zsh-better-npm-completion](https://github.com/lukechilds/zsh-better-npm-completion) ## 更多插件可参考 * [github.com/robbyrussel…](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins) * [github.com/unixorn/awe…](https://github.com/unixorn/awesome-zsh-plugins) * [github.com/robbyrussel…](https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins-Overview) # 卸载 卸载 **oh my zsh** 可运行 `uninstall_oh_my_zsh`; 自动更新 **oh my zsh** 可在 **.zshrc** 中添加 `DISABLE_UPDATE_PROMPT=true`; 自动更新 **oh my zsh** 可运行 `upgrade_oh_my_zsh`; # 技巧 按住 ⌘ 键: 1. 可以拖拽选中的字符串; 2. 点击 url:调用默认浏览器访问该网址; 3. 点击文件:调用默认程序打开文件; 4. 如果文件名是 filename:42,且默认文本编辑器是 Mac vim将会直接打开到这一行; 5. 点击文件夹:在 finder 中打开该文件夹; 6. 同时按住 opt 键,可以以矩形选中。 # 快捷键 | 快捷键 | 描述 | | --- | --- | | `ctrl + u ` | 清除当前行 | | `ctrl + h` | 删除光标之前的字符 | | `ctrl + k` | 删除到文本末尾 | | `⌘ + t` | 水平分屏 | | `⌘ + shift + t` | 垂直分屏 | | `⌘ + shift + h` | 列出剪切板历史 | | `ctrl + a` | 切换到行首 | | `ctrl + e` | 切换到行尾 | | `⌘ + arrow`/`⌘+shift + [` | Tab 切换 | | `⌘ + shift + arrow` | 改变 Tab 的顺序 | | `⌘ + Num` | 快速切换到 Tab 上 | | `⌘ + enter` | 最大化/还原 Tab | | `⌘ + /` | 快速定位到光标位置 | | `⌘ + opt + e` | 一屏显示所有窗口 | | `⌘ + r` | 清屏 | > [Mac iTerm2快捷键汇总大全](http://www.mabiji.com/mac/iterm2hotkeys.html) # Alias ## 添加别名 1. 切换到目录:`$ cd ~/.oh-my-zsh/custom` 2. 创建一个文件:`aliases.zsh` 3. 可以用你喜欢的文本编辑器打开文件,添加别名: ~~~bash # An alias to naviage up one directory level alias open='explorer.exe' ~~~ 4. 保存之后,重启命令行,或者运行`$ source ~/.zshrc` ## git alias ``` g=git ga='git add' gaa='git add --all' gapa='git add --patch' gb='git branch' gba='git branch -a' gbd='git branch -d' gbda='git branch --no-color --merged | command grep -vE "^(\*|\s*(master|develop|dev)\s*$)" | command xargs -n 1 git branch -d' gbl='git blame -b -w' gbnm='git branch --no-merged' gbr='git branch --remote' gbs='git bisect' gbsb='git bisect bad' gbsg='git bisect good' gbsr='git bisect reset' gbss='git bisect start' gc='git commit -v' 'gc!'='git commit -v --amend' gca='git commit -v -a' 'gca!'='git commit -v -a --amend' gcam='git commit -a -m' 'gcan!'='git commit -v -a --no-edit --amend' 'gcans!'='git commit -v -a -s --no-edit --amend' gcb='git checkout -b' gcd='git checkout develop' gcf='git config --list' gcl='git clone --recursive' gclean='git clean -fd' gcm='git checkout master' gcmsg='git commit -m' 'gcn!'='git commit -v --no-edit --amend' gco='git checkout' gcount='git shortlog -sn' gcp='git cherry-pick' gcpa='git cherry-pick --abort' gcpc='git cherry-pick --continue' gcs='git commit -S' gd='git diff' gdca='git diff --cached' gdct='git describe --tags `git rev-list --tags --max-count=1`' gdt='git diff-tree --no-commit-id --name-only -r' gdw='git diff --word-diff' gf='git fetch' gfa='git fetch --all --prune' gfo='git fetch origin' gg='git gui citool' gga='git gui citool --amend' ggpull='git pull origin $(git_current_branch)' ggpur=ggu ggpush='git push origin $(git_current_branch)' ggsup='git branch --set-upstream-to=origin/$(git_current_branch)' ghh='git help' gignore='git update-index --assume-unchanged' gignored='git ls-files -v | grep "^[[:lower:]]"' git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk' gk='\gitk --all --branches' gke='\gitk --all $(git log -g --pretty=%h)' gl='git pull' glg='git log --stat' glgg='git log --graph' glgga='git log --graph --decorate --all' glgm='git log --graph --max-count=10' glgp='git log --stat -p' glo='git log --oneline --decorate' globurl='noglob urlglobber ' glog='git log --oneline --decorate --graph' gloga='git log --oneline --decorate --graph --all' glol='git log --graph --pretty='\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit' glola='git log --graph --pretty='\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit --all' glp=_git_log_prettily glum='git pull upstream master' gm='git merge' gmom='git merge origin/master' gmt='git mergetool --no-prompt' gmtvim='git mergetool --no-prompt --tool=vimdiff' gmum='git merge upstream/master' gp='git push' gpd='git push --dry-run' gpoat='git push origin --all && git push origin --tags' gpristine='git reset --hard && git clean -dfx' gpsup='git push --set-upstream origin $(git_current_branch)' gpu='git push upstream' gpv='git push -v' gr='git remote' gra='git remote add' grb='git rebase' grba='git rebase --abort' grbc='git rebase --continue' grbi='git rebase -i' grbm='git rebase master' grbs='git rebase --skip' grep='grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn}' grh='git reset HEAD' grhh='git reset HEAD --hard' grmv='git remote rename' grrm='git remote remove' grset='git remote set-url' grt='cd $(git rev-parse --show-toplevel || echo ".")' gru='git reset --' grup='git remote update' grv='git remote -v' gsb='git status -sb' gsd='git svn dcommit' gsi='git submodule init' gsps='git show --pretty=short --show-signature' gsr='git svn rebase' gss='git status -s' gst='git status' gsta='git stash save' gstaa='git stash apply' gstc='git stash clear' gstd='git stash drop' gstl='git stash list' gstp='git stash pop' gsts='git stash show --text' gsu='git submodule update' gts='git tag -s' gtv='git tag | sort -V' gunignore='git update-index --no-assume-unchanged' gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1' gup='git pull --rebase' gupv='git pull --rebase -v' gwch='git whatchanged -p --abbrev-commit --pretty=medium' gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify -m "--wip--"' history='fc -l 1' l='ls -lah' ``` ## Git 别名 几乎每个人都会使用一些方法比如 Git 别名来提高效率,几乎所有人都会把使用`git st`来代替`git status`。然而这需要手动设置,每个人也都不完全一样。 Oh My Zsh 提供了一套系统别名(alias),来达到相同的功能。比如`gst`作为`git status`的别名。而且 Git 插件是 Oh My Zsh 默认启用的,相当于你使用了 Oh My Zsh,你就拥有了一套高效率的别名,而且还是全球通用的。是不是棒棒哒?下面是一些我常用的别名: |Alias | Command | | --- | --- | | gapa | `git add --patch` | | gc! | `git commit -v --amend` | | gcl | `git clone --recursive` | | gclean | `git reset --hard && git clean -dfx` | | gcm | `git checkout master` | | gcmsg| `git commit -m` | | gco | git checkout | | gd | git diff | | gdca | git diff --cached | | glola | `git log --graph --pretty = format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all` | | gp | git push | | grbc | git rebase --continue | | gst | git status | | gup | git pull --rebase | | gwip | `git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit -m "--wip--"` | 完整列表请参考:https://github.com/robbyrussell/oh-my-zsh/wiki/Plugin:git ## 参考 [终极 Shell——ZSH](https://zhuanlan.zhihu.com/p/19556676) http://blog.csdn.net/iloveyin/article/details/49522449 https://github.com/robbyrussell/oh-my-zsh/wiki