Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top