LoginSignup
27
6

More than 1 year has passed since last update.

zshrcのカスタマイズ

Last updated at Posted at 2022-12-19

この記事はHRBrain Advent Calendar 2022 カレンダー3の20日目の記事です。

はじめに

株式会社HRBrainでエンジニアをやっている深谷です。
この記事ではおすすめのzchrcのカスタマイズについて書こうと思います。

そもそもzshrcとは

zshrcの説明をする前にzshとはなにかの説明を軽くしておくと

Z shellは、Unixのコマンドシェルの1つである。対話的なログインコマンドシェルとしても、強力なシェルスクリプトコマンドのインタープリターとしても使うことができる。

bashやkshなどと同じコマンドシェルのうちの1つです。
macOSではmacOS 10.15 Catalinaからデフォルトのシェルとして使用されています。

それではzshrcとはなにか

zshをログインシェル、インタラクティブシェルとして使用する場合に読み込まれる設定ファイルのことを指します。
zshrcをカスタマイズすることで今までより便利にzshを使うことが可能となります。
ちなみにrcはrun commandの略です。

zshの導入

Homebrewを使ってzshをインストールします。
Homebrewがない方はこちらからinstallしてください。

brew install zsh

ログインシェルを変更します。

chsh -s /usr/local/bin/zsh

これでzshが使えるようになったはずです。

~/.zshrc

がない場合は作成してください。

カスタマイズ

zsh-completions コマンド補完機能の設定。

ない方は下記のコマンドでinstallしてください。

brew install zsh-completions
autoload -U compinit && compinit -u

日本語ファイル名を表示可能にする

setopt print_eight_bit

ビープ音の停止

setopt no_beep

historyの保存数を増やす

HISTFILE=~/.zsh_history
HISTSIZE=100000
SAVEHIST=100000

git系

  • gitのコマンドをタブキーで補完してくれるgit-completion
  • gitのリポジトリのステータスをプロンプトに表してくれるgit-prompt

ない方は下記のコマンドでinstallしてください。

curl -o .git-completion.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
curl -o .git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
source ~/.zsh/git-prompt.sh
fpath=(~/.zsh $fpath)
zstyle ':completion:*:*:git:*' script ~/.zsh/git-completion.bash
autoload -Uz compinit && compinit
autoload -Uz vcs_info
setopt prompt_subst
zstyle ':vcs_info:git:*' check-for-changes true
zstyle ':vcs_info:git:*' stagedstr "%F{magenta}!"
zstyle ':vcs_info:git:*' unstagedstr "%F{yellow}+"
zstyle ':vcs_info:*' formats "%F{cyan}%c%u[%b]%f"
zstyle ':vcs_info:*' actionformats '[%b|%a]'
precmd () { vcs_info }
PROMPT='
[%B%F{red}%n@%m%f%b:%F{green}%~%f]%F{cyan}$vcs_info_msg_0_%f
%F{yellow}$%f '

下記にgitのaliasをまとめました。

alias g='git'
alias gs='git status'
alias gb='git branch'
alias gc='git checkout'
alias gct='git commit'
alias gg='git grep'
alias ga='git add'
alias gd='git diff'
alias gl='git log'
alias gfo='git fetch origin'
alias gcm='git commit -m'
alias gpo='git push origin'
alias gpom='git push origin master'
alias gst='git stash'
alias gsl='git stash list'
alias gsp='git stash pop'

docker系

何かと長いdockerのコマンドもaliasにしておくと時短になって便利です。

alias d='docker'
alias dc='docker-compose'
alias dcnt='docker container'
alias dimg='docker image'
alias drun='docker container run —rm -d'
alias drunit='docker container run —rm -it'

つらつらと書きましたがちょっと見づらいと思うのでGitHub

  • .zshrc
  • install.sh

を置いておくので好きなように使ってください。

まとめ

色々なカスタマイズを紹介しましたが、他にも色々な設定があるのでどんどん自分の使いやすいzshにしていきましょう。
aliasを登録するだけで時短になりますし、普段自分がよく使うコマンドなども登録していくと業務効率が上がると思います。

ということで、そんな弊社に興味を持った方がいればぜひ下記ページからご応募ください!

27
6
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
27
6