LoginSignup
52
51

More than 1 year has passed since last update.

Macへのspacemacsインストールメモ

Last updated at Posted at 2018-07-29

普段エディタとして Emacs を使用していますが、vimとのいいとこ取りをした(?) 「spacemacs」 なるものがあることを(今さら)知ったので、インストールして使うことにしました。

公式のインストール手順 も丁寧に書かれているし、特につまずくところもないのですが、メモとして残しておきます。

spacemacs2.png

インストール

事前に

インストール済みEmacsのアンインストール

ベースとするEmacsとして、公式のインストール手順にも記載されている emacs-plus を使うので、
すでにEmacsがインストールされている場合はアンインストールしておきます。

.emacsの退避

既存のEmacs設定 ~/.emacs.d および ~/.emacs をリネームするなどして退避しておきます。

$ cd ~
$ mv .emacs.d .emacs.d.bak
$ mv .emacs .emacs.bak

emacs-plusのインストール

改めて emacs-plus をインストールします。

$ brew tap d12frosted/emacs-plus
$ brew install emacs-plus --with-spacemacs-icon
$ ln -s /usr/local/opt/emacs-plus/Emacs.app /Applications

spacemacsのインストール

spacemacs をインストールします。
インストールというより、 spacemacs 用に構成された設定ファイル群 .emacs.d をGutHubからクローンして配置するだけですね。

推奨フォントのインストール

公式のインストール手順 では任意になっていますが、見やすいフォントなのでインストールしておきます。

$ brew tap homebrew/cask-fonts && brew install font-source-code-pro --cask

spacemacsのクローン

GitHub から、spacemacs をクローンして ~/.emacs.d として配置します。

$ cd ~
$ git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d

Emacsの起動

Emacsを起動します。

$ emacs

spacemacs のインターフェースで起動すれば、インストールは成功です。

first_run.png

基本設定

初回起動時に、いくつかの基本設定をどうするか聞いてきます。

キーバインド

What is your preferred editing style?
  Among the stars aboard the Evil flagship (vim)
  On the planet Emacs in the Holy control tower (emacs)

キーバインドをvim風にするか、emacsのままにするかを選択します。
spacemacs の名の通り、vim風にすることで(escではなく) space によるモード切り替えができるようになるのが最大のメリットです。

自分はあくまでEmacsキーバインドを使いたかったので、emacs を選択しました。

ちなみに、ここでは選択肢として出てこないですが、hybrid と言うキーバインドもあります。(spacemacs の設定ファイル ~/.spacemacs で変更できます)
ただ、vimのキーバインドが優先のようで、Emacsキーバインド派には使いにくいなーと思います。

spacemacsの構成

What distribution of spacemacs would you like to start with?
  The standard distribution, recommended (spacemacs)
  A minimalist distribution that you can build on (spacemacs-base)

spacemacs を標準構成にするか、最小構成にするかを選択します。

推奨の spacemacs を選択しました。

補完フレームワークの選択

What type of completion framework do you want?
  A heavy one but full-featured (helm)
  A lighter one but still very powerful (ivy)
  None (not remomended)

編集時の補完に何を使うかを選択します。
この辺、詳しくないですが 君は誰とEmacsる? (補完インターフェイス紹介篇) を参考に、軽量そうな ivy にしました。

カスタマイズ

あとはお好みでカスタマイズしていきます。
spacemacs では、設定は通常の ~/.emacs.d/init.el ではなく ~/.spacemacs に記述していきます。

ターミナル内で起動する

ターミナル上で emacs コマンドで起動すると、ターミナル上ではなく別ウィンドウでアプリとして起動してしまいます。
-nw オプションを指定することでターミナル上で起動できますが、毎回指定するのが面倒なので ~/.bash_profile~/.zshrc でエイリアスを切っておきます。

alias emacs='emacs -nw'

テーマの変更

~/.spacemacsdotspacemacs-themes でテーマを変更できます。
テーマの一覧は Spacemacs Themes にあります。(テーマ名とサムネイルがズレてますが...)

複数指定でき、先頭に指定したテーマが起動時に適用されます。
Alt-m T n でテーマを切り替えることができます。

~/.spacemacs
   ;; List of themes, the first of the list is loaded when spacemacs starts.
   ;; Press <SPC> T n to cycle to the next theme in the list (works great
   ;; with 2 themes variants, one dark and one light)
   dotspacemacs-themes '(spacemacs-dark
                         spacemacs-light)

レイヤーの導入

レイヤーのインストール

通常のEmacsで追加機能をインストールするには、package-install などを使ってインストールしますが、
spacemacs では「レイヤー」と呼ばれる独自の機能拡張の仕組みを使います。

設定ファイル ~/.spacemacs に、インストールするレイヤーを指定する項目があり、
そこにインストールしたいレイヤーを追記することで起動時に自動的にインストールされます。

~/.spacemacs
   ;; List of configuration layers to load.
   dotspacemacs-configuration-layers
   '(
     ;; ----------------------------------------------------------------
     ;; Example of useful layers you may want to use right away.
     ;; Uncomment some layer names and press <SPC f e R> (Vim style) or
     ;; <M-m f e R> (Emacs style) to install them.
     ;; ----------------------------------------------------------------
     ivy
     ;; auto-completion
     ;; better-defaults
     emacs-lisp
     ;; git
     ;; markdown
     ;; org
     ;; (shell :variables
     ;;        shell-default-height 30
     ;;        shell-default-position 'bottom)
     ;; spell-checking
     ;; syntax-checking
     ;; version-control
     )

この状態では、(起動時に選択した補完フレームワークの)ivy が有効化されています。
レイヤー一覧 からインストールしたいレイヤーを探して、ここに追記していけばOKです。

あと、特定のファイル(JSONとかYAMLとか)を開こうとすると、対応するレイヤーをインストールするか聞いてくるので、そこで都度インストールしても良いです。

僕は以下のレイヤーをインストールしました。

レイヤー 説明
ivy キーワード補完(初回起動時に選択)
better-defaults Emacsキーバインドの機能強化
osx macOS向けの機能強化
shell Emacs内でいろいろなシェルを使えるようにする
git Gitクライアント(magit)を使えるようにする
version-control バージョン管理のサポート。変更行にマークが付いたりする。
emacs-lisp Emacs Lisp の言語サポート(デフォルトで有効になってる)
markdown マークダウンの言語サポート
html HTMLの言語サポート
javascript JavaScriptの言語サポート
yaml YAMLの言語サポート
java Javaの言語サポート
scala Scalaの言語サポート
python Pythonの言語サポート
sql SQLの言語サポート
org org-mode を使えるようにする(注1)
  • 注1 2018/08/01現在の spacemacs には、orgレイヤーのインストールに問題があるようです。
    GitHubのIssue にある回避方法(package.el/org-projectile:per-repo の行をコメントアウトする)でインストールできました。

パッケージの導入

前述の通り、spacemacs の機能追加はレイヤーを追加するのが基本ですが、
レイヤーとして提供されていないパッケージについては、パッケージ単体で導入することも可能です。

パッケージのインストール

~/.spacemacsdotspacemacs-additional-packages にインストールしたいパッケージを追記します。

~/.spacemacs
   ;; List of additional packages that will be installed without being
   ;; wrapped in a layer. If you need some configuration for these
   ;; packages, then consider creating a layer. You can also put the
   ;; configuration in `dotspacemacs/user-config'.
   dotspacemacs-additional-packages
   '(
     minimap
     )

パッケージの設定

dotspacemacs-additional-packages で追加したパッケージの設定は user-config() 関数に記述します。

~/.spacemacs
(defun dotspacemacs/user-config ()
  "Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration.
This is the place where most of your configurations should be done. Unless it is
explicitly specified that a variable should be set before a package is loaded,
you should place your code here."

  ;; MiniMapを右に表示する
  ;; https://github.com/dengste/minimap/blob/master/minimap.el
  (setq-default minimap-window-location 'right)
  )

ユーザーの独自設定

ユーザー独自の設定は、~/.spacemacsuser-init() 関数に記述するようです。

僕の場合は以下の設定を追加しました。

~/.spacemacs
(defun dotspacemacs/user-init ()
  "Initialization function for user code.
It is called immediately after `dotspacemacs/init', before layer configuration
executes.
 This function is mostly useful for variables that need to be set
before packages are loaded. If you are unsure, you should try in setting them in
`dotspacemacs/user-config' first."
  ;; 初期Frameサイズを指定
  (setq inhibit-startup-screen t)
  (setq initial-frame-alist
        '((top . 30) (left . 70) (width . 180) (height . 55))
        )

  ;; 初期モードをtextモードにする
  (setq initial-major-mode 'text-mode)

  ;; ウィンドウを半透明にする
  (if window-system
      (progn
        (set-frame-parameter nil 'alpha 95)
        )
    )

  ;; Magit(Gitクライアント)をフルスクリーンで表示する
  ;; https://github.com/syl20bnr/spacemacs/tree/master/layers/%2Bsource-control/git#magit-status-fullscreen
  (setq-default git-magit-status-fullscreen t)

  ;; Ctrl+x g でMagitを起動する
  (global-set-key (kbd "C-x g") 'magit-status)
  )

バイトコンパイル

こちらの記事 Emacs 26.1 が出た! を参照。

キーバインド

  • 公式ドキュメントに コマンド一覧 があります。

    • vimキーバインドの SPC と Emacsキーバインドの Alt-m は同義なので、読み替えれば同じ操作ができます。
      例えば、終了は vimキーバインドでは SPC q q ですが、Emacsキーバインドでは Alt-m q q です。
      もちろん、Emacs標準の Ctrl-x Ctrl-c でも終了できます。
  • チートシート spacemacs-cheatsheet

こんなのあるんだ〜と思ったキーバインド

キーバインド コマンド 説明
Alt-F10 or Alt-m T M toggle-frame-maximized ウィンドウを最大化/最小化します。
Alt-m ' spacemacs/default-pop-shell Emacs内でデフォルトのシェルを起動します。
Alt-m T n cycle-spacemacs-theme ダークテーマとライトテーマを切り替えます。
Alt-m t l spacemacs/toggle-truncate-lines ウインドウ幅で行末を折り返す/折り返さないを切り替えます。
Alt-m f t neo-tree 左にディレクトリツリー(NeoTree)を表示します。
Alt-m f r Recent files 最近開いたファイルの一覧を表示します。
Alt-m f o open-file-or-directory-in-external-app ファイルを外部アプリで開きます。
Alt-m g s magit-status Gitクライアント magit を起動します。
Alt-m z . Folding JSONやYAMLなどの要素を開いたり閉じたりします。
Alt-m v v v... Expand Region 選択範囲を広げていきます。
Alt-ENTER g g spacemacs/jump-to-definition カーソル位置(型は関数など)の定義にジャンプします。
Ctrl-S Swiper Swiper でインクリメンタルサーチします。
Ctrl-c p l projectfile-find-finle-in-directory ディレクトリ(サブディレクトリ含む)下のファイルをファイル名で探します。

トラブルシューティング

起動時にメッセージ「You appear to be setting environment variables ("PATH") in ...」が出る

起動時にメッセージバッファに以下のメッセージが表示される場合は、~/.bashrc~/.zshrc の中で環境変数 PATH を設定していることが原因。
~/.bash_profile および ~/.zshenv で設定したほうが良いよ、と言うアドバイス)

You appear to be setting environment variables ("PATH") in your .bashrc or .zshrc: those files are only read by interactive shells, 
so you should instead set environment variables in startup files like .profile, .bash_profile or .zshenv.  
Refer to your shell’s man page for more info.  
Customize ‘exec-path-from-shell-arguments’ to remove "-i" when done, 
or disable ‘exec-path-from-shell-check-startup-files’ to disable this message.

対処としてはアドバイスどおりにするか、.spacemacsuser-initexec-path-from-shell-check-startup-files nil を指定してこのメッセージを表示しないようにする。

~/.spacemacs
(defun dotspacemacs/user-init ()
  "Initialization function for user code.
It is called immediately after `dotspacemacs/init', before layer configuration
executes.
 This function is mostly useful for variables that need to be set
before packages are loaded. If you are unsure, you should try in setting them in
`dotspacemacs/user-config' first."
  ;; 起動時に、.zshrcでPATHを設定している警告を出さないようにする
  (setq exec-path-from-shell-check-startup-files nil)

最後に

vimキーバインドを使わないにしても、素のEmacsをいちからカスタムしていくよりもお手軽だと思います。
(逆に言えば、細かいカスタマイズはやりにくいかもしれませんね)

色々な機能が追加されていて、起動がやや遅い(3秒くらい待たされる感じ)のがネックですが、便利だとは思いますね。

spacemacs.png

参考文献

52
51
4

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
52
51