Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash completion for Maven escapes colon

I added bash completion for Maven following the docs:

http://maven.apache.org/guides/mini/guide-bash-m2-completion.html

Everything works well except for goals that use a colon. For instance, instead of

mvn eclipse:eclipse

completion escapes the colon

mvn eclipse\:eclipse

Any suggestions how this can be fixed? I'm using Ubuntu 8.10 (2.6.27-17-generic) and

$ bash -version
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)

like image 421
armandino Avatar asked May 10 '10 18:05

armandino


2 Answers

From Bash FAQ E13.

Just after the complete command in the script you linked to, issue this command to remove the colon from the list of completion word break characters:

COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
like image 128
Dennis Williamson Avatar answered Oct 03 '22 22:10

Dennis Williamson


Here is a related question and another suggested solution:

How to reset COMP_WORDBREAKS without effecting other completion script?

As stated before, the simplest solution is to alter COMP_WORDBREAKS. However, modifying COMP_WORDBREAKS in your completion script is not safe (as it is a global variable and it has the side effect of affecting the behavior of other completion scripts - for example scp).

Therefore, bash completion offers some helper methods which you can use to achieve your goal in a better and more safer way.

Two helper methods were added in Bash completion 1.2 for this:

  • _get_comp_words_by_ref with the -n EXCLUDE option
    • gets the word-to-complete without considering the characters in EXCLUDE as word breaks
  • __ltrim_colon_completions
    • removes colon containing prefix from COMPREPLY items
      (a workaround for http://tiswww.case.edu/php/chet/bash/FAQ - E13)

So, here is a basic example of how to a handle a colon (:) in completion words:

_mytool()
{
    local cur
    _get_comp_words_by_ref -n : cur

    # my implementation here

    __ltrim_colon_completions "$cur"
}
complete -F _mytool mytool

Using the helper methods also simplifies the completion script and ensures that you get the same behavior on any environment (bash-3 or bash-4).

You can also take a look at man or perl completion scripts in /etc/bash_completion.d to see how they use the above helper methods to solve this problem.

like image 22
Radu Gasler Avatar answered Oct 03 '22 21:10

Radu Gasler