fishをインストールする

Posted by Tatsuyano on Sun, Sep 17, 2017
In
Tags fish, shell

macを新調したので、shellもzshからfishに乗り換えてみた。

Install fish

$ brew install fish

$ fish -v
fish, version 2.6.0

fishをログインシェルにせず、bashから起動する

ログインシェルをfishにするとfishに問題があったときにterminalが動かなくなってしまうので、bash_profileからexec fishで起動する。 またanyenvがfishに完全に対応していないみたいなので、bashでanyenvを動かしてからfishを動かす。

## HOME/.bash_profile
if [ -d $HOME/.anyenv ]; then
   export PATH=$HOME/.anyenv/bin:$PATH
   eval "$(anyenv init -)"
fi
exec fish

Install fisherman

はじめはOhMyFishをインストールしたが、調べた結果fishermanのほうがよさそうだったのでプラグインマネージャーはfishermanに。コマンドはfisher

$ curl -Lo ~/.config/fish/functions/fisher.fish --create-dirs https://git.io/fisher

$ fisher -v
fisherman version 2.13.1 ~/.config/fish/functions/fisher.fish

テーマはOhMyFishのagnosterを選択。別のテーマに変更する場合は、再度installすると変更される。

$ fisher install omf/theme-agnoster

pluginのインストール

$ fisher install z 0rax/fish-bd edc/bass

$ fisher ls // themeの前には*が付く
* agnoster   await  bd  get   getopts   last_job_id   z   bass

alliasとfunctionを設定する

自分用のaliasとfunctionをHOME/.config/fish/config.fishに設定。

## HOME/.config/fish/config.fish

# git alias
alias ga  "git add"
alias gd  "git diff"
alias gb  "git branch"
alias gco "git checkout"
alias gst "git status"

# colordiff or diff
set tmp (which colordiff) #実行結果を変数にsetすることで、標準出力に出力させないようにする
if test $status -eq 0
    alias diff "colordiff -u"
else
    alias diff "diff -u"
end

# tmux
function tmr
    tmux new -s $argv[1]; or tmux attach -d -t $argv[1]
end

Ctrl+F のキーバインドを変える

Ctrl+Fでカーソルが動かないと思ったら、デフォルトだとCtrl+Fはautosuggestionがbindされている。

bind \cf 'forward-word'
## Autosuggestionsより抜粋
To accept the autosuggestion (replacing the command line contents), press right arrow or Control+F.
To accept the first suggested word, press Alt+→ or Alt+F. 
If the autosuggestion is not what you want, just ignore it: it won't execute unless you accept it.

peco & ghqを入れる

$ brew install peco ghq
$ fisher install z decors/fish-ghq peco  yoshiori/fish-peco_select_ghq_repository

fishでhistory見るにはfish | history | pecoみたいなコマンドで実行する。下記のサイトを参考にfunctionにした。

function peco_select_history
    if test (count $argv) = 0
        set peco_flags
    else
        set peco_flags --query "$argv"
    end
    history|peco $peco_flags|read foo
    if [ $foo ]
        commandline $foo
    else
        commandline ''
    end
end

# keybinding for peco
function fish_user_key_bindings
    bind \cs peco_select_ghq_repository
    bind \cr peco_select_history # Bind for peco select history to Ctrl+R
end

その他参考サイト