Open5

zsh設定記

bontybonty

.zshenv はこのリンクを参考に設定
https://github.com/NagayamaRyoga/dotfiles/blob/0e9e326d58d8a66043cc2bb6a6f3b62d0ac5e1e0/config/zsh/.zshenv

.zshenv
### locale
export LANG="ja_JP.UTF-8"

# ignore /etc/zprofile, /etc/zshrc, /etc/zlogin, and /etc/zlogout
setopt no_global_rcs

### XDG
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_DATA_HOME="$HOME/.local/share"
export XDG_STATE_HOME="$HOME/.local/state"
export XDG_CACHE_HOME="$HOME/.cache"

### zsh (same config is written in /etc/zshenv for macOS)
export ZDOTDIR="$XDG_CONFIG_HOME/zsh"

### rust
export RUST_BACKTRACE=1
export RUSTUP_HOME="$XDG_DATA_HOME/rustup"
export CARGO_HOME="$XDG_DATA_HOME/cargo"

### golang
export GOPATH="$XDG_DATA_HOME/go"

### deno
export DENO_INSTALL="$XDG_DATA_HOME/deno"
export DENO_INSTALL_ROOT="$DENO_INSTALL"

### ruby
# export GEM_HOME="$XDG_DATA_HOME/gem"
# export GEM_SPEC_CACHE="$XDG_CACHE_HOME/gem"
#
# export BUNDLE_USER_HOME="$XDG_CONFIG_HOME/bundle"
# export BUNDLE_USER_CACHE="$XDG_CACHE_HOME/bundle"
# export BUNDLE_USER_PLUGIN="$XDG_DATA_HOME/bundle/plugin"

### sheldon
export SHELDON_CONFIG_DIR="$ZDOTDIR"
bontybonty

以前使ってたzsh4humansのプラグインを参考にsheldonの設定ファイルを追加

plugins.toml
# `sheldon` configuration file
# ----------------------------
#
# You can modify this file directly or you can use one of the following
# `sheldon` commands which are provided to assist in editing the config file:
#
# - `sheldon add` to add a new plugin to the config file
# - `sheldon edit` to open up the config file in the default editor
# - `sheldon remove` to remove a plugin from the config file
#
# See the documentation for more https://github.com/rossmacarthur/sheldon#readme

shell = "zsh"

[templates]
defer = "{{ hooks | get: \"pre\" | nl }}{% for file in files %}zsh-defer source \"{{ file }}\"\n{% endfor %}{{ hooks | get: \"post\" | nl }}"

[plugins]

[plugins.zsh-defer]
github = "romkatv/zsh-defer"

[plugins.powerlevel10k]
github = "romkatv/powerlevel10k"
use = ["{{ name }}.zsh-theme"]
hooks.post = """
source $ZDOTDIR/.p10k.zsh
"""

### lazy

[plugins.zeno]
github = "yuki-yano/zeno.zsh"
hooks.pre = """
export ZENO_HOME="$XDG_CONFIG_HOME/zsh/plugins/zeno"
export ZENO_GIT_CAT="bat --color=always"
export ZENO_GIT_TREE="eza --tree"
"""
hooks.post = """
__zeno_bindkey() {
  bindkey ' ' zeno-auto-snippet
  bindkey '^m' zeno-auto-snippet-and-accept-line
  bindkey '^i' zeno-completion
  bindkey '^r' zeno-history-selection
  bindkey '^s' zeno-insert-snippet
  bindkey '^x^g' zeno-ghq-cd
}
zsh-defer -a -t0.01 __zeno_bindkey
"""
apply = ["defer"]

[plugins.compinit]
inline = """
autoload -Uz compinit
zsh-defer -a -t0.01 compinit -d "$XDG_STATE_HOME/zsh/zcompdump"
"""

# load before fast-syntax-highlighting and zsh-autosuggestions
# load after compinit
[plugins.fzf-tab]
github = "Aloxaf/fzf-tab"
apply = ["defer"]

[plugins.fast-syntax-highlighting]
github = "zdharma-continuum/fast-syntax-highlighting"
apply = ["defer"]

# [plugins.zsh-syntax-highlighting]
# github = "zsh-users/zsh-syntax-highlighting"
# apply = ["defer"]

[plugins.zsh-autosuggestions]
github = "zsh-users/zsh-autosuggestions"
apply = ["defer"]

[plugins.zsh-history-substring-search]
github = "zsh-users/zsh-history-substring-search"
hooks.post = """
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
bindkey "$terminfo[kcuu1]" history-substring-search-up
bindkey "$terminfo[kcud1]" history-substring-search-down
bindkey -M emacs '^P' history-substring-search-up
bindkey -M emacs '^N' history-substring-search-down
"""
apply = ["defer"]

[plugins.zsh-completions]
github = "zsh-users/zsh-completions"
dir = "src"
apply = ["defer", "fpath"]

# load after zeno
[plugins.zsh-autopair]
github = "hlissner/zsh-autopair"
apply = ["defer"]
bontybonty

https://zenn.dev/fuzmare/articles/zsh-plugin-manager-cache

これを参考に各種設定ファイルをコンパイルして同期読み込み、非同期読み込みの設定ファイルを追加

.zshrc
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.config/zsh/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

### sheldon

# source command override technique
function source {
  ensure_zcompiled $1
  builtin source $@
}
function ensure_zcompiled {
  local compiled="$1.zwc"
  if [[ ! -r "$compiled" || "$1" -nt "$compiled" ]]; then
    echo "\033[1;36mCompiling\033[m $1"
    zcompile $1
  fi
}
ensure_zcompiled $ZDOTDIR/.zshrc

