Key Bindings for Clojure

When I write Clojure, I use VS Code and Calva on a Mac and I try to avoid RSI. Everything I do in VS Code is accessible via the Command Palette, Cmd+Shift+P or F1. However, keyboard shortcuts are sure nice. I execute some commands frequently, and these commands would benefit the most from keyboard shortcuts. There are only so many reasonable shortcuts and my memory is only so good, so I want a system that benefits from mnemonics. This reminds me of this standup routine, Gary Gulman On How The States Got Their Abbreviations (warning, a little inappropriate!).

The default Calva keybindings for Paredit can be found here, https://calva.io/paredit/. Not everything we'll discuss is for Paredit. Let's begin. I'll say the Command Title, the keybinding, and some commentary. To change keybindings, in the Command Palette, either run

  • Preferences: Open Keyboard Shortcuts or

  • find the specific command you want to run and select the gear icon.

Calva: Load/Evaluate Current File and its Requires/Dependencies.

I bound that to Option+V.

Calva: Evaluate Current Form (or selection, if any)

I left that as Ctrl+Enter. This one is pretty obvious - evaluate the thing next to my cursor.

Calva: Evaluate Top Level Form (defun)

I left this as Option+Enter. That may have been the default. When might I use this? Anytime my cursor isn't at the opening or closing of a form. Example: I make a change to a function and want to evaluate it:

; Start with this
(defn greet
  []
  (println "hello"))

; I'm using | to indicate where my cursor is.
; Then I eval the top level form
(defn greet
  []
  (println "hi|"))

Calva Paredit: Raise Sexp

Option+R. Raise is awesome.

; Here's the starting point. Again, | is my cursor.
; So I don't want the try/catch anymore.
(try
  |(println "Did")
  (catch Exception _
    (println "Tried")))

; raise!
(println "Did")

Calva Paredit: Slurp Sexp Forward

Ctrl+Option+Right

(+ 1|) 2

; slurp forward!
(+ 1 2)

Calva Paredit: Splice Sexp

Option+S

(+ 1 (|2))

(+ 1 2)

Calva Paredit: Wrap Around ()

Option+P

random-uuid|

(random-uuid)

Calva Paredit: Wrap Around [] and Calva Paredit: Wrap Around {}

I don't use these nearly as much as (), so I left the defaults - Ctrl+Shift+Option+S/C.

Calva: Run Current Test

Command+T

Calva: Run Tests for Current Namespace

Option+Command+T

Calva Paredit: Convolute Sexp ¯\_(ツ)_/¯

Ctrl+Option+C. What does Convolute do? This:

(:a
 (:b
  |(:c)))

(:b
  (:a
   (:c)))

When would someone do that?

(deftest my-test
  (let [a true] ; I want to move this inside the testing form
    (testing "a"
      |(is (true? a)))))

(deftest my-test
  (testing "a"
    (let [a true]
      (is (true? a)))))

Did you find this article valuable?

Support Brett Rowberry by becoming a sponsor. Any amount is appreciated!