更优雅地使用命令行

680 阅读3分钟

工欲善其事,必先利其器,通过武装自己的命令行工具,从而更优雅地使用命令行,可以使工作更加高效并且有趣。本文将以下几个方面来介绍命令行的使用技巧和提效工具

CLI 一键呼入呼出

iterm2 是一款完全免费,为 MacOS 打造的终端工具,特色功能是可以开启热键窗口,达到一键呼入呼出的效果

效果如下:

iterm2

详细设置如下:

1、首先,进行如下设置

preferences > Keys > HotKey > Create a Dedicated Hotkey Window...

iterm2_set01

2、接着,设置热键,并选择 Animate showing and hidingFloating window 这两个选项

iterm2_set02

zsh

目前常用的 Linux 系统和 OS X 系统的默认 Shell 都是 bash。oh my zsh 是强化版的 Shell

如果是 Mac OS,默认应该自带了 zsh 了,安装之前可以确认一下

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

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 的优点

1、主题提示信息从用户名和主机名变成了当前目录的名称

2、按 tab 键补全,不仅可以补全命令,也可以补全选项、参数、文件等

3、跳转路径可省略 cd 命令,并可进行路径的首字符匹配

c/k/k/t/c

按下 tab 键之后,会自动补全为如下路径

code/ktsg/ktsg_new/trunk/config

4、当前所在目录下直接输入 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

homebrew

brew 又叫 homebrew,是 Mac 上的软件包管理工具,可以在 Mac 中方便的安装或者卸载软件

下面是 homebrew 的常用命令

brew install git # 安装
brew uninstall wget # 卸载
brew list # 列出已安装的软件

插件推荐

下面是一些插件推荐,插件安装完成后,需要打开 ~/.zshrc,找到 plugins=,然后在里面写需要的插件名。只要修改了此文件,要使用 source ~/.zshrc 来更新配置

快速跳转

autojump 插件实现了目录间快速跳转,想去哪个目录直接 j + 目录名,不用再频繁的 cd

使用 autojump 命令,或使用短命令 j 来跳转到指定目录。要注意的是,只有打开过的目录插件才会记录。所以,使用时间越长,插件才越智能

j directoryName

zsh_02

安装如下:

brew install autojump

然后在 .zshrc 文件中添加如下语句

[[ -s `brew --prefix`/etc/autojump.sh ]] && . `brew --prefix`/etc/autojump.sh

命令提示

使用 zsh-autosuggestions 插件可以在输入命令时提示自动补全(灰色部分),然后按键盘方向右键,即可补全

zsh_01

安装如下:

cd ~/.oh-my-zsh/custom/plugins/
sudo git clone https://github.com/zsh-users/zsh-autosuggestions

语法高亮

使用 zsh-syntax-highlighting 插件,日常用的命令会高亮显示,命令错误显示红色

zsh_03

安装如下:

cd ~/.oh-my-zsh/custom/plugins/
sudo git clone https://github.com/zsh-users/zsh-syntax-highlighting.git

命令更正

使用 thefuck 插件,可以用于命令纠正,输入 fuck 后,可以纠正前一条输错的命令

the_fuck

安装如下:

brew install thefuck

然后在 .zshrc 文件中添加如下语句

eval $(thefuck --alias)

搜索关键词

使用 web-search 插件可以使用搜索引擎进行搜索,比如使用 googlestackoverflow

$ google oh-my-zsh # 使用 google 搜索 oh-my-zsh
$ stackoverflow oh-my-zsh # 使用 stackoverflow 搜索 oh-my-zsh

zsh_web_search

该插件不需要安装,直接在 zshrc 文件中的 plugins 中添加即可

打开远程仓库

使用 git-open 插件,输入 git open 就能够在浏览器中打开一个仓库的 github 页面

git_open

安装如下:

cd ~/.oh-my-zsh/custom/plugins/
sudo git clone https://github.com/paulirish/git-open.git $ZSH_CUSTOM/plugins/git-open

快捷搜索

fzf 插件是一个通用的命令行模糊搜索工具,依靠模糊的关键词,可以快速定位文件

通过 code $(fzf) 命令可以进行文件搜索

fzf

安装如下:

brew install fzf

翻译

translate shell 是一款默认借助谷歌翻译来进行翻译的命令行翻译器

使用 trans 命令可以进行翻译,加上 -sp选项(speak的简写)同时也可以发音

trans

安装如下:

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)

新开通公众号(小火柴的蓝色理想),和小火柴一起,山也迢迢,水也迢迢,到前端的理想中去

qrcode