UNIX系OS ( Mac / Linux / FreeBSD )のシェルコマンドに関する各種メモ書き

unixのシェル( bash )の便利な設定(1文字ショートカットとヒストリ)。gitブランチ表示

● lsコマンドを短縮する

unixシェルで一番よく使うコマンドの代表格といえば

ls -la

だと思います。 これを短縮して

l [Enter]

で実行できると便利なので以下のようにbash のエイリアスを設定します。

ホームディレクトリ内の .bash_profile または .bashrc に記述

# alias
alias ll='ls -la'
alias  l='ls -laF'
alias  c='cd'
alias  h='history'
alias  gr='grep'
alias hg='history | grep '
alias  g='git'
alias  d='docker'
alias  r='rsub '
alias  v='vim '
alias gp='git plog'
alias gits='git status -uall'
alias pa='php artisan'
alias localip='ipconfig getifaddr en0'
alias globalip='curl ifconfig.io -4'

● プロンプトに現在のパス名、ユーザー名を常に表示させる

またシェルのプロンプトに 「現在のディレクトリパス名」「ユーザー名」を表示させると便利なので以下のように設定します。

ホームディレクトリ内の .bash_profile または .bashrc に記述

# prompt
PS1='[$USER@\H $PWD]$ '

● Unixのシェルにパスを追加

PATH="$PATH":/usr/local/hogehoge  ←追加

とするとパス「/usr/local/hogehoge」を追加できます。

● bashのヒストリ記憶サイズを変更する

export HISTSIZE=50000
export HISTFILESIZE=50000

● 2連続で実行した履歴を何個も残さない( HISTCONTROL その1 )

export HISTCONTROL=ignoredups

● コマンドの前にスペースを入れると履歴に残さない( HISTCONTROL その2 )

export HISTCONTROL=ignorespace

● 全履歴を見て重複コマンドを削除する( HISTCONTROL その3 )

export HISTCONTROL=erasedups

● 上記3つを全てセット( HISTCONTROL その1+2+3 )

export HISTCONTROL=ignoredups:ignorespace:erasedups

● 起動時のシェルを bash に変更する

whereis bash

で bash のパスを調べて、( 例: /usr/local/bin/bash )

chsh -s /usr/local/bin/bash

とします。

● git 最新版のインストール と git-completion , git-prompt のインストール

brew install git
git --version

wget が無い場合は 事前に brew install wget しておきましょう。

git-completion.bashのインストール

wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion.bash
chmod a+x ~/.git-completion.bash
echo "source ~/.git-completion.bash" >> ~/.bash_profile
source ~/.bash_profile

git-prompt.sh のインストール

wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/.git-prompt.sh
chmod a+x ~/.git-prompt.sh
echo "source ~/.git-prompt.sh" >> ~/.bash_profile
source ~/.bash_profile

.bash_profile に設定を記述

source ~/.git-completion.bash
source ~/.git-prompt.sh

# git-prompt設定
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWUPSTREAM=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWSTASHSTATE=1

# prompt
PS1='[$USER@\H $PWD]\033[0;32m\]$(__git_ps1)\[\033[0m\] $ '

gitの表示は以下のようになります。

設定値 意味
GIT_PS1_SHOWDIRTYSTATE=true addされていない変更の存在を「*」で示す
commitされていない変更の存在を「+」で示す
GIT_PS1_SHOWUNTRACKEDFILES=true addされていない新規ファイルの存在を「%」で示す
GIT_PS1_SHOWSTASHSTATE=true stashがある場合は「$」で示す
GIT_PS1_SHOWUPSTREAM=auto upstreamと同期している場合は「=」で示す
upstreamより進んでいる場合は「>」で示す
upstreamより遅れている場合は「<」で示す

引用 : https://qiita.com/ryoichiro009/items/7957df2b48a9ea6803e0

● 上記の設定全部をコピペ用

# alias
alias ll='ls -la'
alias  l='ls -laF'
alias  c='cd'
alias  h='history'
alias  gr='grep'
alias hg='history | grep '
alias  g='git'
alias  d='docker'
alias  r='rsub '
alias  v='vim '
alias gp='git plog'
alias gits='git status -uall'
alias pa='php artisan'
alias localip='ipconfig getifaddr en0'
alias globalip='curl ifconfig.io -4'

# prompt
# PS1='[$USER@\H $PWD]$ '
PS1='[$USER@\H $PWD]\033[0;32m\]$(__git_ps1)\[\033[0m\] $ '

# history
export HISTSIZE=50000
export HISTFILESIZE=50000
export HISTCONTROL=ignoredups:ignorespace:erasedups

### 以下は必要なら追加

# git日本語文字化け対策
export GIT_PAGER="LESSCHARSET=utf-8 less"

# vanila js で jest を実行する時に import / export を使用可能にする
# package.json に 「 "type": "module"」 も追加すること
export NODE_OPTIONS=--experimental-vm-modules

# show git branch 
source ~/.bash_git_prompt

● linuxの場合

linuxの場合は git-completion.bash, git-prompt.sh が 標準でインストールされていることが多いので、まずは検索しましょう

find / -name git-completion.bash
find / -name git-prompt.sh

.bash_profile に設定します。

/usr/share/doc/git/contrib/completion/git-completion.bash
/usr/share/git-core/contrib/completion/git-prompt.sh

● gitリポジトリ内にいる時は gitのブランチ名を表示させる(古いやり方)

~/.bash_git_prompt を以下の内容で保存

# .bash_prompt
function git_branch() {
  s=$(git branch --no-color 2>/dev/null | sed -ne "s/^\* \(.*\)$/\1/p")
  if [ ! "$s" = "" ]; then
    echo "($s)"
  fi
}

export PS1='[$USER@\H $PWD]\033[0;32m\]$(git_branch)\[\033[0m\] $ '

.bash_profileの一番最後に次の1行を追加する

# show git branch 
source ~/.bash_git_prompt

これで git リポジトリ内にいるときは現在のブランチ名を表示させます。 git リポジトリ内にいない時は何を表示しません。

関連エントリー

No.95
03/04 09:06

edit

シェル