Welcome back to snippets, where I detail useful small scripts or commands. This snippet is for the fish shell, so to make it available everywhere, you can either add it directly to your config (in ~/.config/fish/config.fish), or put it the file ~/.config/fish/functions/gitignore.fish.
Note: If you do put it in
~/.config/fish/functions, it's important that the file matches the name of the function! Fish doesn't automatically load every file infunctions; instead, it will load the file when you reference the file's name. If the filename and function name don't match, the function will be "missing" until you try to call the filename (which then won't actually be a function).
This snippet is for creating a .gitignore file from a GitHub template,
similar to the snippet for PowerShell I did a while
ago. Give it a name of a language or
framework, and it'll create that template.
$ gitignore Go
.gitignore created from GoOn to the snippet!
function gitignore --description "Fetches a .gitignore template from GitHub"
# Argument parsing / help / validation
argparse 'h/help' -- $argv
or return
set usage "\
Usage: gitignore [-h|--help] TEMPLATE
Fetch a .gitignore template from the GitHub gitignore repository and save it as
.gitignore.
Options:
-h, --help Show this help message.
Example:
# Fetches Go.gitignore and saves to .gitignore
gitignore Go"
if set -ql _flag_h
echo $usage
return 0
end
if test (count $argv) -lt 1
echo "Error: missing template name" >&2
echo $usage >&2
return 2
end
# URL Building
set template $argv[1]
set url "https://raw.githubusercontent.com/github/gitignore/refs/heads/main/$template.gitignore"
# Try get the file using any available downloader
if type -q curl
curl -fsSL $url -o .gitignore
else if type -q wget
wget -qO .gitignore $url
else
echo "Error: neither curl nor wget is available" >&2
return 1
end
# Report success / failure
if test $status -eq 0
echo ".gitignore created from $template"
else
echo "Failed to fetch $url" >&2
return 1
end
end
complete -c gitignore -s h -l help -d "Show help message"
complete -c gitignore -f \
-a "(cat ~/.config/fish/completions/gitignore.txt 2>/dev/null)" \
-d "Gitignore template"Breaking it down; the initial sections (lines 2-26) are doing argument parsing, help documentation, and validation. PowerShell kind of gives you this for "free" built into the syntax, but here we do it with function calls and checks.
Then, use either curl or wget to get the file. If you have another downloader
that you prefer, feel free to plumb it in (type -q returns non-zero if the
command doesn't exist).
Note:
Quick flag explainer
curl -fsSL -o <path>
-f: Fail on non-HTTP success (so we don't save a 404 page)-s: Silent, no progress meter or other output, except...-S: Show errors, so we still see errors-L: Follow redirects (3XX HTTP status codes)-o <path>: Output the content to this locationwget -qO <path>
-q: Quiet, no status output-O: Output the content to this location- wget already treats non-HTTP success as an error and follows redirects,
so no flag needed
Finally, we check if either command reported success or failure, and print /
return. It's important these go directly after the invocation, so that another
command doesn't clobber the $status code.
Outside the function is registering the autocomplete handlers. The first one is
fairly simple (for gitignore, -h or --help will "Show help message"),
but the second one needs more explaining.
Because the command takes a restricted list of templates, we need to signal to
fish what are valid template names. In this case, don't suggest filenames or
paths (using -f), and get the list of acceptable words from the
~/.config/fish/completions/gitignore.txt file, newline delimited. How does
that file get populated? Read on!
Because github fairly regularly updates its gitignore template list, hardcoding a list will eventually go out of date. This snippet will fetch the list of templates for autocompletion:
set -l templates_file ~/.config/fish/completions/gitignore.txt
set -l url https://api.github.com/gitignore/templates
# Make ~/.config/fish/completions
mkdir -p (dirname $templates_file)
# Use either downloader to get the template list, then strip quotes, trailing
# spaces / commas, and empty lines. Then, save to a temporary file.
if type -q curl
curl -fsSL $url | string unescape | string trim --chars=" ,[]" | string match -v "" | sort > $templates_file.tmp
else if type -q wget
wget -qO- $url | string unescape | string trim --chars=" ,[]" | string match -v "" | sort > $templates_file.tmp
else
echo "Error: neither curl nor wget is available" >&2
return 1
end
# If we succeeded, save over the live completion file.
if test $pipestatus[1] -eq 0
mv $templates_file.tmp $templates_file
else
rm -f $templates_file.tmp
echo "Failed to update gitignore templates" >&2
return 1
endIt'll get the list as a JSON array from the github api, perform some manual string editing, then save to a temporary file before overwriting the completions list.
Note: I could've done this "properly" with a JSON parsing program like
jq, but considering this a snippet-for-a-snippet you're unlikely to run very often, decided to avoid dependencies. You can always do this manually by converting the JSON array from the API to a newline delimited list.
We save to a temporary file so if any errors happen, the existing autocomplete list remains untouched.