Skip to content

Instantly share code, notes, and snippets.

@mitranim
Last active April 30, 2020 06:52
Show Gist options
  • Save mitranim/70df2b39aa0e4400740f96e0f31a6a99 to your computer and use it in GitHub Desktop.
Save mitranim/70df2b39aa0e4400740f96e0f31a6a99 to your computer and use it in GitHub Desktop.
Sublime Text plugin for inserting IDs and sequences
import sublime_plugin
import uuid
import random
import datetime
class gen_uuid(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
self.view.replace(edit, region, str(uuid.uuid4()))
class gen_uuid_no_dashes(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
self.view.replace(edit, region, uuid.uuid4().hex)
# TODO: option to pad seq with zeros
class gen_seq(sublime_plugin.TextCommand):
def run(self, edit, start = 0):
num = start
for region in self.view.sel():
self.view.replace(edit, region, str(num))
num += 1
hex_chars = '0123456789abcdef'
class gen_hex(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
start = min(region.a, region.b)
end = max(region.a, region.b)
text = ''.join(random.choice(hex_chars) for _ in range(end - start))
self.view.replace(edit, region, text)
class gen_datetime(sublime_plugin.TextCommand):
def run(self, edit):
text = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M%:%SZ')
for region in self.view.sel():
self.view.replace(edit, region, text)
[
{ "caption": "Gen: Insert UUID", "command": "gen_uuid" },
{ "caption": "Gen: Insert UUID (No Dashes)", "command": "gen_uuid_no_dashes" },
{ "caption": "Gen: Insert 0 to N", "command": "gen_seq", "args": {"start": 0} },
{ "caption": "Gen: Insert 1 to N", "command": "gen_seq", "args": {"start": 1} },
{ "caption": "Gen: Replace With Hex", "command": "gen_hex", },
{ "caption": "Gen: Replace With Datetime", "command": "gen_datetime", },
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment