zshを導入してみる。
zshがいいと聞いたのでfishと迷ったがzshを導入する。
環境はwslのubuntu。みんなmac使ってるので記事がmacばかり。初心者すぎて違いもわからないけど()
何気に初投稿。ほんとは別で初投稿したかった。まぁ、スクラップなので良し。普通に自己紹介的な感じの記事を別で書くつもり。
参考
便利そうなのでHomebrewを入れる。
いったんやっとく。
'sudo apt update && sudo apt upgrade'
Homebrewのインストール
公式ホームページのインストールコマンド
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
公式にも書いてある。
PATHを通す。test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)"
test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
test -r ~/.bash_profile && echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.bash_profile
echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.profile
ちゃんとインストールできてるか確かめる
$ brew install sl
$ sl
ちゃんと動けば大丈夫。
zshをインストール
brewでzshを入れる。
$ brew install zsh
デフォルトシェルの変更
/etc/shellsに追記しないとbrewで入れたzshはchshで許可されない
$ command -v zsh | sudo tee -a /etc/shells
$ sudo chsh -s "$(command -v zsh)" "${USER}"
.zshrcにbrewの設定を記述する
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
zimをインストール
公式にあるコマンド。
$ curl -fsSL https://raw.githubusercontent.com/zimfw/install/master/install.zsh | zsh
.zshrcファイルの編集
Zim フレームワークで Zsh 環境を構築する
めっちゃ丁寧に解説してくれている。
- 私はstarshipとssh-agentを使っているのでその設定を追記している。
とりあえずの .zshrc ファイル
# Remove older command from the history if a duplicate is to be added.
setopt HIST_IGNORE_ALL_DUPS
# Set editor default keymap to emacs (`-e`) or vi (`-v`)
bindkey -v
# Prompt for spelling correction of commands.
setopt CORRECT
# Customize spelling correction prompt.
SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? '
# Remove path separator from WORDCHARS.
WORDCHARS=${WORDCHARS//[\/]}
# Append `../` to your input for each `.` you type after an initial `..`
zstyle ':zim:input' double-dot-expand yes
# Set a custom terminal title format using prompt expansion escape sequences.
# See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Simple-Prompt-Escapes
zstyle ':zim:termitle' hooks 'preexex' 'precmd'
zstyle ':zim:termitle' format '{${$(A)=1}[1]}'
zstyle ':zim:termtitle' format '%1~'
# Disable automatic widget re-binding on each precmd. This can be set when
# zsh-users/zsh-autosuggestions is the last module in your ~/.zimrc.
ZSH_AUTOSUGGEST_MANUAL_REBIND=1
# highlighted by zsh-highlight
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)
ZIM_HOME=${ZDOTDIR:-${HOME}}/.zim
# Download zimfw plugin manager if missing.
if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
if (( ${+commands[curl]} )); then
curl -fsSL --create-dirs -o ${ZIM_HOME}/zimfw.zsh \
https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
else
mkdir -p ${ZIM_HOME} && wget -nv -O ${ZIM_HOME}/zimfw.zsh \
https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
fi
fi
# Install missing modules, and update ${ZIM_HOME}/init.zsh if missing or outdated.
if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
source ${ZIM_HOME}/zimfw.zsh init -q
fi
# Initialize modules.
source ${ZIM_HOME}/init.zsh
# If the above files have not been replaced, the following can be used
#[[ ~/.zimrc -nt ~/.zim/init.zsh ]] && source ~/.zim/zimfw.zsh init -q
# Bind ^[[A/^[[B manually so up/down works both before and after zle-line-init
for key ('^[[A' '^P' ${terminfo[kcuu1]}) bindkey ${key} history-substring-search-up
for key ('^[[B' '^N' ${terminfo[kcud1]}) bindkey ${key} history-substring-search-down
for key ('k') bindkey -M vicmd ${key} history-substring-search-up
for key ('j') bindkey -M vicmd ${key} histor
# starship
eval "$(starship init zsh)"
# ssh-agent
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2=agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
agent_start
ssh-add ~/.ssh/id_ed25519
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add ~/.ssh/id_ed25519
fi
.zimrc ファイルを設定する
めっちゃ助かった。
fzf, bat, exa, safe-rm を使うためインストールする。
$ brew install fzf fd bat exa safe-rm
$ $(brew --prefix)/opt/fzf/install --key-bindings --completion --update-rc --no-fish
私の .zimrc ファイル
zmodule archive
zmodule fzf
zmodule environment
zmodule git
zmodule input
zmodule termtitle
zmodule utility
zmodule exa
zmodule homebrew
zmodule zsh-users/zsh-completions --fpath src
zmodule completion
zmodule zsh-users/zsh-syntax-highlighting
zmodule zsh-users/zsh-history-substring-search
zmodule zsh-users/zsh-autosuggestions
私の .zshrc ファイル
fpath+=$(brew --prefix)/share/zsh/site-functions
setopt HIST_IGNORE_ALL_DUPS
bindkey -v
setopt CORRECT
# Customize spelling correction prompt.
SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? '
# Remove path separator from WORDCHARS.
WORDCHARS=${WORDCHARS//[\/]}
zstyle ':zim:input' double-dot-expand yes
zstyle ':zim:termitle' hooks 'preexex' 'precmd'
zstyle ':zim:termitle' format '{${$(A)=1}[1]}'
zstyle ':zim:termtitle' format '%1~'
# Disable automatic widget re-binding on each precmd. This can be set when
# zsh-users/zsh-autosuggestions is the last module in your ~/.zimrc.
ZSH_AUTOSUGGEST_MANUAL_REBIND=1
# highlighted by zsh-highlight
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)
ZIM_HOME=${ZDOTDIR:-${HOME}}/.zim
[[ ~/.zimrc -nt ~/.zim/init.zsh ]] && source ~/.zim/zimfw.zsh init -q
# reset modules
source ~/.fzf.zsh
source ~/.zim/init.zsh
# Bind ^[[A/^[[B manually so up/down works both before and after zle-line-init
for key ('^[[A' ${terminfo[kcuu1]}) bindkey ${key} history-substring-search-up
for key ('^[[B' ${terminfo[kcud1]}) bindkey ${key} history-substring-search-down
for key ('k') bindkey -M vicmd ${key} history-substring-search-up
for key ('j') bindkey -M vicmd ${key} history-substring-search-down
# starship
eval "$(starship init zsh)"
# homebrew
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
# ssh-agent
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2=agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
agent_start
ssh-add ~/.ssh/id_ed25519
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add ~/.ssh/id_ed25519
fi