4
10

More than 3 years have passed since last update.

個人的な M1 Mac のCUI環境セットアップメモ

Last updated at Posted at 2021-05-23

自分用の細かいセットアップの手順書です

PATH の YOU は適宜自分のホームディレクトリ名に置き換えてください

環境

  • macOS Big Sur 11.3.1
  • Mac mini (M1, 2020)

iterm

概要

iterm を使うとどのスクリーンにいても透過したターミナルをホットキーで全画面表示することができる.便利

ここから iterm をダウンロード.
/Applications におく

iterm の設定

  • General
    • Closing
      • confirm "Quit iterm2" をオフ
    • Selection
      • Applications in terminal may access clipbord をオン
    • Window
      • Native full screen windows をオフ
  • Appearance
    • General
      • Auto-hide menu bar in non-native fullscreen をオン
      • Exclude from Dock and ⌘-tab applications switcher をオン
  • Profiles
    • General
      • working directory で terminal 起動時のディレクトリを変更できる.
    • Colors
      • lucario をダウンロードしてカラースキームに設定
    • Window
      • Blur をオフ
      • Transparency をいい感じに
      • Keep background colors opaque をオン
      • Settings for new windows を以下のように設定
        • Style: Full Screen
        • Screen: Screen with Cursor
        • Space: AppSpaces
  • Keys
    • Hotkey
      • コントロール + スペースをホットキーに設定

Command Line Tool for Xcode

brew のために Xcode 入れるのは… だから command line tools のみをインストール

$ xcode-select --install

brew

brew

Homebrew の公式サイトを参考に下のコマンドを実行して brew をインストール

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

おそらくこのように設定を ~/.zprofile に書き込みなさいと言われるので,素直に実行

$ echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/YOU/.zprofile

インストールが正常に行われたかは以下のコマンドでチェックできる

$ brew doctor

brew bundle

Homebrew bundle という機能を使うと, brew でのインストールがファイル形式で管理できるので便利.

例えばこの記事のでインストールしているものは Brewfile という名前のファイルにこのように記述して管理できる.

Brewfile
brew 'neovim', args: ['HEAD']
brew 'tmux'
brew 'anyenv'
brew 'git'
brew 'gh'
brew 'tree'
brew 'trash'

以下のようなコマンドで管理できる

# Brewfile に書いてあるパッケージをインストール
$ brew bundle
# Brewfile に書いていない, 依存していないパッケージを表示
$ brew bundle cleanup

zprezto

概要

最近の Mac はデフォルトで zsh なのでそのまま使って大丈夫そう
よく知らないが,軽量 oh-my-zsh みたいなものらしい
これをインストールするときに .zshrc, .zprofile などが上書きされるらしいので既存の .zshrc, .zprofile がある人は退避させておくべき

インストール

この README に従えば問題なくインストールできると思う
投稿時には以下の手順でインストールできた.

ダウンロード

コマンドラインで実行
git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"

インストール

コマンドラインで実行
setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
  ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done

~/.zprofile も書き換えられるので再び homebrew の設定を ~/.zprofile に書き込む

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/YOU/.zprofile

設定

インストールが完了後,ホームディレクトリに .zpreztorc ができるのでここの設定をいじってお好みの設定にする.

個人的に好んでいる設定を晒しておく

~/.zpreztorc
- zstyle ':prezto:module:editor' key-bindings 'emacs'
+ zstyle ':prezto:module:editor' key-bindings 'vi'
- zstyle ':prezto:module:prompt' theme 'sorin'
+ export TERM="xterm-256color"
+ zstyle ':prezto:module:prompt' theme 'powerlevel10k'

Shell を再起動などすると powerlevel10k の初期設定が起動する.

poewrlevel10k

powerlevel10k は zsh のテーマの一つでインタラクティブに設定を行えるリッチなプロンプト.

上記の設定 zstyle ':prezto:module:prompt' theme 'powerlevel10k' を行い,
再起動すると設定画面がコンソール上で開く.
この設定で Nerd-font の MesloLGF NS をインストールすることができるので, 
Nerd-font を設定してないのであればここでお願いする.

powerlevel10k のtransient-prompt 機能は過去のコマンドのプロンプトがシンプルになり見やすいのでとってもおすすめ.

zshrc

お気に入りの設定設定ファイル

.zshrc
# 初回シェル時のみ tmux 実行
# if [ $SHLVL = 1 ]; then
#   tmux attach || tmux new-session
# fi

# プロンプトを画面の下に固定する
tput cup $LINES

# 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

# Source Prezto.
if [[ -s "${ZDOTDIR:-$HOME}/.zprezto/init.zsh" ]]; then
  source "${ZDOTDIR:-$HOME}/.zprezto/init.zsh"
fi

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

# ctrl-l で画面を再描画した時の設定
function myclear() {
    clear
    tput cup $LINES
    zle reset-prompt
}
zle -N myclear
bindkey '^L' myclear

# clear で画面を再描画した時の設定
alias clear="clear;tput cup $LINES"

