15

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)

armandino
  • 17,625
  • 17
  • 69
  • 81

4 Answers4

16

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//:}
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Thank you! This thing has been bugging me for some time :) – armandino May 10 '10 at 20:29
  • 2
    A warning that you probably shouldn't do that, as it breaks completion functions that rely on the default behaviour (like scp). See https://bugs.launchpad.net/gvfs/+bug/290784 for discussion – gfxmonk Nov 12 '10 at 14:19
  • 1
    Agree to @gfxmonk, this is not appropriate as it breaks completion for other functions – Daniel Da Cunha Nov 22 '11 at 03:13
  • As the guys said, this is not recommended as it will break completion for other tools. Instead, you should use the helper methods provided by the bash completion package for this case - _get_comp_words_by_ref and __ltrim_colon_completions. You can also take a look to other completion scripts in /etc/bash_completion.d to see how they use the above helper methods to easily solve this problem. I tried to describe this better in my below answer. – Radu Gasler Oct 22 '12 at 21:03
  • @RaduGasler: Thanks for the downvote. My answer is based on documentation by the maintainer of Bash (see the link). – Dennis Williamson Oct 22 '12 at 21:17
  • @DennisWilliamson: Yes, I know the link, and based on it your answer was good :) But Bash completion is a different package and has a different maintainer. And the maintainer of Bash completion, knowing about the workaround with COMP_WORDBREAKS and the fact that it affects all other completions created the new helper methods in Bash Completion 1.2 to promote an easier and cleaner way to solve this problem - http://www.hanskalabs.net/posts/bash-completion-1.2/ But this was probably not available when you wrote this answer, so now the answer is a little bit outdated - this is why the downvote. – Radu Gasler Oct 22 '12 at 21:37
15

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

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.

Community
  • 1
  • 1
Radu Gasler
  • 1,006
  • 14
  • 17
  • 1
    Hi, the link you've posted to Bash Completion 1.2 is now dead, and the other link doesn't mention anything about either method. It's not clear to me how you use these methods to change the COMP_WORDS array to ignore colons. Would you mind explaining a bit further? – Lou Nov 28 '20 at 17:51
2

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

Dennis answer is definitely correct.

But for the record, there is a logged issue (MNG-3928) to improve the documentation about the Maven integration with bash. The issue has a script attached which is an improved version of the one currently online and just works. You might want to give it a try.

Personally, I use the Bash Completion script from Ludovic Claude's PPA (the one that is bundled into the maven package from Ubuntu) that I download directly from bazaar (her e is a direct download link to the HEAD revision). It is just awesome.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
1

I'd go with the Maven2 Bash Completion File at willcodeforbeer.com.

  • works great
  • handles colons
  • doesn't muck up global variable COMP_WORDBREAKS (thereby breaking scp, etc)
  • easy to edit and understand

Hope that helps!

Adam Monsen
  • 9,054
  • 6
  • 53
  • 82