I wrote a script called prpsls which creates or edits files inside the directory ~/proposals.
Now I'm writing the autocomplete script for it, and I'm trying to set it up in a way that, after I hit Tab, the complete built-in lists files and directories from the ~/proposals directory, regardless of the current working directory.
The behavior I'm trying to achieve is exactly like if I did complete -f prpsls, but instead of listing files and dirs from the current directory, I want it to always complete to the contents of the ~/proposals directory.
For example, when I write ls and tab, it shows:
$ pwd
/home/user/base_directory
$ ls
file_01 file_02 subdir_01/
If I write subd and Tab, it autocompletes to subdir_01. If I tab again, it show the contents of dir_01:
$ ls subdir_01/
file_03 subdir_02/
The behavior I'm looking for is similar:
$ pwd
/home/user/any/directory/it/should/not/matter
$ prpsls # Tab here
proposal_01 proposal_02 job_01/
$ prpsls job_01/ # Tab again
proposal_03 proposal_draft
My question is: Is it possible to achieve this behavior with the complete built-in? If not, how can I achieve this behavior?
I was hoping I could tell to complete what directory to use as base directory through a flag, but the only flag I see related to directories is -d which is the same as -A directory, which generates a list of directories.
Funnily enough, if I use compgen -f "~/proposals/", it does list files and directories in ~/proposals:
$ pwd
/home/user/any/directory/it/should/not/matter
$ compgen -f "~/proposals/"
~/proposals/job_01
~/proposals/proposal_01
~/proposals/proposal_02
I have seen this question, but:
- The accepted answer does not work for nested directories. (i.e. Tab and see the content of a directory)
- The
CDPATHanswer adds more clutter to other commands. - The
compgenanswer doesn't work for nested directories either.