工欲善其事,必先利其器,经过武装本身的命令行工具,从而更优雅地使用命令行,可使工做更加高效而且有趣。本文将如下几个方面来介绍命令行的使用技巧和提效工具html
iterm2 是一款彻底免费,为 MacOS 打造的终端工具,特点功能是能够开启热键窗口,达到一键呼入呼出的效果linux
效果以下:git
详细设置以下:github
一、首先,进行以下设置web
preferences > Keys > HotKey > Create a Dedicated Hotkey Window...
二、接着,设置热键,并选择 Animate showing and hiding
和 Floating window
这两个选项shell
目前经常使用的 Linux 系统和 OS X 系统的默认 Shell 都是 bash。oh my zsh
是强化版的 Shell浏览器
若是是 Mac OS,默认应该自带了 zsh
了,安装以前能够确认一下bash
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
经过以下命令,能够查看当前环境的 shell
curl
echo $SHELL
可使用以下的命令进行 shell
切换,要特别注意的是,切换 shell
后,重启 CLI 才能生效工具
chsh -s /bin/bash # 切换bash chsh -s /bin/zsh # 切换zsh
接下来,开始安装 oh-my-zsh
,要特别注意的是,不能使用官网的地址进行安装,不然会提示
Failed to connect to raw.github.com port 443: Connection refused
而应该用以下的地址进行安装
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
安装完成后,会提示是否将 zsh
设置为默认 shell,选择 Y
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 root. 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
下面简单介绍下 oh-my-zsh
的优势
一、主题提示信息从用户名和主机名变成了当前目录的名称
二、按 tab 键补全,不只能够补全命令,也能够补全选项、参数、文件等
三、跳转路径可省略 cd 命令,并可进行路径的首字符匹配
c/k/k/t/c
按下 tab 键以后,会自动补全为以下路径
code/ktsg/ktsg_new/trunk/config
四、当前所在目录下直接输入 d ,将会展现出历史访问目录列表(最近20个),而且左侧加了数字索引
$ d 0 ~/Desktop/md/blog 1 ~/Desktop/md 2 ~/Desktop 3 ~
使用 git 别名配置,可让 git 体验更简单
能够经过 git config
命令来为命令 git branch
设置一个别名
$ git config --global alias.b branch
这意味着,当要输入 git branch
时,只须要输入 git b
就行了
更简单的方式,是直接编辑 ~/.gitconfig
文件,能够达到相同的效果
[alias] b = branch
但若是只想输入 gb
,就想实现 git branch
相同的效果,则须要使用 linux 的别名功能
实际上,zsh
已经默认设置了 git 的插件,文件路径以下
.oh-my-zsh/plugins/git/git.plugin.zsh
下面是一些经常使用的配置
alias g='git' alias ga='git add' alias gb='git branch' alias gc='git commit -v' alias gca='git commit -v -a' alias gcam='git commit -a -m' alias gcb='git checkout -b' alias gcmsg='git commit -m' alias gco='git checkout' alias gd='git diff' alias gl='git pull' alias glog='git log --oneline --decorate --graph' alias gloga='git log --oneline --decorate --graph --all' alias gp='git push' alias gsb='git status -sb' alias gst='git status'
使用 gst
的效果以下
$ gst On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: html_backup.md new file: t.html
brew 又叫 homebrew
,是 Mac 上的软件包管理工具,能够在 Mac 中方便的安装或者卸载软件
下面是 homebrew
的经常使用命令
brew install git # 安装 brew uninstall wget # 卸载 brew list # 列出已安装的软件
下面是一些插件推荐,插件安装完成后,须要打开 ~/.zshrc
,找到 plugins=
,而后在里面写须要的插件名。只要修改了此文件,要使用 source ~/.zshrc
来更新配置
autojump
插件实现了目录间快速跳转,想去哪一个目录直接 j + 目录名
,不用再频繁的 cd
了
使用 autojump
命令,或使用短命令 j
来跳转到指定目录。要注意的是,只有打开过的目录插件才会记录。因此,使用时间越长,插件才越智能
j directoryName
安装以下:
brew install autojump
而后在 .zshrc
文件中添加以下语句
[[ -s `brew --prefix`/etc/autojump.sh ]] && . `brew --prefix`/etc/autojump.sh
使用 zsh-autosuggestions
插件能够在输入命令时提示自动补全(灰色部分),而后按键盘方向右键,便可补全
安装以下:
cd ~/.oh-my-zsh/custom/plugins/ sudo git clone https://github.com/zsh-users/zsh-autosuggestions
使用 zsh-syntax-highlighting
插件,平常用的命令会高亮显示,命令错误显示红色
安装以下:
cd ~/.oh-my-zsh/custom/plugins/ sudo git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
使用 thefuck
插件,能够用于命令纠正,输入 fuck
后,能够纠正前一条输错的命令
安装以下:
brew install thefuck
而后在 .zshrc
文件中添加以下语句
eval $(thefuck --alias)
使用 web-search
插件可使用搜索引擎进行搜索,好比使用 google
或 stackoverflow
$ google oh-my-zsh # 使用 google 搜索 oh-my-zsh $ stackoverflow oh-my-zsh # 使用 stackoverflow 搜索 oh-my-zsh
该插件不须要安装,直接在 zshrc
文件中的 plugins
中添加便可
使用 git-open
插件,输入 git open
就可以在浏览器中打开一个仓库的 github
页面
安装以下:
cd ~/.oh-my-zsh/custom/plugins/ sudo git clone https://github.com/paulirish/git-open.git $ZSH_CUSTOM/plugins/git-open
fzf
插件是一个通用的命令行模糊搜索工具,依靠模糊的关键词,能够快速定位文件
经过 code $(fzf)
命令能够进行文件搜索
安装以下:
brew install fzf
translate shell
是一款默认借助谷歌翻译来进行翻译的命令行翻译器
使用 trans
命令能够进行翻译,加上 -sp
选项(speak的简写)同时也能够发音
安装以下:
brew install translate-shell
上面的插件安装完成后,.zshrc
文件的插件部分的相关配置以下
plugins=( git web-search autojump zsh-syntax-highlighting zsh-autosuggestions git-open fzf ) # autojump [[ -s `brew --prefix`/etc/autojump.sh ]] && . `brew --prefix`/etc/autojump.sh # thefuck eval $(thefuck --alias)