{ "PATH (export)": { "body": "export PATH=/usr/local/lib:/usr/local/bin:/opt/homebrew/bin/:\\$PATH", "description": "Extends PATH in a way that homebrew installed CLIs are recognized in ARM and Intel Macs.", "prefix": "PATH (export)" }, "awk (get field)": { "body": "awk '{ print $${1:1} }'", "description": "Retrieve a field from the piped in string, with whitespace as the default field delimiter. `$n` means that the nth field will be used.", "prefix": "awk (field)" }, "check device name": { "body": "scutil --get ComputerName | grep -q \"$0\"", "prefix": "device name" }, "check if dark mode (macOS)": { "body": "defaults read -g AppleInterfaceStyle &>/dev/null; && echo 'isDark'", "prefix": "check if dark mode (macOS)" }, "check if in git repo": { "body": "if ! git rev-parse --is-inside-work-tree &>/dev/null ; then print \"\\e[0;33mfile is not ins a git repository.\\e[0m\" && return 1 ; fi", "prefix": "check if in git repo" }, "check if installed": { "body": [ "if [[ ! -x \"\\$(command -v ${1:cli})\" ]]; then print \"\\e[0;33m${1:cli} not installed.\\e[0m\" && return 1; fi", "$0" ], "description": "https://stackoverflow.com/a/26759734/22114136", "prefix": "check if installed" }, "check if on macOS": { "body": "[[ \"\\$OSTYPE\" =~ \"darwin\" ]]", "prefix": "check if on macOS" }, "check if process is running": { "body": "pgrep -xq \"${1:process}\" && $0", "prefix": "check if process is running" }, "check if sudo user": { "body": "sudo -nv && ${1:some_sudo_action}", "prefix": "check if sudo user" }, "confirmation prompt": { "body": [ "# confirmation prompt", "print \"\\e[1;34m$0Proceed? (y/n)\\e[0m\"", "read -rk pressed", "echo", "if [[ \"\\$pressed\" != \"y\" ]]; then", "\techo \"Aborted.\"", "\treturn 1", "fi", "echo" ], "prefix": "confirmation prompt" }, "default value": { "body": "\\${${1:var}:-${2:default_value}}", "prefix": "default value" }, "directory of script": { "body": "\"$(dirname \"$(readlink -f \"\\$0\")\")\"", "prefix": "directory of this script" }, "elseif": { "body": "elif [[ ${1:condition} ]]; then\n\t${0}", "description": "Add an elseif to an if statement.", "prefix": "elseif" }, "extension": { "body": "ext=${${1:file_name}##*.}", "prefix": "get extension" }, "filename": { "body": "file_name=$(basename \"$${1:file_path}\")", "prefix": "filename" }, "filename w/o ext": { "body": "${1:file_name}=${${1:file_name}%.*}", "prefix": "filename w/o ext" }, "find & xargs": { "body": "find . -print0 | xargs -0 -I '{}'", "prefix": "find & xargs" }, "find -exec": { "body": "find . $0 -exec open '{}' \\;", "prefix": "find -exec" }, "for each file": { "body": [ "for filename in *.txt; do", "\techo \"\\$filename\"", "done" ], "prefix": "for each file" }, "for each line (read)": { "body": [ "while read -r line; do", "\techo \"\\$line\"$0", "done < \"\\$${1:input_file}\"" ], "prefix": "for each line (read)" }, "for each line (variable)": { "body": [ "echo \"\\$lines\" | while read -r line; do", "\techo \"\\$line\"", "done" ], "prefix": "for each line (variable)" }, "for i ..": { "body": [ "for ((i = 0; i <= ${1:length}; i++)); do", " echo \\$i$0", "done" ], "prefix": "for i .." }, "function": { "body": "function ${1:name} {\n\t${0}\n}", "prefix": "function" }, "get nth line from string": { "body": "line=$(echo \"\\$${1:str}\" | sed -n \"${2:n}p\")", "prefix": "get nth line from string" }, "i++": { "body": "((${1:i}++))", "prefix": "i++" }, "i--": { "body": "((${1:i}--))", "prefix": "i--" }, "if (short)": { "body": "[[ \"\\$${1:var}\" ]] && $0", "prefix": "if (short)" }, "if .. then": { "body": [ "if [[ \"\\$${1:var}\" ]]; then", "\t$0", "fi" ], "prefix": "if .. then" }, "if .. then .. else": { "body": [ "if [[ \"$${1:cond}\" ]]; then", "\t$0", "else", "\t", "fi" ], "prefix": "if .. then .. else" }, "input (stdin or $1)": { "body": [ "if [[ $# -eq 0 ]]; then", "\tinput=$(< /dev/stdin)", "else", "\tinput=\"$1\"", "fi" ], "description": "reads either from STDIN or $1. stdin may have unescaped newlines, which have to be removed, e.g. via `tr -d '\n'`.", "prefix": "input (stdin or $1)" }, "notify (msg)": { "body": "osascript -e 'display notification \"\" with title \"${1:msg}\"'", "prefix": "notify (msg)" }, "notify (var)": { "body": "osascript -e \"display notification \\\"\\\" with title \\\"$${1:var}\\\"\"", "prefix": "notify (var)" }, "null (pipe)": { "body": "&> /dev/null", "prefix": "null (pipe)" }, "osascript jxa (run script)": { "body": "osascript -l JavaScript \"${1:file}\"", "prefix": [ "jxa (run script)", "osascript -l JavaScript" ] }, "plist: extract key": { "body": "plutil -extract name.childkey xml1 -o - example.plist | sed -n 4p | cut -d'>' -f2 | cut -d'<' -f1", "prefix": "plist: extract key" }, "print in blue": { "body": "print \"\\e[1;34m$0\\e[0m\"", "prefix": "print in blue" }, "progress bar": { "body": [ "for _ in {1..100}; do", "\tprintf \"\ud83e\udf0b\"", "\tsleep 0.5", "done" ], "prefix": "progress bar" }, "quicklook": { "body": "qlmanage -p \"${1:filepath}\"", "description": "QuickLook the file. MacOS only.", "prefix": "quicklook" }, "redirect to stderr": { "body": ">&2", "prefix": "redirect to stderr" }, "resolve home": { "body": "${1:path}=\"\\${${1:path}/#\\~/\\$HOME}\"", "prefix": "resolve home" }, "restart app": { "body": [ "killall \"${1:app_name}\"", "while pgrep -xq \"${1:app_name}\"; do sleep 0.1; done", "open -a \"${1:app_name}\"" ], "description": "safely restart app, avoiding race condition", "prefix": "restart app" }, "sed substitution": { "body": "sed 's/$0//'", "prefix": "sed substitution" }, "shebang": { "body": "#!/usr/bin/env zsh", "prefix": "shebang" }, "slice": { "body": "${1:var}=\"${${1:var}:${2:from}:${3:to}}\"", "prefix": "slice" }, "spinner": { "body": [ "# spinner with 20s timeout", "spinner=\"\u280b\u2819\u2839\u2838\u283c\u2834\u2826\u2827\u2807\u280f\"", "for i in {1..100}; do", "\tpos=\\$((i % \\${#spinner}))", "\tprintf \"\\r%s\" \"\\${spinner:\\$pos:1}\"", "\tsleep 0.2", "done", "printf \"\\r\" # remove spinner" ], "prefix": "spinner" }, "stderr (merge with stdout)": { "body": "2>&1", "prefix": "stderr (merge with stdout)" }, "stdin": { "body": "stdin=\\$(< /dev/stdin)", "description": "$(cat) also works. See https://stackoverflow.com/questions/32363887/in-a-bash-function-how-do-i-get-stdin-into-a-variable", "prefix": "stdin" }, "stdout is to a terminal": { "body": "[[ -t true ]]", "prefix": "stdout is to a terminal" }, "substitute": { "body": "\\${${1:var}//${2:search}/${3:replace}}", "description": "one slash for single substitution, two for global", "prefix": "substitute (expansion)" }, "suppress stderr": { "body": "2>/dev/null", "prefix": "suppress stderr" }, "switch case": { "body": [ "case $${1:var} in", "\"one\" | \"two\")", "\techo \"foo\"", "\t;;", "\"three\")", "\techo \"bar\"", "\t;;", "*)", "\techo \"default\"", "\t;;", "esac" ], "description": "A case command first expands word, and tries to match it against each pattern in turn.", "prefix": "switch case" }, "ternary": { "body": "\\$([[ \"${1:cond}\" ]] && echo \"${2:value1}\" || echo \"${3:value2}\")", "prefix": "ternary" }, "then": { "body": "then\n\t$0\nfi", "prefix": "then .. fi" }, "today (ISO date) + now": { "body": "${1:now}=\\$(date +\"%Y-%m-%d %H:%M:%S\")", "prefix": [ "today (ISO date)", "now" ] }, "trim whitespace": { "body": "${1:text}=$(echo -n \"$${1:text}\" | sed -e 's/^ *//' -e 's/ *$//')", "prefix": "trim whitespace" }, "urlEncode": { "body": "osascript -l JavaScript -e \"encodeURIComponent('${1:text}')\"", "prefix": "urlEncode" }, "wait until app running": { "body": "while ! pgrep -xq \"${1:app_name}\"; do sleep 0.1; done", "prefix": "wait until app running" }, "wait until app terminated": { "body": "while pgrep -xq \"${1:app_name}\"; do sleep 0.1; done", "prefix": "wait until app terminated" }, "while": { "body": "while [[ ${1:condition} ]]; do\n\t${0}\ndone\n", "description": "A while loop by condition.", "prefix": "while" }, "xargs (for each line)": { "body": "xargs -I {} ${1:some_cmd} '{}'", "prefix": "xargs (for each line)" } }