Skip to content

Instantly share code, notes, and snippets.

@okurka12
Created July 12, 2024 02:03
Show Gist options
  • Save okurka12/e4470d60999123db07fec3fb30131510 to your computer and use it in GitHub Desktop.
Save okurka12/e4470d60999123db07fec3fb30131510 to your computer and use it in GitHub Desktop.
#
# for fish shell:
# .. put it into ~/.config/fish/config.fish
# .. tested with fish 3.7.1
#
# cd's into a directory and creates it if it doesn't
# exist yet
#
# USAGE: cdd DIR
#
function cdd
# check that DIR was supplied
if test $(count $argv) = 0
echo "no argument for cdd command" >&2
echo "USAGE: cdd DIR" >&2
return 1
end
# check that only one argument was supplied
if test $(count $argv) -gt 1
echo "too many arguments for cdd command" >&2
echo "USAGE: cdd DIR" >&2
return 2
end
# check if DIR exists, if not, create it
if test ! -e $argv[1]
mkdir -p -- $argv[1]
# DIR exists, check if its a directory
else
if test ! -d $argv[1]
echo "$argv[1] exists and is not a directory" >&2
return 3
end
end
# DIR directory should exist by now
if test ! -d $argv[1]
echo "Could not create $argv[1]" >&2
return 4
end
cd -- $argv[1]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment