in IDE, Tips & Tricks

Sublime Text 3 Snippet Error

I wanted to create a snippet for ST3 to insert a logging function that I created a while back instead of cut and pasting it every time from previous scripts.

Finding information about creating the snippet is in abundance but I encountered an error that none of them covered.

Everytime I tried to insert the snippet it kept hitting the error below:

<snippet>
	<content><![CDATA[
################################################################
#   LOGGING
################################################################
LOG_FILE=""
touch "$LOG_FILE"
exec 3>&1 1>>${LOG_FILE} 2>&1

# Prints to log and terminal
LogMessage() {
  echo "$(date) \$(hostname): $*" | tee /dev/fd/3
}
# Only prints to terminal
ConsoleMessage() {
  echo "$*" >&3
}
]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<!-- <tabTrigger>log</tabTrigger> -->
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<!-- <scope>source.sh</scope> -->
</snippet>

The error is that all the $ need to be escaped so the snippet looks like this:

<snippet>
	<content><![CDATA[
################################################################
#   LOGGING
################################################################
LOG_FILE=""
touch "\$LOG_FILE"
exec 3>&1 1>>\${LOG_FILE} 2>&1

# Prints to log and terminal
LogMessage() {
  echo "\$(date) \$(hostname): \$*" | tee /dev/fd/3
}
# Only prints to terminal
ConsoleMessage() {
  echo "\$*" >&3
}
]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<!-- <tabTrigger>log</tabTrigger> -->
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<!-- <scope>source.sh</scope> -->
</snippet>

Note: all $ are escaped otherwise the variables will try to be parsed and not just pasted as is.