Ignore bash file extension for vim

I wrote a little bash function to ensure completion for vim. The function is as follows:

# 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 tried to ignore files with extensions pdf, dvi, etc., defining the variable _VIM_IGNORE and setting FIGNORE, but this does not work.

Any idea how to do this?

Thanks.

+4
source share
2 answers

I did not find any documentation, but based on my experiments, it seems that FIGNORE does not affect the processing of compgen() / _filedir() (which is just a wrapper around the first). This only affects completion when it is installed in the shell from which completion is launched (but then globally, which you don't need).

I think you cannot use FIGNORE this smart way and must explicitly implement the COMPREPLY array COMPREPLY yourself.

+2
source

Thanks to Ingo's suggestion, 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 removes it from the array.

+1
source

Source: https://habr.com/ru/post/1444693/


All Articles