1

Possible Duplicate:
Properly handling spaces and quotes in bash completion

I would like to be use muti-word quoted strings for bash completion.

e.g. I like to be able to do this

$ command <tab> 
  "Long String 1"
  "Long String 2"

where "Long String 1" and "Long String 2" are the suggestions given when tab is pressed.

I tried using this where ~/strings contains a list of quoted strings

function _hista_comp(){
    local curw
    COMPREPLY=()
    curw=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=($(compgen -W '`cat ~/strings`' -- $curw))    
    return 0
}
complete -F _hista_comp hista

The above function splits the string on whitespace. Is there any way to make it return the whole quoted string?

e.g if ~/string had the following lines

  "Long String 1"
  "Long String 2"  

It would give 5 suggestions instead of 2.

Community
  • 1
  • 1
Bilal Syed Hussain
  • 8,664
  • 11
  • 38
  • 44
  • Looks like a duplicate of http://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion. And the answer seems to be that `compgen` doesn't play nicely with spaces. – Mark Reed May 04 '12 at 02:06

1 Answers1

2

After trying various things I found that adding

    IFS=$'\x0a';

to the start of the function (changes the input separator to new line) makes the function handle spaces correctly.

So the function would be

function _hista_comp(){  
    IFS=$'\x0a';
    local curw
    COMPREPLY=()
    curw=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=($(compgen -W '`cat ~/strings`' -- $curw))    
        uset IFS
    return 0
}
complete -F _hista_comp hista

This would allow

$ command <tab> 
  "Long String 1"
  "Long String 2"

as I wanted.

Bilal Syed Hussain
  • 8,664
  • 11
  • 38
  • 44
  • 1
    Or, more simply, `IFS=$'\n'`. By the way, the function in your answer is identical to the function in your question (I think you forgot to add the `IFS` line - I would make it `local`, too). – Dennis Williamson May 04 '12 at 03:39