3
2

【Mac / Terminal】ターミナルをイイ感じにカスタマイズ

Last updated at Posted at 2023-08-26

giphy.gif

Oh My Zshインストール

Oh My Zshとは、zsh(Macのデフォルトシェル)を良い感じにしてくれる便利ツールです。詳しくはこちらの記事がとても参考になります!

▶︎ 公式ページ

以下でインストールができます。

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Git補完

こちらのカスタマイズを適用すると、Gitコマンドやブランチ名などのサジェストをCtrl + iで表示してくれます。コマンドやブランチのスペルミスなどが減ります。とても便利です。

下記コマンドを順に実行するとインストールが完了します。

mkdir -p ~/.zsh
cd ~/.zsh
curl -o git-completion.bash https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
curl -o _git https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh

インストールしたプラグインを有効化します。~/.zshrcに以下の記述を追加してください。

zstyle ':completion:*:*:git:*' script ~/.zsh/git-completion.bash
fpath=(~/.zsh $fpath)

autoload -Uz compinit && compinit

コマンドサジェスト

このカスタマイズにより、以下のように、過去に実行したコマンドを元に薄色でサジェストを表示してくれます。

サジェストが表示されている状態で右矢印キーを押すと勝手にコマンドを入力してくれます。とっても便利です。
Screen Shot 2023-07-25 at 15.17.12.png

下記コマンドを順に実行するとインストールが完了します。

git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting

インストールしたプラグインを有効化します。~/.zshrcに以下の記載がある行があると思うので、以下のように修正してください。

~/.zshrc(修正前)
plugins=(git)

👇

~/.zshrc(修正後)
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)

設定の読み込み

以下コマンド実行、またはターミナルの再起動で、インストールしたカスタムが反映されます。

source ~/.zshrc

テーマ変更

自分の好みに合わせてターミナルのテーマを変更してみるのも楽しいです。

~/.zshrcに、以下のような記述があると思います。これが適用されるテーマの名前になります。

ZSH_THEME="dallas"

ここが参照するのは、Oh My Zshインストール時に作成される~/.oh-my-zshディレクトリ配下のthemesディレクトリ配下になります。

.zsh-themeという拡張子のファイルがたくさんあると思いますが、これらがそれぞれのテーマを定義するファイルになります。

僕の場合は、dallasというテーマを設定しているので、dallas.zsh-themeが適用されているデザインファイルになります。

Oh-My-Zshの公式Wikiにて、テーマ一覧が掲載されています。お好みに合わせてターミナルをカスタマイズしてみましょう!

▶︎ https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

また、これらは自分なりにカスタマイズすることもできます。僕の場合、dallasに少し絵文字などを追加しています✨
Screen Shot 2023-08-26 at 20.15.33.png
以下がテーマファイルになりますので、よかったら参考にしてみてください😀

~/.oh-my-zsh/themes/dallas.zsh-theme
# Personalized!

# Grab the current date (%D) and time (%T) wrapped in {}: {%D %T}
DALLAS_CURRENT_TIME_="%{$fg[white]%}🍔{%{$fg[yellow]%}%D %T%{$fg[white]%}}%{$reset_color%}"
# Grab the current machine name: muscato
DALLAS_CURRENT_MACH_="%{$fg[green]%}WHOPPER%{$fg[white]%}:%{$reset_color%}"
# Grab the current filepath, use shortcuts: ~/Desktop
# Append the current git branch, if in a git repository: ~aw@master
DALLAS_CURRENT_LOCA_="%{$fg[cyan]%}%~\$(git_prompt_info)%{$reset_color%}\$(parse_git_dirty)"
# Grab the current username: dallas
DALLAS_CURRENT_USER_="%{$fg[blue]%}%%{$reset_color%}"
# Use a % for normal users and a # for privileged (root) users.
DALLAS_PROMPT_CHAR_="%{$fg[blue]%}==👉%{$reset_color%}"
# For the git prompt, use a white @ and blue text for the branch name
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[green]%}@(%{$fg[red]%}"
# Close it all off by resetting the color and styles.
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}%{$fg[green]%})"
# Do nothing if the branch is clean (no changes).
ZSH_THEME_GIT_PROMPT_CLEAN=""
# Add 3 cyan ✗s if this branch is diiirrrty! Dirty branch!
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[yellow]%}✗✗✗"

ZSH_THEME_RUBY_PROMPT_PREFIX="%{$fg[white]%}[%{$fg[magenta]%}"
ZSH_THEME_RUBY_PROMPT_SUFFIX="%{$fg[white]%}]%{$reset_color%}"

# Put it all together!
PROMPT="$DALLAS_CURRENT_TIME_\$(ruby_prompt_info)$DALLAS_CURRENT_MACH_$DALLAS_CURRENT_LOCA_$DALLAS_PROMPT_CHAR_ "

快適なターミナルライフを🤞✨

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