Skip to content

Instantly share code, notes, and snippets.

@v-kolesnikov
Created November 21, 2015 21:31
Show Gist options
  • Save v-kolesnikov/aa5adf58bf7d66595a4a to your computer and use it in GitHub Desktop.
Save v-kolesnikov/aa5adf58bf7d66595a4a to your computer and use it in GitHub Desktop.
Wrangling Parentheses
The most effective way to edit Clojure code is structurally with paredit.vim, but if you don't have time to learn that, Vim still brings a lot to the table for dealing with all the parentheses in Clojure code.
First, obviously, the % motion (:help %) is very useful. In normal mode, put the cursor on an opening or closing paren and you can:
Hit % to jump to the matching paren.
Hit d% to delete the parens and everything they contain.
Hit y% to "yank"/copy the parens and everything in them.
Hit c% to delete the parens and the text they contain and start editing.
Hit v% to select the parens and the text they contain visually.
The % motion is useful, but it's often more convenient to work with Vim's "block" text object (:help text-objects). This manifests in two forms. First, ab ("all block") which is an entire form, including the parentheses. Second, ib ("inner block") which is all the text within the enclosing parentheses. So, put your cursor anywhere within some parentheses in normal mode and you can:
Hit dab ("delete all block") to delete the entire form.
Hit dib ("delete inner block") to delete everything inside the parens.
Hit cab ("change all block") to delete the entire form and enter insert mode.
Hit cib ("change inner block") to delete the contents of the form, preserving parens, and enter insert mode.
Hit yab ("yank all block") to copy the entire form including parens.
Hit yib ("yank inner block") to copy the everything inside the parens.
And so on. Getting these commands in muscle memory can really speed up working with Clojure forms.
Tip: Vim has text objects for blocks enclosed in square brackets (vectors), quotes (strings), curly braces (maps, sets) etc. They're all invaluable. :help text-objects !!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment