简体   繁体   English

在vim的bash完成中忽略文件扩展名

[英]Ignore file extension in bash completion for vim

I wrote a small bash function to provide completion for vim. 我写了一个小的bash函数来为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. 我试图让它忽略扩展名为pdf,dvi等的文件,方法是定义_VIM_IGNORE变量并将FIGNORE设置为,但这不起作用。

Any idea how to accomplish this? 知道怎么做到这一点?

Thanks. 谢谢。

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. 我没有找到任何文档,但根据我的实验,似乎FIGNORE不影响compgen() / _filedir() (它只是前者的包装)处理本身。 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). 它只会在触发完成的shell中设置完成时影响完成(但是全局,这不是你想要的)。

I guess you cannot use FIGNORE in this clever way, and have to explicitly implement a filter of the COMPREPLY array yourself. 我想你不能以这种聪明的方式使用FIGNORE ,并且必须自己明确地实现COMPREPLY数组的过滤器。

Thank to the suggestion by Ingo, this is the solution I have: 感谢Ingo的建议,这是我的解决方案:

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. 如果文件以其中一个被忽略的扩展名结尾,则将其从数组中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM