Skip to content

Instantly share code, notes, and snippets.

@dhoboy
Last active July 19, 2023 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhoboy/4001eb946d45e1c1118a95002c8cc4e8 to your computer and use it in GitHub Desktop.
Save dhoboy/4001eb946d45e1c1118a95002c8cc4e8 to your computer and use it in GitHub Desktop.
3 vim s commands to transform JS object to Clojure map syntax

If you are transforming a lot of JS object syntax into Clojure map syntax in vim, these three s commands can help you:

s/,//g
s/'/"/g
s/\(\w*\):/:\1/g

The first one is simple, it removes all commas from the line
The second one is also simple, it swaps any single quotes for double quotes
The third one is a little tricky, it swaps the colon from the end of the keyword to the beginning of the keyword

I saved each of these into a register and pasted them into my command mode with Control-R then typing the register letter. This is how you paste from a register in insert mode, and it works in command mode too.

It transforms lines of text like:
{ name: 'sam', age: 35, hobby: [ 'sports', 'sewing' ] }

To:
{ :name "sam" :age 35 :hobby [ "sports" "sewing" ]}

Maybe there's a way to make executing all three s commands in one macro, I haven't looked into that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment