Fixed issues with new dots() bash function where it dit not handle the depth correctily. Dots in longer inputs are also passed normally and not handled by the function to enable things like nvim ../somefile etc.

This commit is contained in:
Sebastian Lenzlinger 2024-12-10 13:08:50 +01:00
parent d8d3a77113
commit b984c2797f

View File

@ -103,24 +103,38 @@ goto() {
} }
# Easier cd'ing (aliases here because they depend on dots() function being available) # Easier cd'ing (aliases here because they depend on dots() function being available)
# Alias for one level up
#alias ..="command cd .."
dots() { dots() {
# Count number of dots # Extract the input argument
local depth=$(echo -n "$1" | tr -cd '.' | wc -c) local input="$1"
# if no dots, stay in current directory # Ensure input consists only of dots
if [ "depth" -eq 0 ]; then if [[ ! $input =~ ^\.+$ ]]; then
depth=1 echo "Error: 'dots' function accepts only dots (.., ..., ...., etc.)"
return 1
fi fi
for _ in $(seq 1 "$depth"); do # Count the number of dots in the input
cd .. local depth=${#input}
# If depth is 1 (single dot), stay in the current directory
if [ "$depth" -eq 1 ]; then
return 0
fi
# Construct the relative path based on the number of dots
local path=""
for _ in $(seq 1 "$((depth - 1))"); do
path+="../"
done done
# Change directory up the calculated number of levels
cd "$path" || return
} }
# aliases for even easier navigation # Aliases for multiple levels
alias ..='dots ..' alias ..='dots ..'
alias ...='dots ...' alias ...='dots ...'
alias ....='dots ....' alias ....='dots ....'