Skip to content

Instantly share code, notes, and snippets.

@backslash-f
Last active October 13, 2020 09:09
Show Gist options
  • Save backslash-f/eea7c0a38f92f56a527808465e08b53c to your computer and use it in GitHub Desktop.
Save backslash-f/eea7c0a38f92f56a527808465e08b53c to your computer and use it in GitHub Desktop.
[Sublime] Custom plugin to remove zero width spaces (u200b) / key bind
###
# Save this as a .py file in your user package dir, e.g.:
# /Users/<name>/Library/Application Support/Sublime Text 3/Packages/User/u200b_killer.py
###
# Source: https://rdzhou.github.io/2017/07/31/How-to-Remove-u200b/
import sublime, sublime_plugin
class ShowU200b(sublime_plugin.TextCommand):
def run(self, edit):
u200b_regions = self.view.find_all('\u200b')
if len(u200b_regions) > 0:
self.view.add_regions("u200b_regions", u200b_regions, "string", "circle")
self.view.show_popup('u200b detected!')
else:
self.view.erase_regions("u200b_regions")
sublime.status_message("🔪 u200b killer: no zero-width-spaces found.")
class RemoveU200b(sublime_plugin.TextCommand):
def run(self, edit):
regions = self.view.find_all('\u200b')
u200b_chars = len(regions)
if u200b_chars > 0:
sublime.status_message("🔪 u200b killer: %s found." %(u200b_chars))
for r in reversed(regions):
self.view.erase(edit, r)
else:
sublime.status_message("🔪 u200b killer: no zero-width-spaces found.")
###
# Now save this one as a .sublime-commands file in your user package dir, e.g.:
# /Users/<name>/Library/Application Support/Sublime Text 3/Packages/User/u200b_killer.sublime-commands
###
[
{
"caption": "Show U200b",
"command": "show_u200b"
},
{
"caption": "Remove U200b",
"command": "remove_u200b"
}
]
###
# Finally configure your keybindings as in this example
###
[
{
"keys": ["super+shift+c"],
"command": "show_u200b"
},
{
"keys": ["super+shift+d"],
"command": "remove_u200b"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment