Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add spaces to the end of some Bash autocomplete options, but not to others?

I'm trying to create a Bash completion script for a Java program. A typical invocation of that program might look like this:

$ javaProgram -Dproperty=foo option1 option2

Part of my script will be to suggest the various Java properties that are available for this program (i.e., when the user types -D, the script would suggest, say, property=, allowing the user to then type the value).

I'd like the completion to not insert a space after the equals sign. However, when the user is typing the various options for the program (option1 and option2 in the above example), I'd like the script to do the completion, and insert a space when it is completed.

I'm new to Bash completion scripting, but I'm aware of the nospace option for the complete shell builtin. It doesn't seem to work for the compgen builtin, though, which seems like what I want. I've tried using nospace, and then explicitly including spaces at the end of the appropriate options, but they don't seem to be making it through.

Does anyone know how to get spaces at the end of some options, but no spaces at the end of others?

like image 502
Christopher Maier Avatar asked Feb 26 '10 03:02

Christopher Maier


5 Answers

You can do it by removing spaces with the corresponding option, then add them back manually for the options where you want them, all using the simple -W ("wordlist") completion:

complete -o nospace -W 'Dproperty= "option1 " "option2 "' javaProgram

Autocompletion will now insert a space after option1 and option2, but none after Dproperty=.

like image 78
Benjamin W. Avatar answered Oct 14 '22 13:10

Benjamin W.


The feature in in scp's bash completion mentioned by C4H5As that retains the added space is not the ampersand, that is just sed syntax for "replace with match".

You have to change the IFS so compgen does not split the input on spaces but on newlines or tabs:

local IFS=$'\t\n'
COMPREPLY=( $(compgen -W "$options" -- $cur) )
like image 37
jtaylor Avatar answered Oct 14 '22 13:10

jtaylor


It would be helpful to see the code you have so far.

However, look at the completion code for scp in /etc/bash_completion.d/ssh if you have that. It puts spaces after local filenames but not after hostnames.

like image 24
Dennis Williamson Avatar answered Oct 14 '22 12:10

Dennis Williamson


Dennis is right on the money, in the function you reference with complete -F _your_func, you want something like this (taken from /etc/bash_completion):

    # escape spaces; remove executables, aliases, pipes and sockets;
    # add space at end of file names
    COMPREPLY=( $( ssh -o 'Batchmode yes' $userhost \
               command ls -aF1d "$path*" 2>/dev/null | \
               sed -e "s/[][(){}<>\",:;^&!$&=?\`|\\ ']/\\\\\\\\\\\\&/g" \
               -e 's/[*@|=]$//g' -e 's/[^\/]$/& /g' ) )
    return 0

This particular example uses the -o nospace option but adds the space manually. It's the last -e ('s/[^\/]$/& /g') that adds the space. In this case it's adding only if the last parameter doesn't end in a slash (which would be a directory and have files to auto-complete).

The key difference seems to be adding an ampersand before the space. Try that and see if it works.

like image 1
Mark McDonald Avatar answered Oct 14 '22 11:10

Mark McDonald


As the poster mentioned, it is not possible to pass the -o nospace option to compgen. One possibility is defining two functions where one inserts a call to compopt -o nospace before calling compgen (the other function would not).

Example:

__my_complete_no_space() {
    local IFS=$' \t\n' cur_="${3-$cur}"
    [[ $cur_ == *:* ]] && cur_="${cur#*:}"
    compopt -o nospace
    COMPREPLY=( $(compgen -P "${2-}" -W "$1" -S "${4-}" -- "$cur_") )
}

This is useful when the addition of space (or not) depends on the context of what needs to be completed.

like image 1
Dom Avatar answered Oct 14 '22 12:10

Dom