LoginSignup
298
279

More than 5 years have passed since last update.

zshのhistoryを便利に使うためのTips

Last updated at Posted at 2012-12-26

ここで言うhistoryとは、コマンド履歴のことです。シェルはもちろんzshです。この投稿を読んでくれている人は、多分zshを使っていると思いますが、まだ入れてないという人は以下の様な手順を踏むと、zshをインストールすることができます。

zshのインストール

Windowsユーザーの場合

Cygwinというものを使います。まず、Ctrl+Rを押して、powershellと入力し、Enterを押しましょう。そして、以下のスクリプトを実行します。または、こちらからダウンロードして、実行ファイルをダブルクリックするのもいいかもしれません。その際は、Wgetだけにはチェックを入れておきましょう。

function Install-Cygwin {
   param ( $TempCygDir="$env:temp\cygInstall" )
   if(!(Test-Path -Path $TempCygDir -PathType Container))
    {
       $null = New-Item -Type Directory -Path $TempCygDir -Force
    }
   $client = new-object System.Net.WebClient
   $client.DownloadFile("http://cygwin.com/setup.exe", "$TempCygDir\setup.exe" )
   Start-Process -wait -FilePath "$TempCygDir\setup.exe" -ArgumentList "-q -n -l $TempCygDir -s http://mirror.nyi.net/cygwin/ -R c:\Cygwin"
   Start-Process -wait -FilePath "$TempCygDir\setup.exe" -ArgumentList "-q -n -l $TempCygDir -s http://mirror.nyi.net/cygwin/ -R c:\Cygwin -P openssh"
   Start-Process -wait -FilePath "$TempCygDir\setup.exe" -ArgumentList "-q -n -l $TempCygDir -s http://mirror.nyi.net/cygwin/ -R c:\Cygwin -P wget"
}
Install-Cygwin

インストールできたら、早速起動し、apt-cygコマンドを使えるようにします。

wget http://apt-cyg.googlecode.com/svn/trunk/apt-cyg 

mv apt-cyg /usr/bin

chmod +x /usr/bin/apt-cyg

最後に、zshをインストールして使えるようにします。

apt-cyg install zsh vim

sed -i 's/bash/zsh/g' /Cygwin.bat

touch ~/.zshrc

/Cygwin.bat

詳しくは、こちらの記事が参考になります。

Linuxユーザーの場合

Ubuntu
sudo apt-get install -y zsh

sudo chsh -s /bin/zsh

Macユーザーの場合

Macユーザーの場合は、デフォルトでインストールされていると思います。よって、chshコマンドだけでもOKです。しかし、最新版を使いたかったら、Homebrewなどを使ってインストールします。

Homebrewの導入
/usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
brew install zsh

which zsh

chsh -s /usr/local/bin/zsh

zshのhistoryを便利に使うためのTips

historyを使う

基本的には、C-rを押すと、history検索できます。

~/.zshrc
# 履歴ファイルの保存先
export HISTFILE=${HOME}/.zsh_history

# メモリに保存される履歴の件数
export HISTSIZE=1000

# 履歴ファイルに保存される履歴の件数
export SAVEHIST=100000

# 重複を記録しない
setopt hist_ignore_dups

# 開始と終了を記録
setopt EXTENDED_HISTORY

全履歴を一覧表示する

~/.zshrc
function history-all { history -E 1 }

ここで、特定のキーワードのみを覚えていた場合、それらをつなぎあわせて検索することで、複雑なワンライナーの再利用も簡単になります。

history-all | grep find | grep tr

historyの選択

historyを選択したいのならzaw.zshが便利です。Gitをインストールした状態で以下のように設定します。

echo -e "function mkcd(){mkdir -p \$1 && cd \$1}" >> ~/.zshrc && exec $SHELL

mkcd ~/zsh_plugin/

git clone git://github.com/zsh-users/zaw.git

echo "source ${PWD}/zaw/zaw.zsh" >> ~/.zshrc && exec $SHELL

デフォルトでは、Ctrl-x ;で起動します。

以下のようなキーバインドを設定することで、C-hを押すと、容易にhistory一覧を表示し、選択できるようになります。ちなみに、auto-fu.zshなどを導入している場合は、C-gを押してから入力しましょう。

~/.zshrc
bindkey '^h' zaw-history

historyの検索

入力に合わせてhistoryを検索したい場合は、以下のキーバインドを設定しておくと簡単に検索できて便利です。

~/.zshrc
# history search
bindkey '^P' history-beginning-search-backward
bindkey '^N' history-beginning-search-forward

historyの共有

~/.zshrc
setopt share_history

その他、便利な設定

# ヒストリに追加されるコマンド行が古いものと同じなら古いものを削除
setopt hist_ignore_all_dups

# スペースで始まるコマンド行はヒストリリストから削除
setopt hist_ignore_space

# ヒストリを呼び出してから実行する間に一旦編集可能
setopt hist_verify

# 余分な空白は詰めて記録
setopt hist_reduce_blanks  

# 古いコマンドと同じものは無視 
setopt hist_save_no_dups

# historyコマンドは履歴に登録しない
setopt hist_no_store

# 補完時にヒストリを自動的に展開         
setopt hist_expand

# 履歴をインクリメンタルに追加
setopt inc_append_history

# インクリメンタルからの検索
bindkey "^R" history-incremental-search-backward
bindkey "^S" history-incremental-search-forward
298
279
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
298
279