Skip to content

Instantly share code, notes, and snippets.

@pudquick
Last active January 4, 2023 18:39
Show Gist options
  • Save pudquick/1dcedff8107e03588acc119d2fccb15c to your computer and use it in GitHub Desktop.
Save pudquick/1dcedff8107e03588acc119d2fccb15c to your computer and use it in GitHub Desktop.
Split Sublime Text saved .sublime_session into separate text files in the current working directory
import uuid, json
def find_all_keys(key, var):
if hasattr(var,'items'):
for k, v in var.items():
if k == key:
yield v
if isinstance(v, dict):
for result in find_all_keys(key, v):
yield result
elif isinstance(v, list):
for d in v:
for result in find_all_keys(key, d):
yield result
def split_session(file_path):
with open(file_path, 'rt') as f:
s = json.load(f)
buffers = [x for x in find_all_keys('buffers', s)]
docs = []
for x in buffers:
for y in x:
docs.append(y)
contents = []
for x in docs:
if 'contents' in x:
# docs which don't have a 'contents' key are open docs, but not modified, so we don't care about those
contents.append(x['contents'])
for x in contents:
with open(str(uuid.uuid4()) + '.txt', 'wt') as f:
_ = f.write(x)
# split_session('/path/to/your/current.sublime_session')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment