Skip to content

Instantly share code, notes, and snippets.

@willbeaufoy
Last active August 29, 2015 14:09
Show Gist options
  • Save willbeaufoy/70412ca6b6bc8070255e to your computer and use it in GitHub Desktop.
Save willbeaufoy/70412ca6b6bc8070255e to your computer and use it in GitHub Desktop.
Format a file so that no line can exceed 72 characters (for git commit messages)
# Outputs to shell. Could be updated to change file in place. Explanation here:
# https://stackoverflow.com/questions/26991777/regex-sed-how-to-match-the-last-space-character-in-a-line-before-a-72-charac?answertab=active#tab-top
sed 'y/ /³/
s/.*/\
&³/
:space
s/\(.*\n\)\([^³]\{0,72\}\)³/\1\2 /
t space
s/\(.*\) \([^³]*³\)/\1\
\2/
t space
s/.\(.*\)./\1/
' file.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment