Clojure開発環境での基本操作まとめ: Spacemacs, IntelliJ IDEA (Cursive), VS Code (Calva), Vim (vim-iced), rebel-readline

[State of Clojure Community 2020](https://www.surveymonkey.com/results/SM-CDBF7CYT7/)によると、[Clojure](https://clojure.org/)開発環境の人気上位はEmacs/Spacemacs (43.23%), IntelliJ IDEA (31.73%), VS Code (9.88%), Vim (8.87%)ということで、これらのエディタ/IDEは国内のClojurianの間でもよく使われている印象があります。

そこで、今回の記事では以下5種類の開発環境で高頻度で利用すると思われる基本的な操作を簡単に整理してみます。

- [Spacemacs](https://www.spacemacs.org/) + [Clojure layer](https://github.com/syl20bnr/spacemacs/tree/develop/layers/+lang/clojure)
- [IntelliJ IDEA](https://www.jetbrains.com/ja-jp/idea/) + [Cursive](https://cursive-ide.com/)
- [VS Code (Visual Studio Code)](https://azure.microsoft.com/ja-jp/products/visual-studio-code/) + [Calva](https://marketplace.visualstudio.com/items?itemName=betterthantomorrow.calva)
- [Vim](https://www.vim.org/) + [vim-iced](https://github.com/liquidz/vim-iced)
- [rebel-readline](https://github.com/bhauman/rebel-readline)　※ エディタではなくClojure REPLをちょっとリッチにしてくれるライブラリ

その他のエディタも含めたClojure開発関連プラグインについては[Clojure/ClojureScript関連リンク集 > エディタプラグイン](https://qiita.com/lagenorhynque/items/68c314c288b75a9492ba#%E3%82%A8%E3%83%87%E3%82%A3%E3%82%BF%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3)が参考になるかもしれません。

また、rebel-readlineはエディタ/IDEによる高度なREPL関連機能が使えない状況で素のREPLを少し便利にしてくれるライブラリで、LeiningenやClojure CLIからいつでも使えるように設定しておくのがオススメです(cf. [参考: rebel-readlineの設定](#参考-rebel-readlineの設定))。

ちなみに、私[lagénorhynque](https://twitter.com/lagenorhynque) (a.k.a カマイルカ)はSpacemacsユーザでClojure以外も含めてほぼすべてSpacemacsで完結させている(Scala, JavaのためにIntelliJを起動することがたまにあるくらい)ので、他のエディタ/IDEでの操作にはとても不慣れです😅

# REPLの起動と接続

## Spacemacs + Clojure layer

- 操作: **`, '` (または `M-RET '`)してコマンド(e.g. `cider-jack-in-clj`)を選択**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/0f5d0585-609f-031b-023c-5a2f7282262e.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/f5faed26-5152-6c0a-1e13-20fca6325b7e.png)

※ REPLバッファを右側に開いてみた例(レイアウトなどはお好みで)

## IntelliJ IDEA + Cursive

- 初回の設定: Add Configurationで `Clojure REPL > Local` の実行/デバッグ設定を追加する

- 操作: **`Control+Shift+R`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/0c0d9c5c-4f94-d5d9-cd33-0d0792bbe05f.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/5d464b1b-f9aa-086b-dfc0-af1579e2c9a2.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/4a5a8406-0296-b1db-966e-3a4676b75c5b.png)

## VS Code + Calva

- 操作: **`Control+Option+C Control+Option+J` (または `Control+Alt+C Control+Alt+J`)してプロジェクトタイプ(e.g. `Leiningen`)とプロファイル(e.g. `:dev`, `:repl`)を選択**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/9b457539-d65c-f746-35f1-99d6a7a8ad93.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/a60beb96-e77a-9507-67f7-799c8ec33969.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/dbc38af1-9ae6-131a-417e-513a232837f5.png)

## Vim + vim-iced

- 操作: **`<Leader>"`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/5d5297eb-8f7b-d37d-7325-1d212b0abbd0.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/9b476cb0-be48-cd39-3f9e-2d14945b4556.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/dba56974-f0b0-63de-6de3-3286caa5046f.png)

※ 標準出力バッファを右側に開いてみた例(レイアウトなどはお好みで)

## rebel-readline

- Leiningenでの操作: **`lein trampoline run -m rebel-readline.main` (エイリアスが `rebel` なら `lein rebel`)**

- Clojure CLIでの操作: **`clojure -m rebel-readline.main` (エイリアスが `rebel` なら `clojure -M:rebel`)**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/116aa126-7a40-c810-2bfa-5c2e35740440.png)

# 定義ジャンプ(またはsourceの表示)

## Spacemacs + Clojure layer

- 操作: **`, g g` (または `M-RET g g`)**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/d83f7ec5-6b13-4311-2efc-40324e9f68ad.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/c915198e-311b-4c94-1a95-ad252fdbbd04.png)

## IntelliJ IDEA + Cursive

- 操作: **`Command+B` (または `Control+B`)**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/499b79ac-30c1-f4dc-9d41-7e86e9e0bcb2.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/f6bfe744-7eec-26a8-fe5f-8be84d0f6a2e.png)

## VS Code + Calva

- 操作: **`F12`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/c2176233-689a-9be6-87bc-d21220feefe3.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/a3b8b4b0-b2a1-ac75-e7f3-60e3c52c1582.png)

## Vim + vim-iced

- 操作: **`<C-]>`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/de1bede1-c760-e28c-804f-db17aaa39332.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/7f238b95-c049-be76-1855-d44a43522a1a.png)

## rebel-readline

- 操作: **`Contrl+X Control+S`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/fd9431d0-6aea-4df1-dafe-6f0899cb28e4.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/094314c4-0576-ddd6-63c5-85869965897b.png)

# docの表示

## Spacemacs + Clojure layer

- 操作: **`, h h` (または `M-RET h h`)**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/7271e342-8f12-0fb5-849a-f923cdde661f.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/8b1c8e64-9f76-6d0e-2a06-c4ac393914e4.png)

## IntelliJ IDEA + Cursive

- 操作: **`F1`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/cd79a8b6-291f-52a3-a657-cb10d44ad7e3.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/1ee23184-08a2-234e-2500-e6cbd8fd1bfd.png)

## VS Code + Calva

- 操作: **`Command+K Command+I` (または `Control+K Control+I`)**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/4ea0ac14-7bae-d0e4-bb97-20e21e55415e.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/fbdc6b85-9e8c-4989-1cd0-9fd48773801c.png)

## Vim + vim-iced

- 操作: **`<Leader>hb`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/7020d2de-18c7-7b09-fcde-c20745da2999.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/0b022c08-bdd4-2677-cf3f-048356148e51.png)

## rebel-readline

- 操作: **`Contrl+X Control+D`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/1d0503b7-d944-a655-9a27-b85f3f66edf7.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/2c133673-ec38-647f-34c5-c62227a93bef.png)

# 局所的なeval

## Spacemacs + Clojure layer

- 操作: **`, e e` (または `M-RET e e`)**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/48626c73-de25-52dd-14f3-50173a1a3179.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/84443d66-1ce4-9d95-9bf2-1c57131adde0.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/c3b8582d-42df-f01d-1096-aaace8973dfa.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/a0cff0a0-33c9-7a8d-5164-031ad79bc528.png)

## IntelliJ IDEA + Cursive

- 初回の設定: Keymapで `Plug-ins > Cursive > Send Form Before Caret to REPL` にショートカットを設定する(e.g. `Option+Shift+E`)

- 操作: **`Send Form Before Caret to REPL` のショートカット**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/f3e0f4dc-a206-6de0-5668-e471f2338913.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/c1a44ce3-7269-fa10-41d8-148e065327e5.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/5410b33e-f478-4326-9684-a1d2ee16e2a6.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/4e83e404-fcb0-84ff-1378-08809e2afb96.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/805d43f1-7b23-9ed0-1399-215f97b9e116.png)

## VS Code + Calva

- 操作: **`Control+Option+C E` (または `Control+Alt+C E`)**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/305f3b60-f9b0-50a2-7fd7-46a294c7e8e0.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/b30463db-6cf3-6a18-936c-04f3099d86fb.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/6b91dbb5-b370-d75a-2152-9e06cdf30f3c.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/bf718aec-a6d7-5cfe-3891-cd6643706c01.png)

## Vim + vim-iced

- 操作: **`<Leader>et`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/55955d40-fb4f-8b54-5cfe-371b3274c81d.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/c04c5012-5e61-6047-b57c-01b7134fc321.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/f641d330-4402-bbac-b41f-41ff80c104c5.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/51adf4e7-d3d5-0866-6a54-0dc8f929bd9b.png)

## rebel-readline

- 操作: **`Contrl+X Control+E`**

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/a6f56bea-0983-7230-85f3-1f881675212d.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/36c0f97f-41aa-8c20-75f6-0308b5c36794.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/671a361c-fe89-5b61-b166-11ea398cbf38.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/33430/54129986-982b-34c0-5857-d3a2a10fa6ec.png)

# 参考: rebel-readlineの設定

ClojureのデフォルトのREPLを便利にしてくれる[rebel-readline](https://github.com/bhauman/rebel-readline)は[README](https://github.com/bhauman/rebel-readline/blob/master/README.md)の説明に従って以下のようにプロジェクト横断でグローバルにLeiningenやClojure CLIに設定することができます(特定のプロジェクトに対してだけ組み込むことも可能です)。

## Leiningen

```clojure:~/.lein/profiles.clj
{:user {:dependencies [[com.bhauman/rebel-readline "0.1.4"]]
        :aliases {"rebel" ["trampoline" "run" "-m" "rebel-readline.main"]}}}
```

## Clojure CLI

```clojure:~/.clojure/deps.edn
{:aliases {:rebel {:extra-deps {com.bhauman/rebel-readline {:mvn/version "0.1.4"}}
                   :main-opts ["-m" "rebel-readline.main"]}}}
```

# Further Reading

- [Clojure/ClojureScript関連リンク集 - Qiita > エディタプラグイン](https://qiita.com/lagenorhynque/items/68c314c288b75a9492ba#%E3%82%A8%E3%83%87%E3%82%A3%E3%82%BF%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3)
- [Clojure開発環境でのリンターclj-kondo, Joker設定まとめ: Spacemacs, IntelliJ IDEA (Cursive), VS Code (Calva), Vim (vim-iced) - Qiita](https://qiita.com/lagenorhynque/items/dd9d6a1d97cbea738bc0)
- [Clojure開発環境でのフォーマッターcljstyle設定まとめ: Spacemacs, IntelliJ IDEA (Cursive), VS Code (Calva), Vim (vim-iced) - Qiita](https://qiita.com/lagenorhynque/items/a5d83b4a36a1cf1cacbe)
- [Clojureで快適なREPL駆動開発のために"reloaded workflow"を実践しよう - Qiita](https://qiita.com/lagenorhynque/items/a752ffae490e83ebe70d)