シェルコマンドTips

shell
Author

Ryo Nakagami

Published

2025-04-29

シェルスクリプト便利コマンドリスト

直前のコマンドの引数呼び出し
コマンド 動作 ショートカット
!^ 直前のコマンドの最初の引数
!$ 直前のコマンドの最後の引数 esc + . または Alt + .
!:N 直前のコマンドのN番目の引数
!* 直前のコマンドのすべての引数

Alt + . の特徴

  • Altを押しながら.を連続で入力すると,一回目は直前,二回目は2回前の最後の引数をsuggestしてくれます.
  • esc+ .もセットで押せば同じような挙動となりますが,入力しづらいので Alt + . の方が好みです

Zsh Terminal用ショートカットリスト

Cursor moving
ショートカット keybind setting 動作
ctrl + bindkey '^[[1;5C' backward-word cursor backward by one word
alt + B default cursor backward by one word
ctrl + bindkey '^[[1;5D' forward-word cursor forward by one word
alt + F default cursor forward by one word
ctrl + A default ライン先頭へ移動
home default ライン先頭へ移動
ctrl + E default ライン末尾へ移動
end default ライン末尾へ移動
Search
ショートカット keybind setting 動作
ctrl + R default Reverse search in history
ctrl + S default Forward search in history
Editing
ショートカット keybind setting 動作
ctrl + K default カーソル位置から後ろのwordsをすべて削除
ctrl + U default ライン全消し
ctrl + Y default 削除した文字列をペースト
ctrl + W default カーソル位置からword block先頭までを削除
Git
ショートカット keybind setting 動作
ctrl + G then ctrl + A bindkey "^G^A" _git_add git add -u
ctrl + G then ctrl + home "^G^[[1;5H" _git_cd cd "$(git root)"
ctrl + G then ctrl + S bindkey "^G^S" _git_status git status -sb

git status -sbコマンド

git statusgit status -sbの違いは出力形式にあります

git status

  • 現在のブランチ、ステージ済み・未ステージの変更、未追跡ファイルなどを詳細に表示
% git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  modified:   file.txt

git status -sb

  • 短くスクリプト向けの要約を表示
    • -s で各ファイルの状態を2文字コードで表示(例: Mは変更)
    • -b で現在のブランチと追跡情報を先頭に追加
% git status -sb
## main...origin/main
 M file.txt

Appendix-1: ANSI escape sequence

シンボル 対応コマンド 説明
^[[ ESC + [
1;5 modifier (5 = Ctrl)
^A Ctrl+A
^G Ctrl+G ASCII 7, BEL, bell character
^S Ctrl+S ASCII 19, XOFF, used for terminal flow control to pause output
C Cursor Right
D Cursor Left
H Home key

Appendix-2: custom keybind setup

以下のファイルを .zshrc に読み込ませています

custom_keybind.sh
#!/bin/zsh
#---------------------------------------------------
# cursor moving command
#---------------------------------------------------
bindkey '^[[1;5D' backward-word
bindkey '^[[1;5C' forward-word


#---------------------------------------------------
# git-related command
#---------------------------------------------------
function _git_cd() {
    echo "cd $(git root)"
    cd "$(git root)"
    zle accept-line
}
zle -N  _git_cd

function _git_status() {
    echo "git status -sb" 
    git status -sb
    zle accept-line
    #zle reset-prompt
}
zle -N  _git_status  # _git_status関数をgit_status widgetとして登録

function _git_add() {
    echo "git add -u" # promptにgit add -uを表示
    git add -u
    zle accept-line
    
}
zle -N _git_add  # _git_status関数をgit_status widgetとして登録

function _git_commit_amend() {
    echo "git commit --amend --no-edit" # promptにgit commit --amend --no-editを表示
    git commit --amend --no-edit
    zle accept-line
    
}
zle -N _git_commit_amend  # _git_commit_amend関数をgit_status widgetとして登録

function _git_pull() {
    echo "git pull" # promptにgit add -uを表示
    git pull
    zle accept-line
    
}
zle -N _git_pull  # _git_status関数をgit_status widgetとして登録

function _git_push() {
    echo "git push" # promptにgit add -uを表示
    git push
    zle accept-line
    
}
zle -N _git_push  # _git_status関数をgit_status widgetとして登録

#---------------------------------------------------
# vscode-related command
#---------------------------------------------------
function _vscode_cd() {
    echo "code-cd" # promptにgit add -uを表示
    code-cd
    zle accept-line
    
}
zle -N _vscode_cd  # _git_status関数をgit_status widgetとして登録


#---------------------------------------------------
# bindkey setup
#---------------------------------------------------
# git
bindkey "^G^A" _git_add
bindkey "^G^[[1;5H" _git_cd       # Ctrl + g + Home
bindkey "^G^M" _git_commit_amend
bindkey "^G^P" _git_push
bindkey "^G^l" _git_pull
bindkey "^G^S" _git_status

# vscode related
bindkey "^V^[[1;5H" _vscode_cd       # Ctrl + v + Home