Skip to content

Instantly share code, notes, and snippets.

@christiangenco
Created June 3, 2020 21:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiangenco/7f2669a52e5a51590379b0639eca7779 to your computer and use it in GitHub Desktop.
Save christiangenco/7f2669a52e5a51590379b0639eca7779 to your computer and use it in GitHub Desktop.
Sublime Text 3 plugin to toggle and timestamp markdown todo checkboxes
# ~/.config/sublime-text-3/Packages/User/MarkdownCheckboxes.py
import sublime, sublime_plugin
import re
from time import strftime
def now():
return strftime("%Y-%m-%dT%H:%M:%S")
timestampRegex = r"\s*\(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\)"
def toggleCheckbox(line):
if "[ ]" in line:
timestamp = " (" + now() + ")"
return line.replace("[ ]", "[x]") + timestamp
elif "[x]" in line:
unchecked = line.replace("[x]", "[ ]")
undated = re.sub(timestampRegex, "", unchecked)
return undated
else:
return line
class ToggleMarkdownCheckboxCommand(sublime_plugin.TextCommand):
def run(self, edit):
# self.view.insert(edit, 0, "hello world")
for region in self.view.sel():
# just look at lines with a cusor, not a selection
if region.empty():
line = self.view.line(region)
lineContents = self.view.substr(line)
newLineContents = toggleCheckbox(lineContents)
self.view.replace(edit, line, newLineContents)
[
{"keys": ["shift+enter"], "command": "toggle_markdown_checkbox"}
]
@christiangenco
Copy link
Author

On a user-defined shortkey (I like shift+enter), toggle and timestamp a markdown checkbox between:

- [ ] buy coconut milk

and:

- [x] buy coconut milk (2020-06-03T16:40:47)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment