更優雅地使用命令行


zsh

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

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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM