Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ziz/7812576 to your computer and use it in GitHub Desktop.
Save ziz/7812576 to your computer and use it in GitHub Desktop.
TextMate 2 bundle command to strip trailing whitespace from (only) modified lines on save. Released under the MIT license <http://opensource.org/licenses/MIT> - caveat hacker.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>command</key>
<string>#!/usr/bin/env bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] &amp;&amp; . "${TM_SUPPORT_PATH}/lib/bash_init.sh"
if [[ ! -e "$TM_FILEPATH" ]]; then
sed -E 's/[[:space:]]*$//'
exit
fi
TMPFILE=$(mktemp /tmp/stwdiff.XXXXXX)
if [ $? -ne 0 ]; then
cat
exit
fi
SEDCOMMANDS=$(mktemp /tmp/stwdiff.XXXXXX)
if [ $? -ne 0 ]; then
rm -f "$TMPFILE"
cat
exit
fi
cat &gt; "$TMPFILE"
diff \
--old-line-format='' \
--new-line-format='' \
--old-group-format='' \
--new-group-format='' \
--unchanged-group-format='' \
--changed-group-format='%dF,%dLs/[[:space:]]*$//
' \
"$TM_FILEPATH" "$TMPFILE" &gt; "$SEDCOMMANDS"
if [ $? -eq 1 ]; then
sed -E -f "$SEDCOMMANDS" "$TMPFILE"
else
cat "$TMPFILE"
fi
rm -f "$TMPFILE" "$SEDCOMMANDS"</string>
<key>input</key>
<string>document</string>
<key>name</key>
<string>Strip Trailing Whitespace (Modified Lines) on Save</string>
<key>outputCaret</key>
<string>interpolateByLine</string>
<key>outputLocation</key>
<string>replaceDocument</string>
<key>scope</key>
<string>source</string>
<key>semanticClass</key>
<string>callback.document.will-save</string>
<key>uuid</key>
<string>3A6AB59D-1CA1-4CED-B1E4-B68A464BFA6E</string>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment