更優雅地使用命令行

zsh

工欲善其事,必先利其器,經過武裝本身的命令行工具,從而更優雅地使用命令行,可使工做更加高效而且有趣。本文將如下幾個方面來介紹命令行的使用技巧和提效工具html

CLI 一鍵呼入呼出

iterm2 是一款徹底免費,爲 MacOS 打造的終端工具,特點功能是能夠開啓熱鍵窗口,達到一鍵呼入呼出的效果linux

效果以下:git

iterm2

詳細設置以下:github

一、首先,進行以下設置web

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

iterm2_set01

二、接着,設置熱鍵,並選擇 Animate showing and hidingFloating window 這兩個選項shell

iterm2_set02

zsh

目前經常使用的 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

經過以下命令,能夠查看當前環境的 shellcurl

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

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)
相關文章
相關標籤/搜索