Skip to content

Instantly share code, notes, and snippets.

@peppemas
Created November 9, 2018 10:52
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 peppemas/2e2a18d8acfce5473a836743aa7096b6 to your computer and use it in GitHub Desktop.
Save peppemas/2e2a18d8acfce5473a836743aa7096b6 to your computer and use it in GitHub Desktop.
Sumblime Text Plugin: Remove ANSI escape sequence from selected text
#######################################
# Sublime Text Plugin
# Remove ANSI Escape sequence from text
# Written by Giuseppe Mastrangelo
#
# select text and from console execute:
# view.run_command("remove_ansi_escape")
#######################################
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment