Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent vim from opening binary files accidentally?

I frequently accidentally open a binary executable, i.e. "foo", when I mean to open the associated source code "foo.cpp". The root of the problem is that tab completion, i.e. :e fo<tab> typically lands on the binary instead of the source code.

Is there a way to get vim to only tab complete names of text files? Or alternatively, change the tab completion order?

Sometimes my hasty tab completion error happens outside of vim; for those cases, what is the best way to prevent vim from opening files that are not text?

like image 962
Andrew Wagner Avatar asked Apr 26 '11 16:04

Andrew Wagner


3 Answers

Not exactly what you need, but I have something like this in my .vimrc

" ignore these files when completing names and in Ex
set wildignore=.svn,CVS,.git,*.o,*.a,*.class,*.mo,*.la,*.so,*.obj,*.swp,*.jpg,*.png,*.xpm,*.gif,*.pdf,*.bak,*.beam
" set of file name suffixes that will be given a lower priority when it comes to matching wildcards
set suffixes+=.old
like image 65
taro Avatar answered Oct 23 '22 05:10

taro


For tab completion outside of vim, that will depend on your shell. Most shells have some form of autocompletion support. In particular, Zsh has the ability to autocomplete e.g. remote hosts for ssh. I'm not a wizard with these things, but it would probably be relatively simple to get your shell to drop files with certain suffixes from the autocompletion list when the command you are typing starts with "vim".

A quick google search turn up this page, which has this:

# Filename suffixes to ignore during completion (except after rm command)
zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.o' '*?.c~' \
'*?.old' '*?.pro'

It should not be too difficult to modify this logic to get what you want (if you use Zsh).

like image 40
chisophugis Avatar answered Oct 23 '22 04:10

chisophugis


Maybe you can find this useful:

set wildmenu
set wildmode=longest,list

(taken and using from How do I make vim do normal (bash like) tab completion for file names?)

like image 1
uzsolt Avatar answered Oct 23 '22 04:10

uzsolt