Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
Created December 18, 2018 21: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 mattdeboard/0b0255469c435517f51c89d7897d3886 to your computer and use it in GitHub Desktop.
Save mattdeboard/0b0255469c435517f51c89d7897d3886 to your computer and use it in GitHub Desktop.
Convert snake_case JSON keys in region to camelCase
(defun camelcase-keys-in-region (start end)
"Change snake_case JSON keys in the region to camelCase.
START and END are start/end of region."
(interactive "r")
(save-restriction (narrow-to-region start end)
(goto-char (point-min))
(while (re-search-forward "^ *[a-z0-9_]+:" nil t)
(while (re-search-backward "_\\(.\\)" (point-at-bol) t)
(replace-match (upcase (match-string 1)))))))
@mattdeboard
Copy link
Author

This is pretty inefficient (search forward, then backward) because the JSON I'm processing may have values that are also snake_case, so I can't just process all snake-cased words.

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