在 oh-my-zsh 中自訂常用的 alias (重啟 terminal 後仍然有效)


Posted by huiming on 2021-08-24

摘要

  1. ~/.oh-my-zsh/plugins/git/git.plugin.zsh 中有內建多組 git 可以使用。
  2. ~/.oh-my-zsh/custom 路徑下建立自己的 alias.zsh 就能設定常用的 aliases。

git alias

oh-my-zsh 本身自帶許多 git 的 aliases,可以在 ~/.oh-my-zsh/plugins/git/git.plugin.zsh 裡面進行調整,雖然這些 aliases 數量壯觀但實際用到的還是只有那幾個,以下記錄幾個常用的指令:

# 看狀態
alias gs='git status'
alias gd='git diff'
alias gdca='git diff --cached'

# 分支操作
alias gb='git branch'
gb -m <new-branch-name> => rename branch
gb -D <branch-name>     => delete branch

alias gco='git checkout'
gco -b <branch-name> => create & checkout 到新建立的 branch

# commit
alias gall='git add --all'
alias gcm='git commit -m'
也可以直接 gcam <commit-message> 啦,只是我習慣 commit 前看一下 staged 的內容

alias grh^='git reset head^'
退回上一個 commit

alias gcn!='git commit -v --no-edit --amend'
把這次的修改併到上一個 commit

alias glo='git log --oneline --decorate'

# remote branch
alias gl='git pull'
alias ggpush='git push origin "$(git_current_branch)"'

建立 oh-my-zsh custom aliases

開發專案時,package.json 裡面通常會有啟動專案的 scripts,例如 npm run start, yarn start:dev, yarn build 等等,雖然已經很簡短了但人就是沒有最懶,只有更懶,alias 給他寫起來。

最快的方法就是在 terminal 直接輸入:alias ysd='yarn start:dev 就能直接用了,但這招的效果只要在 terminal 關掉重啟後就會消失了,所以我之前還傻傻地每次打開 terminal 都重新輸入一次,雖不是很麻煩但心裡不是那麼的爽快。(os: 金魚腦 terminal,為什麼 git 的指令可以一般的指令不行?)

關掉 terminal 後就忘了剛剛的設定

後來找到這篇,原來 oh-my-zsh 確實可以自訂 git 以外的 alias,依照該文的步驟:

  1. 進入 ~/.oh-my-zsh/custom
  2. 建立 alias.zsh (檔名自取,副檔名要 .zsh)
  3. 用編輯器依照 alias <alias>='do something' 的格式指定
  4. 重啟 terminal 或用 source ~/.zshrc 後就可以使用

終於不用每次開 terminal 還要手動指定了,我就懶。

References

  1. Day 15 - 使用 zsh 的 alias 讓指令縮短
  2. Oh-my-zsh 自帶的 git aliases
  3. oh-my-zsh and persistent aliases

#oh-my-zsh #alias #Git







Related Posts

[DAY4] 初學Command Line

[DAY4] 初學Command Line

智能合約(三) - IDE 開發環境

智能合約(三) - IDE 開發環境

跟爺爺奶奶們度過開心快樂的夏令營

跟爺爺奶奶們度過開心快樂的夏令營


Comments