Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Sublime Text 2 Python plugin to "type" the file that's currently being edited, character by character, for screencasts.

View TypeFileOut.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import sublime, sublime_plugin
 
BLOCKLEN = 4
 
class TypeFileOutCommand(sublime_plugin.TextCommand):
def nextchar(self):
if self.body:
totype = []
while 1:
try:
ch = self.body.pop(0)
except IndexError:
totype.append(ch)
break
totype.append(ch)
if ch in ["\n", " "] or len(totype) > BLOCKLEN:
break
self.view.insert(self.edit, self.view.sel()[0].begin(), "".join(totype))
timeout = 10
if "\n" in totype:
timeout = 250
elif " " in totype:
timeout = 80
sublime.set_timeout(self.nextchar, timeout)
 
def run(self, edit):
self.edit = edit
# First, read everything in this view
reverything = sublime.Region(0, self.view.size())
self.body = list(self.view.substr(reverything))
 
self.view.erase(edit, reverything)
 
sublime.set_timeout(self.nextchar, 2000)

Excellent idea!

...but I get the following error in ST3:

Traceback (most recent call last):
File "/Users/dutton/Library/Application Support/Sublime Text 3/Packages/User/TypeFileOut.py", line 18, in nextchar
self.view.insert(self.edit, self.view.sel()[0].begin(), "".join(totype))
File "/Applications/Sublime Text.app/Contents/MacOS/sublime.py", line 645, in insert
raise ValueError("Edit objects may not be used after the TextCommand's run method has returned")
ValueError: Edit objects may not be used after the TextCommand's run method has returned
Writing file /Users/dutton/Desktop/foo with encoding UTF-8 (atomic)

You can replace self.view.insert(...) command with

self.view.run_command("insert", {"characters": "".join(totype)})

The problem is all running indents get multiplicated. I have only started evaluating SublimeText so cannot fix that at the moment.

Thanks (one year later!)

I managed to fix the indent problem by adding "auto_indent": false in my preferences file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.