2
5

More than 1 year has passed since last update.

zshに移行してみた

Posted at

経緯

1年くらいArchlinuxを使っていて,fishを普段使いしているが,最近macbookを使う機会がありzshも案外悪くないかもしれないと思い移行することにした。

zshはPOSIX準拠なので問題が発生しづらいという利点もある。

本体のインストール

yay -S zsh

これだけ

zsh

でzshに移動しておく

カスタマイズ

お好きなエディタで.zshrcを弄っていく

nvim ~/.zshrc

フレームワーク

どうやらZimというzshのフレームワークがあるらしい。

これを使うとlsやgit関連のコマンドのエイリアスなどの便利機能を追加してくれる。

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

curl -fsSL https://raw.githubusercontent.com/zimfw/install/master/install.zsh | zsh

ヒストリ関連

どうやら上キーで呼びだせるコマンドの履歴をヒストリと言うらしい。

通常ではターミナルを起動してから閉じるまでしか保存されないため,別にファイルを作りそこに保存しておく。

.zshrc
export LANG=ja_JP.UTF-8
HISTFILE=$HOME/.zsh-history
HISTSIZE=1000000
SAVEHIST=1000000
setopt share_history # 複数ターミナルでヒストリの共有

テーマ

powerlevel10kを導入する

yay -S --noconfirm zsh-theme-powerlevel10k-git

公式によるとコミュニティパッケージはよく壊れるから使ってはいけないらしい。

zshrcにpowerlevel10kを読み込ませる

.zshrc
source /usr/share/zsh-theme-powerlevel10k/powerlevel10k.zsh-theme

source .zshrcするとpowerlevel10kの設定画面が出てくるので指示された選択肢から自分の好みに合わせる。
設定をやり直したい場合はzshrcに書いている通りp10k configureを実行すればよい。
↓こんな感じになった
image.png

プラグイン

zplugにしようと思ったがRust製の高速なプラグインマネージャーのsheldonというものを使ってみることにした。

インストール

yay -S sheldon

zshにsheldonをロードさせる

.zshrc
eval "$(sheldon source)"

ここからはplugins.tomlを弄っていく

nvim ~/.config/sheldon/plugins.toml

基本的にはgithubのリポジトリを書いていくだけ

pluginsテーブル配下に書いていく

まずzsh-completions(補完強化)を導入する

.config/plugins.toml
[plugins.zsh-completions]
github = "zsh-users/zsh-completions"

こんな感じで指定した後,sheldon sourceを実行すれば適用される。

lsの強化

lsの上位互換であるlsd(ls-deluxeの略で,決して違法薬物ではない)を紹介する。

yay -S lsd

これでインストールできる。
あとは.zshrcにaliasを設定すればOK

.zshrc
alias ls='lsd'
alias l='lsd'
alias ll='lsd -l'
alias la='lsd -a'
alias lla='lsd -la'
alias lt='lsd --tree'

こんな感じになる
image.png

終わりに

割と快適なターミナル環境になったのではないだろうか。

最後に.zshrcsheldon/plugins.tomlを晒して終わりとする。

.zshrc
# Start configuration added by Zim install {{{
#
# User configuration sourced by interactive shells
#

# -----------------
# Zsh configuration
# -----------------

#
# History
#

# Remove older command from the history if a duplicate is to be added.
setopt HIST_IGNORE_ALL_DUPS

#
# Input/output
#

# Set editor default keymap to emacs (`-e`) or vi (`-v`)
bindkey -e

# 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//[\/]}

# -----------------
# Zim configuration
# -----------------

# Use degit instead of git as the default tool to install and update modules.
#zstyle ':zim:zmodule' use 'degit'

# --------------------
# Module configuration
# --------------------

#
# git
#

# Set a custom prefix for the generated aliases. The default prefix is 'G'.
#zstyle ':zim:git' aliases-prefix 'g'

#
# input
#

# Append `../` to your input for each `.` you type after an initial `..`
zstyle ':zim:input' double-dot-expand yes

#
# termtitle
#

# Set a custom terminal title format using prompt expansion escape sequences.
# See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Simple-Prompt-Escapes
# If none is provided, the default '%n@%m: %~' is used.
#zstyle ':zim:termtitle' format '%1~'

#
# zsh-autosuggestions
#

# 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

# Customize the style that the suggestions are shown with.
# See https://github.com/zsh-users/zsh-autosuggestions/blob/master/README.md#suggestion-highlight-style
#ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=242'

#
# zsh-syntax-highlighting
#

# Set what highlighters will be used.
# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)

# Customize the main highlighter styles.
# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/main.md#how-to-tweak-it
#typeset -A ZSH_HIGHLIGHT_STYLES
#ZSH_HIGHLIGHT_STYLES[comment]='fg=242'

# ------------------
# Initialize modules
# ------------------

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

# ------------------------------
# Post-init module configuration
# ------------------------------

#
# zsh-history-substring-search
#

zmodload -F zsh/terminfo +p:terminfo
# 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} history-substring-search-down
unset key
# }}} End configuration added by Zim install

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.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

# oh-my-zsh
plugins=(
  last-working-dir command-not-found
)

# ----------------old----------------------

# Created by newuser for 5.9
# autoload -Uz compinit && compinit
# setopt auto_list # 一覧表示
# setopt auto_menu # Tabで補完候補を切り替え
# zstyle ':completion:*:default' menu select=1 # 一覧表示のときにTabで選択できるように
# export LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
# zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}

# cd関連
# setopt auto_cd

# history
 export LANG=ja_JP.UTF-8
 HISTFILE=$HOME/.zsh-history
 HISTSIZE=1000000
 SAVEHIST=1000000
 setopt share_history # 複数ターミナルでヒストリの共有

# powerlevel10k
source /usr/share/zsh-theme-powerlevel10k/powerlevel10k.zsh-theme

# plugin
eval "$(sheldon source)"

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# alias
alias ls='lsd'
alias l='lsd'
alias ll='lsd -l'
alias la='lsd -a'
alias lla='lsd -la'
alias lt='lsd --tree'
.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"

[plugins]

# For example:
#
# [plugins.base16]
# github = "chriskempson/base16-shell"

# 補完
[plugins.zsh-completions]
github = "zsh-users/zsh-completions"

# syntax-highlight
[plugins.F-Sy-H]
github = "zdharma/fast-syntax-highlighting"

# suggest
[plugins.zsh-autosuggestions]
github = "zsh-users/zsh-autosuggestions"

# z
[plugins.zsh-z]
github = "agkozak/zsh-z"

# ()や""を自動補完
[plugins.autopair]
github = "hlissner/zsh-autopair"

# oh-my-zsh
[plugins.oh-my-zsh]
github = "ohmyzsh/ohmyzsh"

参考

2
5
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
2
5