I've followed several bash-completion threads, usualy none were answered, because of obvious reasons. (of which i'm just aware since a day or a few)
Non-the-less, because i was writing scripts, i never considered it to be possible to add this functionality.
Also, because i though that my 'script idea' for which i wanted bash-completition seemed quite challenging, since it is based upon files within several folders, using the folders as arguments to the actual script, i thought it was too difficult.
I was terribly wrong!
First of all, read into: An Introduction to Programmable Completion
Then look into some existing ones in: /usr/share/bash-completion/completions or /etc/bash_completion.d
Once you understand that it is only checking the current(last) and previous 'argument', comparing them with the first (the application), the achievment seems alot easier.
For this example, you should know, that the 'application' in this case, has these 'arguments':
(The find command lists all items found in directory 'menu', sed replacing 'menu' with 'ds' and every '/' with a space, removing 'default' items, since they will not be shown in tab-completion)
So for the application 'ds' i have these arguments, which are based on folders and files.
NOTE: What in this example is named 'cur' and 'prev', could be named anything else, or would not even need to be set, as they simply represent: $2 (prev) and $3 (cur).
You can check yourself, by simply adding:
Into any existing function within that file, also this helps debuging.
Speaking of which, the easiest way to test new changes, is to source the file providing the bash-completition code.
Anyway, so given above list, and knowing that it only checks $2 (prev) and $3 (cur), it is the simplest to work with a case statement.
The $2 (cur) is used if you are currently writing an argument.
The $3 (prev) is when you are looking for new words.
So we need a case statement for $2 (cur) and for $3 (prev).
This said, we need to decide wether we can use a single list several times, read out the values of a directory, or decide upon even diffrent circumstances and call even another function.
To get started, i decided to set a specific list to some of the entries, and share a single list for multiple entries.
So you have both, and its simpler to handle, as the list applies to them, and i took care that the given words expect the same values, so those lists can be shared.
Anyway, so looking at the application 'ds', we see its first line of arguments would be: prj new ssh add review setup
Looking at the 'pre' argument word 'prj', its values would be: ks git rpm edit list
And so forth...
So one simply makes a list for each of the entry.
Luckily, we can use the 'string mechanism' used here, to use regular bash commands.
So, instead of a list of words inside the quotes after an -W, you could use something like:
And when all this is put together, it looks like this:
(This is an example of my first completion-task, and may not be a perfect one, but simple and working)
Now to test this, all you would need to do is this, in order:
Save the above text as: ./dev-scripts_compl.bash
NOTE:
When testing, you obviously would not get an output as the screenshot shows, as the file created by 'touch' is empty.
But the completition will work, as it depends on the ./dev-scripts_compl.bash, just checking for an executeable named 'ds'.
Hope this helps you to get started,
enjoy your bash-completions
Heyas
For my project TUI i had prepared bash completion.
Bash-completion works, at least if i source that file manualy.
However, when i'm installing it, it wont apply, not even for new opened terminals.
Allthough i had it working once, i dont get why it doesnt work now.
<...>
+ ''
+... (0 Replies)
Heyas
I'm trying to read out a file which contains a variable and use that list to complete a bash command.
Difficult part on this is, that 2 (actualy 3) different lists are to be used, but the 'dynamic' ones from the external file dont work properly.
It only seems to work with the list... (2 Replies)
Hello
I want use bash_completion on a script but the information on the internet give me not a solution.
The scrip start with "cd" to a direction
Than the script do "ls"
Than I must give a name of a sub-direction and here I want to use <TAB>, so when I have a direction with the name
smith... (2 Replies)
Hi,
I'm having a problem with tab completion at the bash command line. For some reason, whenever I type g<tab>, the terminal will freeze up for 5-10 seconds before asking me if I want to display all 325 possibilities. I thought that maybe it's because of the high number of commands, but I have... (4 Replies)
Hi there!
I have this script which handles bookmarks. Bookmarks are basically a string that point to a certain path, e.g., project -> ~/code/projects/project.
Currently I have working completion for the bookmarks:
~ $ m p<Tab>
~ $ m project
What I want to implement now is the cd... (0 Replies)
Hello,
Bash completion is great, but there are some features I'd like to change. One thing is the behaviour when there are lots of very similar possibilities.
E.g., my directory contains 133 files, from pubmed_result1.txt to pubmed_result133.txt
$ ls Lyonprim/p
Display all 133... (2 Replies)
hello,
I have been trying for a couple days to figure this out to no avail. I am converting some csh code to bash. I have converted everything except the completion code.
#bashrc (I set this alias in my bashrc)
alias test='source ${PATH}/test.sh'
#${PATH}/test.sh (returns some aliases and... (0 Replies)
completion-ignore-case option doesn't work in my version:
/home/user $ echo $BASH_VERSION
3.2.48(1)-release
/home/user $ ls -l *
-rw-r--r-- 1 user user 0 2009-10-18 00:09 somefile
-rw-r--r-- 1 user user 0 2009-10-18 00:09 Somefile
/home/user $ set completion-ignore-case on
But when I... (2 Replies)
I really like tcsh's:
set completion='enhanced'
because it treats underscores and dash signs the same. I don't have to reach for the shift key when trying to complete files that have underscores. As far as I know, there is nothing like this in bash.
Does such a thing exist in bash? If... (2 Replies)