Stepan Loginov
Stepan Loginov

Reputation: 1767

Spaces and suffixes in bash complete()

I have two difficulties with compgen shell builtin. I try show it in simple bash _filedir-like (using ls) code:

_myfiledir(){
  path="$cur"
  prefix=`echo /$path | grep -o "[0-9a-Z\-+_/]*/"`
  sufix=`echo /$path | grep "/[0-9a-Z\-+_]*$" | grep -o "[0-9a-Z\-+_]*$"`
  res=`ls -p $prefix`
  COMPREPLY=($( compgen -o nospace -W "$res" -- $sufix ))
}

In this case when cur=="usr/li" then prefix=="/usr/" and sufix=="li"

I have two difficulties. With space and replacement. For example:

$ script usr/li[TAB]

I get:

$ script lib/  <- additional space here

I need:

$ script usr/lib/ <- no space here

This code is only for example.

Upvotes: 0

Views: 623

Answers (1)

Stepan Loginov
Stepan Loginov

Reputation: 1767

-o nospace

This option don't work in compgen context. Use compopt to change options. In your case compopt -o nospace usefull for whitespace issue, and compopt -o filenames for filenames. Be careful with file names, you need add $prefix to $res variable. Option filenames only cut part of full path when displayed to user for autocompletition.

Upvotes: 0

Related Questions