I'm wondering if this is possible in Bash, but I'd like to use tab completion to completely replace the current argument that's being expanded. I'll give an example: I'd like to have a function that moves up an arbitrary number of levels in the tree, so I can call up 2 And that would cd me 2 directories up. However, I would like to make it so that if at the number 2, I press tab, it will expand that number to being the path (either relative or absolute, either is fine). I have this almost working using the complete builtin except it will only append the text, so it will be something like up 2/Volumes/Dev/
Is it possible to replace the completed symbol?
Thanks in advance :)
Update:
So a big thanks to chepner, because actually checking my code revealed where my bug was. I was comparing against the wrong var, and the debug code I had was causing the value to not replace.
For anyone interested, here's the code (and there could be a much better way to accomplish this):
# Move up N levels of the directory tree
# Or by typing in some dir in the PWD
# eg. Assuming your PWD is "/Volumes/Users/natecavanaugh/Documents/stuff"
# `up 2` moves up 2 directories to "/Volumes/Users/natecavanaugh"
# `up 2/` and pressing tab will autocomplete the dirs in "/Volumes/Users/natecavanaugh"
# `up Users` navigate to "/Volumes/Users"
# `up us` and pressing tab will autocomplete to "/Volumes/Users"
function up {
dir="../"
if [ -n "$1" ]; then
if [[ $1 =~ ^[0-9]+$ ]]; then
strpath=$( printf "%${1}s" );
dir=" ${strpath// /$dir}"
else
dir=${PWD%/$1/*}/$1
fi
fi
cd $dir
}
function _get_up {
local cur
local dir
local results
COMPREPLY=()
#Variable to hold the current word
cur="${COMP_WORDS[COMP_CWORD]}"
local lower_cur=`echo ${cur##*/} | tr [:upper:] [:lower:]`
# Is the arg a number or number followed b