Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I replace a command line argument with tab completion?
    primarykey
    data
    text
    <p>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/</p> <p>Is it possible to replace the completed symbol?</p> <p>Thanks in advance :)</p> <h1>Update:</h1> <p>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.</p> <p>For anyone interested, here's the code (and there could be a much better way to accomplish this):</p> <pre><code># 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 by a slash if [[ $cur =~ ^[0-9]+/? ]]; then dir="../" strpath=$( printf "%${cur%%/*}s" ); dir=" ${strpath// /$dir}" # Is the arg just a number? if [[ $cur =~ ^[0-9]+$ ]]; then COMPREPLY=($(compgen -W "${dir}")) else if [[ $cur =~ /.*$ ]]; then cur="${cur##*/}" fi results=$(for t in `cd $dir &amp;&amp; ls -d */`; do if [[ `echo $t | tr [:upper:] [:lower:]` == "$lower_cur"* ]]; then echo "${t}"; fi done) COMPREPLY=($(compgen -P "$dir" -W "${results}")) fi else # Is the arg a word that we can look for in the PWD results=$(for t in `echo $PWD | tr "/" "\n"`; do if [[ `echo $t | tr [:upper:] [:lower:]` == "$lower_cur"* ]]; then echo "${t}"; fi; done) COMPREPLY=($(compgen -W "${results}")) fi } #Assign the auto-completion function _get for our command get. complete -F _get_up up </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload