Question

I wrote a small bash function to provide completion for vim. The function is the following:

# completion for vim
_vim()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    case $prev in
        -h|--help|-v|--version)
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]] ; then
        local _vopts='-v -e -E -s -d -y -R -Z -m -M -b -l -C'
        _vopts="${_vopts} -N -V -D -n -r -r -L -A -H -F -T -u"
        _vopts="${_vopts} --noplugin -p -o -O --cmd -c -S -s -w -W"
        _vopts="${_vopts} -x -X --remote --remote-silent --remote-wait"
        _vopts="${_vopts} --remote-wait-silent --remote-tab --remote-send"
        _vopts="${_vopts} --remote-expr --serverlist --servername"
        _vopts="${_vopts} --startuptime -i -h --help --version"
        COMPREPLY=( $( compgen -W "${_vopts}" \
            -- "$cur" ) )
        return 0
    fi

    local _VIM_IGNORE=".pdf:.dvi:.jpg:.pyc:.exe:.tar:.zip:.ps"
    FIGNORE="${_VIM_IGNORE}" _filedir
} &&
complete -F _vim vim vi v gv vd

I was trying to have it ignore files with extensions pdf, dvi, etc by defining the _VIM_IGNORE variable and setting FIGNORE to that but that does not work.

Any idea how to accomplish this?

Thanks.

Was it helpful?

Solution

I haven't found any documentation, but based on my experiments, it seems that FIGNORE does not affect the compgen() / _filedir() (which is just a wrapper around the former) processing itself. It only affects completions when it is set in the shell from which the completion is triggered (but then globally, which is not what you want).

I guess you cannot use FIGNORE in this clever way, and have to explicitly implement a filter of the COMPREPLY array yourself.

OTHER TIPS

Thank to the suggestion by Ingo, this is the solution I have:

function _vim()
{
  local cur prev idx ext

  COMPREPLY=()
  _get_comp_words_by_ref cur prev

  case $prev in
    -h|--help|-v|--version)
      return 0
      ;;
  esac

  if [[ "$cur" == -* ]] ; then
    local _vopts='-v -e -E -s -d -y -R -Z -m -M -b -l -C'
    _vopts="${_vopts} -N -V -D -n -r -r -L -A -H -F -T -u"
    _vopts="${_vopts} --noplugin -p -o -O --cmd -c -S -s -w -W"
    _vopts="${_vopts} -x -X --remote --remote-silent --remote-wait"
    _vopts="${_vopts} --remote-wait-silent --remote-tab --remote-send"
    _vopts="${_vopts} --remote-expr --serverlist --servername"
    _vopts="${_vopts} --startuptime -i -h --help --version"
    COMPREPLY=( $( compgen -W "${_vopts}" \
      -- "$cur" ) )
    return 0
  fi

  local _VIM_IGNORE=(pdf xdvi jpg pyc exe tar zip ps)
  _filedir
  for idx in ${!COMPREPLY[@]}; do
    ext=${COMPREPLY[$idx]}
    ext=${ext##*.}
    for iext in ${_VIM_IGNORE[@]}; do
      if test "$ext" = "$iext"; then
        unset -v COMPREPLY[$idx]
        break
      fi
    done
  done
  return 0
}

if the file ends in one of the ignored extensions it deletes it from the array.

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