Skip to content

Instantly share code, notes, and snippets.

@potch
Created January 26, 2012 18:42
Show Gist options
  • Save potch/1684290 to your computer and use it in GitHub Desktop.
Save potch/1684290 to your computer and use it in GitHub Desktop.
TextMate command to smart-wrap a line to 78 chars, accounting for indent.
<!-- before -->
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<!-- after -->
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
</p>
#!/usr/local/bin/python
import os
import re
line = os.environ['TM_CURRENT_LINE']
line_length = len(line)
p = re.compile('^\s*')
indent = p.match(line).group()
indent = indent.expandtabs(2)
indent_size = len(indent)
line = line.lstrip()
chunk_size = 78 - indent_size
out = []
i = 0
while len(line.strip()) and i < 100:
idx = line.rfind(' ', 0, chunk_size)
idx2 = line.find(' ', chunk_size)
if idx < 0 or len(line) < chunk_size:
if idx2 < 0:
out.append(indent + line)
break
out.append(indent + line[0:idx2])
line = line[idx2+1:]
else:
out.append(indent + line[0:idx])
line = line[idx+1:]
i = i + 1
print ('\n'.join(out))
@bj-mcduck
Copy link

Do we have to do anything else to install this?
Restart textmate, only works on certain OSs or anything?

Adjust any settings afterward in Textmate?
I just created the file and I notice no changes to the way it soft wraps.

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