Skip to content

Instantly share code, notes, and snippets.

@gimenete
Created August 24, 2012 10:07
Show Gist options
  • Save gimenete/3448719 to your computer and use it in GitHub Desktop.
Save gimenete/3448719 to your computer and use it in GitHub Desktop.
SublimeText2 plugin for escaping code. Useful for languages that don't support heredoc
[
{
"keys": ["command+ctrl+e"], "command": "escape_code"
}
]
import sublime, sublime_plugin
# Transforms the selected text by escaping especial characters (\t, \n, \r,...)
# Made following this tutorial
# http://net.tutsplus.com/tutorials/python-tutorials/how-to-create-a-sublime-text-2-plugin/
# SublimeText2 API reference http://www.sublimetext.com/docs/2/api_reference.html
class EscapeCodeCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
content = self.view.substr(region)
content = content.replace('\\', '\\\\')
content = content.replace('\t', '\\t')
content = content.replace('\r', '\\r')
content = content.replace('\n', '\\n')
content = content.replace('\b', '\\b')
content = content.replace('\f', '\\f')
content = content.replace('\'', '\\\'')
content = content.replace('\"', '\\\"')
self.view.replace(edit, region, content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment