Custom directory completion adds spaces

I have the following directory structure:

/home/tichy/xxx/yyy/aaa /home/tichy/xxx/yyy/aab /home/tichy/xxx/yyy/aac 

I would like to type cdw y <TAB> and get cdw yyy / <CURSOR> as a result, so I could add cdw yyy / a <TAB> and get cdw yyy / aa <CURSOR>

The solution I came up with gives me the following:
cdw y <TAB> => cdw yyy <SPACE> <CURSOR>

The following code that I have so far:

 _cdw () { local cur prev dirs COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" COMPREPLY=($(compgen -d -- /home/tichy/xxx/${cur}|perl -pe 's{^/home/tichy/xxx/}{}')) # no difference, a bit more logical: dirs=$(compgen -o nospace -d /home/tichy/xxx/${cur}|perl -pe 's/{^/home/tichy/xxx/}{}') COMPREPLY=($(compgen -d -W ${dir} ${cur}|perl -pe 's{^/home/tichy/xxx/}{}')) return 0 } complete -F _cdw cdw cdw () { cd /home/tichy/xxx/ $@ } 

Any ideas what's wrong? It seems to me that the completion process seems to be completed and no longer expects input.

+5
source share
4 answers

How about something like this:

 COMPREPLY=( $(cdw; compgen -W "$(for d in ${cur}* ${cur}*/*; do [[ -d "$d" ]] && echo $d/; done)" -- ${cur}) ) 

(I'm not sure if you can call your shell function here or not, otherwise you may have to duplicate it a bit.)

It will also save you from hacking perl :-)

+2
source

The simplest solution I have found so far is to create add-ons that look like this:

 COMPREPLY=( $( compgen -W "file1 file2 file3 dir1/ dir2/ dir3/" ) ) 

and add this line before returning

 [[ $COMPREPLY == */ ]] && compopt -o nospace 

This sets the nospace parameter whenever the completion can fill up with a slash, so that the user ends with:

 cmd dir1/<cursorhere> 

instead:

 cmd dir1/ <cursorhere> 

and it does not set the nospace option whenever the completion can be filled to the full file name, so the user ends with:

 cmd file1 <cursorhere> 

instead:

 cmd file1<cursorhere> 
+8
source

If I understand correctly, do you want bash -autocomplete directory name and not have extra space? (This is what I was looking for when I got to this page).

If so, when you register the completion function, use "-o nospace".

 complete -o nospace -F _cdw cdw 

I don't know if nospace works on compgen.

+5
source

termination provides a solution for this without any workarounds: funciton _filedir defined in / etc / bash _completion:

  626 # This function performs file and directory completion. It better than 627 # simply using 'compgen -f', because it honours spaces in filenames. 628 # @param $1 If `-d', complete only on directories. Otherwise filter/pick only 629 # completions with `.$1' and the uppercase version of it as file 630 # extension. 631 # 632 _filedir() 

Then it is enough to indicate the following:

 _cdw () { local cur prev dirs cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" _filedir # add -d at the end to complete only dirs, no files return 0 } 
+1
source

Source: https://habr.com/ru/post/1386295/


All Articles