Clojure support for Sublime Text 4
This package provides Clojure support for Sublime Text and includes:
- Clojure and EDN syntax grammars (Sublime Text 3+)
- Code formatter/indenter (Sublime Text 4075+)
- nREPL, Socket REPL, ShadowCLJS REPL clients (Sublime Text 4075+)
Installation
Package Control: Install Package
โ Clojure Sublimed
Clojure syntax
Clojure Sublimed ships with its own syntax definition for Clojure and EDN. Unlike default Clojure syntax, this package is:
- slightly more pedantic as per EDN spec and Clojure Reader,
- rigorously tested,
- can be used to highlight rainbow parentheses,
- punctuation and validation inside regexps,
- quoted and unquoted regions are marked for highlighting,
- semantically correct tokenization, perfect for fonts with ligatures,
- has separate EDN syntax, same way JSON is separate from JavaScript in Sublime Text.
How to enable? Assign syntax to Clojure files:
- open any clj/cljc/cljs file,
- run
View
โSyntax
โOpen all with current extension as...
โClojure Sublimed
โClojure (Sublimed)
.
Want to put your parser to test? Check out syntax_test_edn.edn and syntax_test_clojure.cljc.
Formatter/indenter
Clojure Sublimed includes optional support for Simple Clojure Formatting rules. It doesnโt require REPL connection, any Clojure runtime or external tools.
To reformat whole file, run Clojure Sublimed: Reindent Buffer
.
To reindent only selected line(s), run Clojure Sublimed: Reindent Lines
.
To enable reindenting/formatting on save, add format_on_save: true
to settings. (See how to edit settings)
To enable correct indentations as you type code, rebind Enter
to Clojure Sublimed: Insert Newline
:
{"keys": ["enter"],
"command": "clojure_sublimed_insert_newline",
"context": [{"key": "selector", "operator": "equal", "operand": "source.edn | source.clojure"},
{"key": "auto_complete_visible", "operator": "equal", "operand": false},
{"key": "panel_has_focus", "operator": "equal", "operand": false}]}
Best way to do it is through running Preferences: Clojure Sublimed Key Bindings
.
REPL clients
Clojure Sublimed REPL clients enable interactive development from the comfort of your editor.
Principles:
- Minimal distraction. Display evaluation results inline.
- Decomplected. Eval code and nothing more.
Features:
- evaluate code,
- display evaluation results inline.
- display stack traces inline,
- interrupt evaluation,
- eval multiple forms at once (parallel evaluation),
- lookup symbol info,
- show evaluation time,
- bind keys to eval arbitrary code.
Clojure Sublimed has four REPL clients:
- Raw nREPL: no extra middlewares, could work with any nREPL server.
- JVM nREPL: only works with JVM server.
- ShadowCLJS nREPL: works with ShadowCLJS
- JVM Socket REPL: works with raw Socket REPL on JVM
All four support same basic features (eval, lookup, interrupt on JVM), maybe with slightly different quality of implementation.
How to choose which REPL to use?
- Are you on JVM? Use Socket REPL.
- CLJS? Use ShadowCLJS REPL.
- Only have basic nREPL? Use Raw nREPL.
- JVM nREPL is mostly a carryover from Clojure Sublime v1-2 and is now superseded by Socket REPL. Works better than Raw nREPL but worse than Socket REPL.
We intentionally excluded following features:
- Autocomplete. Static analysis is much simpler and much more reliable than requiring an always-live connection to the working app.
Look at Sublime LSP with Clojure LSP or SublimeLinter with clj-kondo if you need autocompletion.
How to use
For Clojure apps:
- Run Socket Server, e.g. with either
clj -X clojure.core.server/start-server :name repl :port 5555 :accept clojure.core.server/repl :server-daemon false
or
clj "-J-Dclojure.server.repl={:port 5555 :accept clojure.core.server/repl :server-daemon false}"
- Run
Clojure Sublimed: Connect to Socket REPL
command.
For Shadow-cljs apps:
- Run
shadow-cljs watch app
. (This starts a HTTP server and an nREPL) - If you are building a web-app, open the http-server url (from step 1) in the browser. This connects the shadow server to JS runtime.
- Run
Clojure Sublimed: Connect shadow-cljs
command.
For other nREPL apps:
- Run nREPL server.
- Run
Clojure Sublimed: Connect to raw nREPL
command.
Evaluating code from buffer
From here you have three options:
Clojure Sublimed: Evaluate
without selection evaluates topmost form around your cursor:
Clojure Sublimed: Evaluate
with selection evaluates selected text:
Clojure Sublimed: Evaluate Buffer
will evaluate the entire file:
You donโt have to wait for one form to finish evaluating to evaluate something else. Multiple things can be executed in parallel:
By default, Clojure Sublimed will also print evaluation time if it takes more than 100 ms:
Copying evaluation results
Sometimes you want to copy evaluation result. It is recommended to rebind Cmd+C
/Ctrl+C
from copy
to sublime_clojure_copy
. This will copy evaluation result if inside evaluated region and fallback to default copy
otherwise.
Interrupting
If your evaluation runs too long and you want to interrupt it, run Clojure Sublimed: Interrupt Pending Evaluations
:
Opening stacktrace
If your evaluation failed, put your cursor inside failed region and run Clojure Sublimed: Toggle Stacktrace
:
Clojure Sublimed will display stacktraces in a Clojure-friendly way. Compare with the default REPL:
Looking up symbol
To show symbol info, run Clojure Sublimed: Toggle Symbol Info
:
Universal Clojure Sublimed: Toggle Info
command acts as either Toggle Stacktrace
or Toggle Symbol Info
, depending on context.
Binding keys to eval code
Every project is different, and sometimes itโs convenient to run a piece of code so often youโd want it on a shortcut. It might be a namespace reload, test execution, database reconnect, linter, formatter โ possibilities are endless.
To support such use cases, Clojure Sublimed allows you to bind arbitrary piece of code to a keyboard shortcut. Run Preferences: Clojure Sublimed Key Bindings
and add something like this:
{"keys": ["ctrl+t"],
"command": "clojure_sublimed_eval_code",
"args": {"code": "(clojure.test/run-all-tests)"}}
Then, whenever you press Ctrl + T, youโll see the result in the status bar, like this:
Tip: use (clojure.test/run-all-tests (re-pattern (str *ns*))))
to run tests in current namespace.
Tip: bind (user/reload)
to Cmd R to reload your app with tools.namespace.
Clearing results
Finally, to clear evaluation results run Clojure Sublimed: Clear Evaluation Results
.
Editing settings
To edit settings, run Preferences: Clojure Sublimed Settings
command.
Session-wide settings
It is sometimes desirable to set dynamic Clojure vars for the whole session. To do that, edit "eval_shared"
setting. For example:
"eval_shared": "(do (set! *warn-on-reflection* true) (set! *print-namespace-maps* false))"
This will be applied to every evaluation.
Default Key Bindings
Clojure Sublimed comes with no keybindings enabled by default to guarantee they wonโt conflict with any other extension (Sublime Textโs limitation).
This is the recommended keymap:
Command | macOS | Windows/Linux | Mnemonic |
---|---|---|---|
Evaluate | Ctrl Enter | Ctrl Alt Enter | |
Evaluate Buffer | Ctrl B | Ctrl Alt B | [B]uffer |
Interrupt Pending Evaluations | Ctrl C | Ctrl Alt C | [C]ancel |
Toggle Info | Ctrl I | Ctrl Alt I | [I]nfo |
Clear Evaluation Results | Ctrl L | Ctrl Alt L | c[L]ear |
Copy Evaluation Results | Command C | Ctrl C | [C]opy |
Reindent Lines | Ctrl F | Ctrl Alt F | [F]ormat |
Reindent Buffer | Ctrl Shift F | Ctrl Alt Shift F | Capital [F]ormat |
To set it up, run Preferences: Clojure Sublimed Key Bindings
command and copy example keybindings to your local Key Bindings file.
stdout/stderr
Clojure Sublimed does things a little different when it comes to stdout. Normally REPL would show you all stdout/stderr that originated from your session. I find it confusing, because it doesnโt always work and you have to check two places for output. Moreover, thereโs no output panel, so thereโs no place to show stdout anyway.
So instead, Clojure Sublimed does not redirect neither stdout nor stderr. Check original console to see what was printed there.
Frequently Asked Questions
Q: REPL/eval doesnโt work
A: Make sure you are using nREPL 0.9 or later. A: Also check console (Cmd `) for errors
Q: How to connect to a Babashka REPL?
A: Use Raw nREPL.
Credits
Made by Niki Tonsky.
With contributions by Jaihindh Reddy.
See also
Writer Color Scheme: A color scheme optimized for long-form writing.
Alabaster Color Scheme: Minimal color scheme for coding.
Sublime Profiles: Profile switcher.