It’s my first plugin and it’s super simple but there could easily be somethings worth knowing from this e.g. python coding practices, potential bugs
import sublime import sublime_plugin class EscapeAndPasteCommand(sublime_plugin.TextCommand): def run(self, edit, **kwargs): # Get the clipboard text originalText = textToPaste = sublime.get_clipboard() # Loop through the arguments making changes where possible for replacement in kwargs: # print("replacing '" + replacement + "'' with '" + kwargs[replacement] + "'") textToPaste = textToPaste.replace(replacement,kwargs[replacement]) # Place the text back into the clipboard and paste in place sublime.set_clipboard(textToPaste) self.view.run_command("paste") # Restore the original text to the clipboard sublime.set_clipboard(originalText)
So the idea is you feed this a dictionary of matches and replacements that it will perform on the clipboard object. Once those changes are made it restores the untouched text to the clipboard.
So I made a key binding for this so that when I am pasting text, in my markdown files, that has file paths it will autoescape it for me e.i. underscores and slashes that are meant to be just that.
{ "keys": ["ctrl+shift+v"], "command": "escape_and_paste", "args": { "\": "\\", "_" : "\_", "*" : "\*" } }
It works well for me and I think I would be lost without it now makes my documentation processes.