LoginSignup
28
23

More than 5 years have passed since last update.

zsh,autojump, pecoで作業効率を上げたい

Last updated at Posted at 2015-08-27

一回一回パスを打ち込んで探索するのが面倒だなーと思って

version確認

cat /etc/redhat-releaseでcentosのバージョン確かめれる.

$ cat /etc/redhat-release
CentOS release 6.4 (Final)

これにzsh,autojump, pecoを入れていく.

zsh

zsh(Z Shell)はUnixのコマンドシェルの1つ(Wiki参照)

使っていてやっぱり補完がいいなと思う(月並みな感想ですが)

zshのインストール

yum -y install zsh

これでインストールできる.

入ったか確認する.

$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/dash
# 追加された
/bin/zsh

zshをインストールしたあとは,設定ファイルの.zshrcをいじっていくのですが,テンプレートとしてoh-my-zshを入れると楽になる.

oh-my-zshの導入

git clone https://github.com/robbyrussell/oh-my-zsh.git

これでカレントディレクトリに.oh-my-zshが入る

その後の手順をとりあえず一通り書く

$cd .oh-my-zsh

$cp templates/zshrc.zsh-template ~/.zshrc

これでテンプレートをそのまま自分の設定ファイルにできた.

いきなりcpするのは怖いなって人はあらかじめ.zshrcのコピーを取っておけばいいと思う.

.zshrc
# oh-my-zshどこにあるの?
export ZSH=$HOME/.oh-my-zsh

# テーマ何にするの?
ZSH_THEME="cloud"

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

ZSH_THEMEこのページでサンプルが見れるから好みのテーマに適宜変更すればいいと思う.

設定を変えた後はsource .zshrc
これは次以降かいてません

auto-jump

ディレクトリ探索が楽になる.
自分がよく使うパス(使ったことのあるパス)を記憶してよく使う順に候補を出してくれる.

例をあげるとこんな感じ

$ j do[tab]
do__1__/Users/shu/Documents
do__2__/Users/shu/Downloads
・
・

インストール

wting/autojumpによるとPython2.6以上が必要らしい.

$ python --version
Python 2.6.6

大丈夫だった.

git clone https://github.com/wting/autojump.git

$ cd autojump
$ ./install.py
・
・
・
[[ -s $HOME/.autojump/etc/profile.d/autojump.sh ]] && source /home/shu-/.autojump/etc/profile.d/autojump.sh

    autoload -U compinit && compinit -u
・
・

こんな感じになる.
とりあえず言われた通り.zshrcに記述してリスタート

ここで注意だが,source .zshrcだとうまくいかなかった.
一回ログアウトしてログインしなおすと使えるようになる.

確認

とりあえず適当にどっかにcdして,それがちゃんとjコマンドででてくるか確かめる.

$ cd autojump
$ cd
$ j a[tab]
a__1__/home/shu-/autojump  a__2__/home/shu-/autojump

なんか2個できたけどできた.

これは階層関係なくどこにでも飛べます

$ pwd
/usr/share/tomcat7

$ j a[tab]

$ pwd
/home/shu-/autojump

こんな感じ

peco

コマンド履歴が見やすくなる

例えばこんな感じ
control + r

QUERY> sudo                                                                                                                          IgnoreCase [210 (1/10)]
sudo yum install autojump-zsh
sudo yum autojump-zsh
sudo yum -y autojump-zsh
sudo service httpd restart
・
・

QUERY> に入力した文字でフィルタリングしてくれる

詳しくは peco/peco

インストール

wget でダウンロードする.

wget -O peco.tar.gz https://github.com/peco/peco/releases/download/v0.3.3/peco_linux_386.tar.gz

-O オプションは保存先に名前をつけれるオプション

そしたら解凍する.
tar -C ./ -xzf peco.tar.gz

すると,peco_linux_386というファイルができる.

その後
sudo mv peco_linux_386 /user/local/bin/
これでユーザ全員が使えるようになる.

zshのhistroyでpecoを使う

pecoをCentOS6.5にインストール
[peco]peco-select-history.zsh で表示されるコマンド履歴の重複を削除する
を参考にさせていただきました.

.zshrc
#peco
function peco-select-history() {
    local tac
    if which tac > /dev/null; then
        tac="tac"
    else
        tac="tail -r"
    fi

    BUFFER=$(\history -n 1 | \
        eval $tac | \
        awk '!a[$0]++' | \
        peco --query "$LBUFFER")
    CURSOR=$#BUFFER
    zle clear-screen
}
zle -N peco-select-history
bindkey '^r' peco-select-history

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

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

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

# 重複を記録しない
setopt hist_ignore_dups

# 開始と終了を記録
setopt EXTENDED_HISTORY

pecoの設定をするついでにzshのHistroyの設定も一緒にした.

個人的には重複を記録しないオプションは大事だと思う.

確認

適当にコマンド打つ

$ cd
$ [control]+r
QUERY>
cd

#重複がないか確認した
cd httpd/
sudo cd httpd
cd log
cd lo
cd /var
・
・
#なさそう
#historyの確認
$ less .zsh_history
#重複はなかった

最終的な.zshrc

oh-my-zshをテンプレートとして利用しているが,最終的な設定ファイルは以下のようになる.

なおmacを利用していて,rmにはaliasを設定している.

.zshrc
# Path to your oh-my-zsh installation.
export ZSH=$HOME/oh-my-zsh

# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.
ZSH_THEME="cloud"

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion. Case
# sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)

# User configuration

export PATH=$HOME/bin:/usr/local/bin:$PATH
# export MANPATH="/usr/local/man:$MANPATH"

source $ZSH/oh-my-zsh.sh

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# ssh
# export SSH_KEY_PATH="~/.ssh/dsa_id"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

## auto-jump
[[ -s $HOME/.autojump/etc/profile.d/autojump.sh ]] && source $HOME/.autojump/etc/profile.d/autojump.sh

autoload -U compinit && compinit -u

## peco
function peco-select-history() {
    local tac
    if which tac > /dev/null; then
        tac="tac"
    else
        tac="tail -r"
    fi

    BUFFER=$(\history -n 1 | \
        eval $tac | \
        awk '!a[$0]++' | \
        peco --query "$LBUFFER")
    CURSOR=$#BUFFER
    zle clear-screen
}
zle -N peco-select-history
bindkey '^r' peco-select-history

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

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

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

# 重複を記録しない
setopt hist_ignore_dups

# 開始と終了を記録
setopt EXTENDED_HISTORY

# alias
##rm設定
to_trash() {
    for file in $@
    do
        mv $file ~/.Trash
    done
}
alias rm="to_trash"

感想

それぞれまだまだ使えることが多そうだから,GitHubページをみてもっと理解を含めていろいろ使えるようになりたい

28
23
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
28
23