Skip to content

Instantly share code, notes, and snippets.

@judy2k
Last active July 28, 2025 14:04
Show Gist options
  • Save judy2k/bc359353d0469f0401d677a08e63903b to your computer and use it in GitHub Desktop.
Save judy2k/bc359353d0469f0401d677a08e63903b to your computer and use it in GitHub Desktop.
Fish abbreviations to build, run, and test
# Configure the abbreviations b, build, r, run, t, test.
#
# Written and placed in the public domain, by Mark Smith <[email protected]>
function project_file \
-d "Find a file in parent directories"
# Find a file in the specified directory (or PWD), or any parent directory.
#
# Usage:
# project_file [-p START_DIRECTORY] [-v] FILENAME
#
# `project_file FILENAME` will look for 'FILENAME' in the PWD or any parent directory.
# `project_file -p START_DIRECTORY FILENAME` will look for FILENAME in START_DIRECTORY,
# and then all parents of START_DIRECTORY.
#
# If -v is provided, project_file will print the path of the found file.
# The exit code is 0 if a file was found and 1 otherwise.
argparse v/verbose 'p/path=' -- $argv
or return
set filename $argv[1]
if test -z $filename
echo "project_file: missing FILENAME" >&2
return -2
end
if set -ql _flag_p # Use the -p value, if provided
set p $(path resolve "$_flag_p")
else
# Default to the working directory
set p "$PWD"
end
for i in (seq 1 64) # Avoiding an infinite loop
if test $p = /
# File wasn't found in any parent directory
return 1
else if test -e "$p/$filename"
# File found. Return
if set -ql _flag_v
echo "$p/$filename"
end
return 0
else
# File not found here. Check in parent.
set p $(path dirname $p)
end
end
print "project_file: possible infinite loop. aborted" >&2
return -1
end
function _expand_build \
-d "Expand the 'build' abbreviation into a project-specific build command."
if project_file Justfile
echo just build
else if project_file Makefile
echo make build
else if project_file Cargo.toml
echo cargo build
else if project_file package.json
echo npm run build
else if project_file .venv
echo uv build
else
# Do not expand:
echo "_expand_build: You don't appear to be in a project dir" >&2
return 1
end
end
function _expand_run \
-d "Expand the 'run' abbreviation into a project-specific run command."
if project_file Justfile
echo just run
else if project_file Makefile
echo make run
else if project_file Cargo.toml
echo cargo run
else if project_file package.json
echo npm run dev
else if project_file .venv
echo uv run # This command will require you to type the name of the script you want to run.
else
# Do not expand:
echo "_expand_run: You don't appear to be in a project dir" >&2
return 1
end
end
function _expand_test \
-d "Expand the 'test' abbreviation into a project-specific test command."
if project_file Justfile
echo just test
else if project_file Makefile
echo make test
else if project_file Cargo.toml
# I use nextest, but you could change this to `cargo test`
echo cargo nextest run
else if project_file package.json
echo npm run test
else if project_file .venv
echo uv run pytest
else
# Do not expand:
echo "_expand_test: You don't appear to be in a project dir" >&2
return 1
end
end
abbr --add build --function _expand_build
abbr --add b --function _expand_build
abbr --add run --function _expand_run
abbr --add r --function _expand_run
abbr --add test --function _expand_test
abbr --add t --function _expand_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment