Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a simple bash_completion code. The only thing it does, is when I type grcat and then Tab, it offers to complete filenames from /etc/grcat.

_grcat() {
    local word
    COMPREPLY=()
    if [ 1 -eq "$COMP_CWORD" ]; then
      pushd /etc/grcat &>/dev/null || return 1
      word="${COMP_WORDS[COMP_CWORD]}"
      COMPREPLY=($(compgen -f "$word"))
      popd &>/dev/null
    fi
} 
complete -F _grcat grcat

I need to modify it so that it lists all files from /etc/grcat, except /etc/grcat/README.

How can I make bash_completion ignore one particular file ?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You can also use -X option with compgen:

_grcat() {
  local word
  COMPREPLY=()
  if [ 1 -eq "$COMP_CWORD" ]; then
    pushd /etc/grcat &>/dev/null || return 1
    word="${COMP_WORDS[COMP_CWORD]}"
    local filterpat='README'
    COMPREPLY=($(compgen -f -X "$filterpat" -- "$word"))
    popd &>/dev/null
  fi
} 
complete -F _grcat grcat

compgen accepts most of same options as complete, except -p and -r.

compgen also generates the result, write the matches to standard output. complete only stores rules for future use.

share|improve this answer
    
that is strange. It still shows README, when I copy and paste your code exactly –  Martin Vegter Aug 12 at 17:31
    
@MartinVegter: Fixed it! Since you have been in /etc/grcat, so we only need README as pattern to ignore. –  cuonglm Aug 12 at 17:42
    
perfect! Works now. –  Martin Vegter Aug 12 at 17:51

From here, I see that the variable that you are looking for is FIGNORE. You can do something like,

export FIGNORE=$FIGNORE:/etc/grcat/README

It seems you could also use Complete command to accomplish what you are trying to do. I see the information regarding the usage of Complete command from here and here.

You can actually try modifying the complete command that you have in your script as something like,

complete -f -X '/etc/grcat/README' grcat

From man page of complete,

-X filterpat filterpat is a pattern as used for filename expansion. It is applied to the list of possible completions generated by the preceding options and arguments, and each
completion matching filterpat is removed from the list. A leading ! in filterpat negates the pattern; in this case, any completion not matching filterpat is removed.

share|improve this answer
    
how do I integrate it in my bash_completion code. Obviously, I don't want to export FIGNORE for all bash. I only want to apply it to the running instance of bash completion. –  Martin Vegter Aug 12 at 15:42
    
@MartinVegter, you can use the X flag to ignore patterns. See my edit. –  Ramesh Aug 12 at 15:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.