source $ZDOTDIR/sync.zsh

# sheldon cache technique
sheldon_cache="$SHELDON_CONFIG_DIR/sheldon.zsh"
sheldon_toml="$SHELDON_CONFIG_DIR/plugins.toml"
if [[ ! -r "$sheldon_cache" || "$sheldon_toml" -nt "$sheldon_cache" ]]; then
  sheldon source > $sheldon_cache
fi
source "$sheldon_cache"
unset sheldon_cache sheldon_toml

zsh-defer source $ZDOTDIR/lazy.zsh
zsh-defer unfunction source
sync.zsh
### path

# unique path
typeset -gU PATH path

path=(
  /usr/local/bin(N-/)
  /usr/bin
  /bin
  /usr/local/sbin(N-/)
  /usr/sbin
  /sbin
  /Library/Apple/usr/bin
)

path=(
  "$HOME/.local/bin"(N-/)
  "$GOPATH/bin"(N-/)
  "$DENO_INSTALL/bin"(N-/)
  "$path[@]"
)

# export homebrew env
eval "$(/opt/homebrew/bin/brew shellenv zsh)"

### history
export HISTFILE="$XDG_STATE_HOME/zsh_history"
export HISTSIZE=10000
export SAVEHIST=100000

setopt extended_history
setopt hist_find_no_dups
setopt hist_ignore_all_dups
setopt hist_ignore_dups
setopt hist_ignore_space
setopt hist_reduce_blanks
setopt hist_save_no_dups
setopt hist_verify
setopt inc_append_history_time

### options

# cd options
setopt auto_cd
setopt auto_pushd
setopt pushd_ignore_dups
setopt pushd_to_home

# no beep
setopt no_beep
setopt no_list_beep
setopt no_hist_beep

# misc
setopt rm_star_wait      # confirm rm star
setopt ignore_eof        # disable ^d (use 'exit', 'logout')
setopt magic_equal_subst # enable to expand ~ after = e.g.) --prefix=~/local
setopt no_case_glob      # ignore case when glob
setopt extended_glob     # use '*, ~, ^' as regular expression
setopt auto_remove_slash # automatically delete slash complemented by supplemented by inserting a space
setopt mark_dirs         # if the path is directory, add '/' to path tail when generating path by glob
setopt print_eight_bit   # support japanese file name
setopt no_flow_control   # disable ^s

### hooks

# do not add common commands to history
zshaddhistory() {
  local line="${1%%$'\n'}"
  [[ ! "$line" =~ "^(cd|history|ls|ll|la)($| )" ]]
}

# ls when cd
chpwd() {
  if (( ${+commands[eza]} )); then
    eza --icons --git -a --group-directories-first
  else
    ls -a
  fi
}

### keybindings
bindkey -e
lazy.zsh
### ls-colors
export LS_COLORS="di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32"

### aliases

# confirm commands
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# emacs
alias e='emacs -nw'

# vi
alias vi='nvim'
alias vim='nvim'

# eza
alias ls='eza --group-directories-first --git --icons'
alias l='eza --group-directories-first --git --icons'
alias la='eza --group-directories-first --git --icons -a'
alias ll='eza --group-directories-first --git --icons -l --header --color-scale --time-style=long-iso'
alias lla='eza --group-directories-first --git --icons -la --header --color-scale --time-style=long-iso'
alias tree='eza --group-directories-first --git --icons --tree'

# bat
export MANPAGER="sh -c 'col -bx | bat --color=always --language=man --plain'"
export BAT_THEME="Dracula"
export BAT_STYLE="numbers,changes,header"
alias cat='bat'

# git
alias ggpull='git pull origin $(git symbolic-ref --quiet --short HEAD 2> /dev/null)'
alias ggpush='git push origin $(git symbolic-ref --quiet --short HEAD 2> /dev/null)'

# lazygit
alias lzg='lazygit'

# httpie
alias https='http --default-scheme=https'

# ranger
alias rr='ranger'

### completion

# from fzf-tab
# disable sort when completing `git checkout`
zstyle ':completion:*:git-checkout:*' sort false
# set descriptions format to enable group support
# NOTE: don't use escape sequences (like '%F{red}%d%f') here, fzf-tab will ignore them
zstyle ':completion:*:descriptions' format '[%d]'
# set list-colors to enable filename colorizing
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# force zsh not to show completion menu, which allows fzf-tab to capture the unambiguous prefix
zstyle ':completion:*' menu no
# preview directory's content with eza when completing cd
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'eza -1 --color=always $realpath'
# custom fzf flags
# NOTE: fzf-tab does not follow FZF_DEFAULT_OPTS by default
zstyle ':fzf-tab:*' fzf-flags --color=fg:1,fg+:2 --bind=tab:accept
# To make fzf-tab follow FZF_DEFAULT_OPTS.
# NOTE: This may lead to unexpected behavior since some flags break this plugin. See Aloxaf/fzf-tab#455.
zstyle ':fzf-tab:*' use-fzf-default-opts yes
# switch group using `<` and `>`
zstyle ':fzf-tab:*' switch-group '<' '>'

# ignore case
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'

# rehash immediately when install commands
zstyle ":completion:*:commands" rehash 1

### GPG
export GPG_TTY="$TTY"

### less
export LESSHISTFILE='-'

### mise
eval "$($(brew --prefix)/bin/mise activate zsh)"

### zoxide
eval "$(zoxide init --cmd cd zsh)"