Skip to content

Instantly share code, notes, and snippets.

@symmetriq
Last active November 13, 2017 01:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save symmetriq/4315f9619f428f761d35 to your computer and use it in GitHub Desktop.
Save symmetriq/4315f9619f428f761d35 to your computer and use it in GitHub Desktop.
BBEdit: Toggle CamelCase _
#!/usr/bin/env ruby
#-------------------------------------------------------------------------------
# Toggle Camelcase _
#-------------------------------------------------------------------------------
# Jason Sims <jason@symmetriq.com>
#-------------------------------------------------------------------------------
#
# Toggles between camelcase and underscores
# e.g. doTheThing <-> do_the_thing
#
#-------------------------------------------------------------------------------
#
# Installation in BBEdit:
#
# 1. Place this file in your "Text Filters" directory:
# ~/Library/Application Support/BBEdit/Text Filters/
# or
# ~/Dropbox/Application Support/BBEdit/Text Filters/
#
# 2. Assign a keyboard shortcut to "Toggle CamelCase _" in:
# BBEdit → Preferences → Menus & Shortcuts → Text → Apply Text Filter
#
#-------------------------------------------------------------------------------
# More scripts & snippets here:
# https://gist.github.com/symmetriq
#-------------------------------------------------------------------------------
input = ARGF.set_encoding('UTF-8').read
if input.include? '_'
# has underscores; convert to camelCase
print input.gsub(/_[A-Za-z]/) {|char| char[1].upcase }
else
# treat as camelCase (nothing will happen if the string contains no capital letters)
print input.gsub(/([A-Z])/, '_\1').downcase
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment