zsh関連の設定 (1)【自分のPCで使用しているソフトウェア,設定の紹介 #5】
今回はzsh(zshell)の起動時に読み込むファイルの紹介をしたいと思います。 全体の流れと共に,順番に説明していきます。
zshrc
以下が~/.zshrc
です。
dotfiles/zshrc at master · ssh0/dotfiles · GitHub
まず環境変数ZSH_ROOT
を設定します。(他の設定で使う)
# Set the zsh root directoy export ZSH_ROOT=$HOME/.zsh
fzf
次にfzfを有効化(もしインストールされていなければ後でインストール)
# Using fzf if [ ! -f ~/.fzf.zsh ]; then # install fzf function _install_fzf() { confirm y "fzf is not installed. Start installation ? " git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install } install_fzf=true else source ~/.fzf.zsh install_fzf=false fi export FZF_COMPLETION_TRIGGER=''
環境変数を設定
言語などの環境変数を設定していきます。
# Set some environmet variables source "$ZSH_ROOT/functions/environment.zsh"
~/.zsh/functions/environment.zsh
export ZSH_CACHE_DIR=$ZSH/cache # PATH to /usr/local/lib export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH # XDG_CONFIG_HOME export XDG_CONFIG_HOME=$HOME/.config # set language environment export LANG=en_US.UTF-8 export MANLANG=ja_JP.UTF-8 export LC_TIME=en_US.UTF-8 # python startup export PYTHONSTARTUP="$HOME/.pythonrc.py"
環境変数を利用して,デフォルトのアプリケーションを設定します。
find_alt
という関数を定義して,引数にとったコマンドをhash関数で存在を確認し,そのコマンドを設定します。
# find alternative apps if it is installed on your system find_alt() { local i for i in $@; do hash "$i" 2>/dev/null && echo "$i" && return 0 done return 1 } # set the default program # the first program in the array will be chosen as the default export OPENER=$(find_alt xdg-open exo-open gnome-open ) export BROWSER=$(find_alt firefox luakit google-chrome chromium chromium-browser $OPENER ) export BROWSERCLI=$(find_alt w3m links2 links lynx elinks $OPENER ) export EDITOR=$(find_alt nvim vim emacs nano leafpad gedit pluma $OPENER ) export FILEMANAGER=$(find_alt thunar nautilus dolphin pcmanfm tspacefm enlightenment_filemanager $OPENER ) export PAGER=$(find_alt less more most) export PERCOL=$(find_alt fzf peco percol) export PLAYER=$(find_alt mpv mplayer cvlc $OPENER ) export READER=$(find_alt mupdf zathura evince $OPENER ) export TERMCMD=$(find_alt urxvt xterm gnome-terminal) unfunction find_alt
# To stop ranger from loading both the default and my custom rc.conf export RANGER_LOAD_DEFAULT_RC=false # override the safe location for pick # export PICK_SAFE="$HOME/Dropbox/conf/pick.safe" # enhancd by "cd" export ENHANCD_COMMAND='cd' # dot export DOT_COMMAND='d' # takenote export TAKENOTE_ROOTDIR="$HOME/Workspace/blog" export TAKENOTE_FILERCMD=ranger # shtest export SHTEST_FILENAME_PRE="test_" export SHTEST_FILENAME_EXTENSION="sh" # cheat # ----- # cheat allows you to create and view interactive cheatsheets on the # command-line. It was designed to help remind *nix system administrators of # options for commands that they use frequently, but not frequently enough to # remember. # [chrisallenlane/cheat](https://github.com/chrisallenlane/cheat) # sudo pip install cheat export CHEATCOLORS=true export PC=
ls時の色を設定
ivoarch/dircolors-zenburnを使わせてもらっています。
# dircolors eval $(dircolors "$ZSH_ROOT/dircolors/dircolors-zenburn")
tmux関連の設定
# tmux plugin should be loaded as much as first of zshrc. # Only run if tmux is actually installed. hash tmux 2>/dev/null && source "$ZSH_ROOT/functions/tmux.zsh"
zshプラグインを有効化
zgenもしくはzplugを使ってzshプラグインを管理しています。
# Use zgen to load some zsh plugins source "$ZSH_ROOT/functions/zgen.zsh" # zplug #source "$ZSH_ROOT/functions/zplug.zsh"
- dotfiles/zgen.zsh at master · ssh0/dotfiles · GitHub
- dotfiles/zplug.zsh at master · ssh0/dotfiles · GitHub
- b4b4r07/enhancd zsh
- chrissicool/zsh-256color
- ssh0/dot
- ssh0/zsh-takenote
- fcambus/ansiweather
- Tarrasch/zsh-bd
- zsh-users/zsh-syntax-highlighting
補完関連の設定
# Add fpath for completions # There are completion functions named like '_function'. fpath=($ZSH_ROOT/completions $fpath) source "$ZSH_ROOT/functions/completion.zsh" autoload -U compinit compinit -D
~/.zsh/completions
以下に,_コマンド名
の形式で補完関数を置いておき,fpath
に追加しておくことで補完できるようになります。
内容
- cheat
- incfn
- jupyter
- mkdocs
- myrsync
- mytask
- pandoc
- pick
- ranger
- recordmydesktop
- s
- shtest
- streamradio
- tig
- tmuxinator
- youtube-dl
~/.zsh/functions/completion.zsh
は以下:
#=#=#= # Set completion options # # **Features** # # * show completion menu # * verbose output (with description) # * ls colors with LS_COLORS #=#= unsetopt flowcontrol setopt auto_menu # show completion menu on succesive tab press setopt interactive_comments setopt list_packed setopt complete_in_word setopt always_to_end setopt auto_param_slash export WORDCHARS='' zstyle ':completion:*' verbose yes zstyle ':completion:*' format '%B%F{magenta}%d%b%f' zstyle ':completion:*:default' menu select=2 zstyle ':completion:*:messages' format '%F{yellow}%d'"$DEFAULT" zstyle ':completion:*:warnings' format '%F{red}No matches for:''%F{yelllow} %d'"$DEFAULT" zstyle ':completion:*:options' description 'yes' zstyle ':completion:*:descriptions' format '%F{yellow}Completing %B%d%b%f'"$DEFAULT" zstyle ':completion:*' group-name '' # case sensitive (but if upper case, don't translate it to lower case.) zstyle ':completion:*' matcher-list 'm:{a-zA-Z-_}={A-Za-z_-}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" zstyle ':completion:*:*:*:*:*' menu select zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01' zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm -w -w" # disable named-directories autocompletion zstyle ':completion:*:cd:*' tag-order local-directories directory-stack path-directories # Use caching so that commands like apt and dpkg complete are useable zstyle ':completion::complete:*' use-cache true zstyle ':completion::complete:*' cache-path "$ZSH_CACHE_DIR" # Don't complete uninteresting users zstyle ':completion:*:*:*:users' ignored-patterns \ adm amanda apache at avahi avahi-autoipd beaglidx bin cacti canna \ clamav daemon dbus distcache dnsmasq dovecot fax ftp games gdm \ gkrellmd gopher hacluster haldaemon halt hsqldb ident junkbust kdm \ ldap lp mail mailman mailnull man messagebus mldonkey mysql nagios \ named netdump news nfsnobody nobody nscd ntp nut nx obsrun openvpn \ operator pcap polkitd postfix postgres privoxy pulse pvm quagga radvd \ rpc rpcuser rpm rtkit scard shutdown squid sshd statd svn sync tftp \ usbmux uucp vcsa wwwrun xfs '_*' # ... unless we really want to. zstyle '*' single-ignored show # history search autoload history-search-end zle -N history-beginning-search-backward-end history-search-end zle -N history-beginning-search-forward-end history-search-end # user defined compdef _man man2pdf
その他設定ファイル読み込み
# Load custom plugins while read myfunction; do source "$ZSH_ROOT/functions/${myfunction}.zsh" done < <(cat << EOF alias history colorize command-not-found confirm extract git less ls man ranger shtest peco-history bindkey termsupport prompt EOF ) unset -v myfunction
この部分に関しては次回に回したいと思います。
まとめ
zshの設定に関して,いくつか紹介しました。次回は,最後に紹介した部分を詳しく説明できればと思います。