Sublime Text Plugin to remove ANSI color escape sequences from the selected text.

#######################################
# Sublime Text Plugin
#
# Sublime Text Plugin to remove ANSI color
# escape sequences from the selected text.
#
# Written by Giuseppe Mastrangelo
#
# Usage:
#
# select text and from console execute:
#
#     view.run_command("remove_ansi_escape")
#
# https://gist.github.com/peppemas/2e2a18d8acfce5473a836743aa7096b6
#######################################

import sublime
import sublime_plugin
import re


class RemoveAnsiEscapeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        selection = self.view.sel()
        for region in selection:
            region_text = self.view.substr(region)
            ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
            cleaned_text = ansi_escape.sub('', region_text)
            self.view.replace(edit, region, cleaned_text)