Convert CamelCase to lower_case_underscore and vice versa

With the following script you can easily convert a selected string in Geany to CamelCase or lower_case_underscore format depending on the source format of the string.


  • Python is required (works with Python 2 and 3)
  • Simply copy the script somewhere on your system, e.g. /home/<username>/bin/
  • Configure Geany: Edit→Format→Send Selection to→Set Custom Commands
  • In the dialog add a new command and simply use: python /home/<username>/bin/


Select some string in Geany and use Edit→Format→Send Selection to→<your configured command> and Geany will replace the string by the converted version. Alternatively, you can use the shortcut Ctrl-<number> in case your command is one of the first three configured commands. For details, read the documentation about Send Selection to commands.

The script
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def camel_case_to_lower_case_underscore(string):
    Split string by upper case letters.
    F.e. useful to convert camel case strings to underscore separated ones.
    @return words (list)
    words = []
    from_char_position = 0
    for current_char_position, char in enumerate(string):
        if char.isupper() and from_char_position < current_char_position:
            from_char_position = current_char_position
    return '_'.join(words)
def lower_case_underscore_to_camel_case(string):
    """Convert string or unicode from lower-case underscore to camel-case"""
    splitted_string = string.split('_')
    # use string's class to work on the string to keep its type
    class_ = string.__class__
    return splitted_string[0] + class_.join('', map(class_.capitalize, splitted_string[1:]))
def read_data():
def detect_conversion_method(data):
    if '_' in data:
        return lower_case_underscore_to_camel_case
        return camel_case_to_lower_case_underscore
def main():
    data = read_data()
    conversion_method = detect_conversion_method(data)
    result = conversion_method(data)
if __name__ == '__main__':