# vimでescが押された後のタイムラグを防ぐ
KEYTIMEOUT=1

# cursorの形状をmodeによって変える
zle-keymap-select () {
if [ $KEYMAP = vicmd ]; then
    printf "\033[2 q"
else
    printf "\033[6 q"
fi
}
zle -N zle-keymap-select
zle-line-init () {
zle -K viins
printf "\033[6 q"
}
zle -N zle-line-init
bindkey -v

.p10k.zhstypeset -g POWERLEVEL9K_INSTANT_PROMPT_COMMAND_LINES=0 を追加

powerlevel10k の設定で transient-prompt をオンにして,このように設定することで, このgif のように画面下にプロンプトを固定することができる.

anyenv

nodenv とか pyenv とかをまとめて管理できる便利なもの

インストール

リポジトリを参照

$ brew install anyenv
# anyenv init を実行すると eval "$(anyenv init -)" を ~/.zshrc に追加するように指示される
$ echo 'eval "$(anyenv init -)"' >> ~/.zshrc
# シェルを再起動
$ exec $SHELL -l
$ anyenv install --init

node と python をインストール

$ anyenv install nodenv
$ anyenv install pyenv

投稿時では pyenv の PATH の設定をちゃんとしないと警告が出る.
以下の項目を ~/.zprofile, ~/.zshrc に追記

.zprofile
export PYENV_ROOT="$HOME/.anyenv/envs/pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
.zshrc
eval "$(pyenv init -)"
$ nodenv install 15.14.0
$ nodenv global 15.14.0
$ pyenv install 3.9.5
$ pyenv global 3.9.5

Homebrew が参照するPATHの中に anyenv のパスが含まれていると brew doctor が警告をはくので alias で brew の参照する PATH を設定

.zshrc
alias brew="PATH=/opt/homebrew/bin brew"

tmux

おなじみ端末多重接続ソフトウェア

インストール

brew でインストール

$ brew install tmux

.tmux.conf

お気に入りの設定

~/.tmux.conf
# .tmux.conf の変更を適用する
bind C-r source-file "${HOME}/.tmux.conf"

# TrueColr
set -g default-terminal "xterm-256color"
set-option -ga terminal-overrides ",xterm-256color*:Tc:smso=[3m"

# ステータスバの上に線をひく
setw -g pane-border-status bottom
setw -g pane-border-format '─'

# ペイン分割の線の色
set -g pane-active-border-style bg=default,fg=colour244
set -g pane-border-style fg=colour239

# prefix キーを C-q に変更する
set -g prefix C-q

# C-b のキーバインドを解除する
unbind C-b

# キーストロークのディレイを減らす
set -sg escape-time 1

# ステータスバーを1秒毎に描画し直す
set-option -g status-interval 1

# vim のキーバインドでペインを移動する
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind -r C-k select-window -t :-
bind -r C-j select-window -t :+

# vim のキーバインドでペインをリサイズする
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# c で新しいウィンドウを作成する
bind c new-window -c "#{pane_current_path}"

# | でペインを縦分割する
bind | split-window -hc "#{pane_current_path}"

# - でペインを縦分割する
bind - split-window -vc "#{pane_current_path}"

# 番号基準値を変更
set-option -g base-index 1

# コピーモードを設定する
# コピーモードで vim キーバインドを使う
setw -g mode-keys vi

# 'v' で選択を始める
bind -T copy-mode-vi v send -X begin-selection

# 'V' で行選択
bind -T copy-mode-vi V send -X select-line

# 'C-v' で矩形選択
bind -T copy-mode-vi C-v send -X rectangle-toggle

# 'y' でヤンク
bind -T copy-mode-vi y send -X copy-selection

# 'Y' で行ヤンク
bind -T copy-mode-vi Y send -X copy-line

# 'C-p' でペースト
bind-key C-p paste-buffer

# マウスに関する設定
set-option -g mouse on
bind-key -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind-key -n WheelDownPane select-pane -t= \; send-keys -M

setw -g mode-keys vi
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"
unbind -T copy-mode-vi MouseDragEnd1Pane

powerline

pip を用いた powerline インストール方法を参考に以下の三つのライブラリを pip でインストールする

# powerline 本体
$ pip install powerline-status

# powerline の segment で用いているライブラリ
# psutil python package. Required for some segments like cpu_percent. Some segments have linux-only fallbacks for psutil functionality.
$ pip install psutil

# powerline の segment で用いているライブラリ
# pyuv python package. Required for libuv-based watcher to work.
$ pip install pyuv

tmux の powerline の設定を参考に.tmux.confに以下の行を追記する.

~/.tmux.conf
run-shell "powerline-daemon -q"
source "{repository_root}/powerline/bindings/tmux/powerline.conf"

{repository_root}ここに書いてある通り pip show powerline-status を実行したときに出力される Location: {path}{path} のこと.

~/.tmux.confの具体例
source "/Users/You/.anyenv/envs/pyenv/versions/3.9.5/lib/python3.9/site-packages/powerline/bindings/tmux/powerline.conf"

powerlineはこのように~/.config/powerlineディレクトリに設定を書くことでカスタマイズが可能.

ちなみに私はこのように設定している

nvim

vim について詳しく書くと大変なことになるのでセットアップ方法のみ書く

インストール

neovim 本体をインストールするのですが vim のプラグインで以下のものを使うので, 以下のコマンドで依存する言語拡張をインストール

  • denite.nvim
    • 色々なインターフェース
    • python3 依存
  • defx.nvim
    • ファイルマネージャー
    • python3 依存
  • coc.nvim
    • LSPサーバー
    • node 依存
  • nvim-treesitter
    • 複数言語のシンタックスハイライター
    • HEAD の neovim 依存
$ brew install --HEAD neovim
$ pip install pynvim
$ npm install -g neovim

neovim に入って checkhealth を実行して問題なければ ok

$ nvim
:checkhealth

neovim をデフォルトのエディタに設定するために .zprofile を編集

.zprofile
- export EDITOR='nano'
+ export EDITOR='nvim'
- export VISUAL='nano'
+ export VISUAL='nvim'
- export PAGER='less'
+ export PAGER='nvim -R'
+ export MANPAGER='nvim -c "set ft=man" -R'

私の init.vim

init.vim の詳細については省略.以下のコマンドで私が個人的に継ぎ足し継ぎ足ししている init.vim をとってこれる

自分用
$ cd ~/.config
$ git clone https://github.com/shimehituzi/nvim.git

dein

私はプラグインマネージャーとして dein を使っているので以下のコマンドでインストールする

$ curl https://raw.githubusercontent.com/Shougo/dein.vim/master/bin/installer.sh > installer.sh
$ sh ./installer.sh ~/.cache/dein

これを実行すると下記のような出力を得るので ~/.config/nvim/init.vim に貼り付ける

"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif

" Required:
set runtimepath+=/Users/You/.cache/dein/repos/github.com/Shougo/dein.vim

" Required:
call dein#begin('/Users/You/.cache/dein')

" Let dein manage dein
" Required:
call dein#add('/Users/You/.cache/dein/repos/github.com/Shougo/dein.vim')

" Add or remove your plugins here like this:
"call dein#add('Shougo/neosnippet.vim')
"call dein#add('Shougo/neosnippet-snippets')

" Required:
call dein#end()

" Required:
filetype plugin indent on
syntax enable

" If you want to install not installed plugins on startup.
"if dein#check_install()
"  call dein#install()
"endif

"End dein Scripts-------------------------

私の init.vim では, この状態で neovim に入ると使用しているプラグインがインストールされ,次のコマンドを打つといくつかのプラグインがセットアップされる.

:UpdateRemotePlugins

coc.nvim

私の neovim では LSP クライアントとして coc.nvim を使っているので必要な LSP サーバを以下のようなコマンドでインストール
下の例では json typescript javascript の LSP サーバーを導入している

neovimのコマンドライン
:CocInstall coc-json coc-tsserver coc-eslint

git

デフォルトで入ってはいるが brew で最新版をインストール

$ brew install git

git のグローバル設定

コマンドで --global してやってもいいのかもしれない

個人的にお気に入りの設定.(そもそも name と email はないと git が使えない)

  • git の pager を nvim にしている
  • git pull した時に remote の削除ずみブランチが反映される設定をしている
~/.gitconfig
[user]
    name = YOUR_NAME
    email = username@gmail.com
[core]
    page = nvim -R
[color]
    pager = no
[filter "lfs"]
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
    process = git-lfs filter-process
    required = true
[fetch]
    prune = true
[init]
    defaultBranch = master

github-cli

github を cli で操作できるやつ

インストール

brew でインストールできる

brew install gh

zsh の補完

~/.zshrc に以下を記述

eval "$(gh completion -s zsh)"

認証

以下のコマンドで認証できる

gh auth login

私は以下のように設定した

❯ gh auth login
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser

! First copy your one-time code: ****-****
- Press Enter to open github.com in your browser...
✓ Authentication complete. Press Enter to continue...

- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as *********

gh auth status で確認ができるように,これで ~/.config/gh/hosts.yml に github のログイン情報が保存される

この github-cli の認証経由で,githubへssh-keyの登録などをしなくても, git push, git pull が可能になるところが個人的にいいなと思った.

ちなみに HTTPS ではなく SSH を protocol に選ぶと ~/.ssh に新しい ssh-key が生成されて github にも登録されるっぽい

github-cli でできること

  • リポジトリの新規作成
  • リポジトリのクローン
  • リポジトリをブラウザで表示
  • プルリクエストの操作
  • イシューの操作

などなどいろいろできるっぽい

その他

trash

trash は MacOS のゴミ箱に入らないファイルを入れてくれるもの
rm -rf なんて怖いことをせずにディレクトリを削除できる

$ brew install trash

tree

ディレクトリをツリー表示してくれるやつ

$ brew install tree

全角スペース無効

bigsurでは無理らしい

4
10
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
4
10