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のブランチ名を表示させる

~/.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 リポジトリ内にいない時は何を表示しません。

引用:https://www.ninton.co.jp/archives/2373

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

# 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]$ '

# 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

関連エントリー

No.95
05/09 14:28

edit

シェル