Is there any way to determine within Vim that the file which I'm editing is either in text or binary format?For example:vim http://example.com/vs:vim http://cdn.sstatic.net/vi/img/apple-touch-icon.pngSomething that would return either True or False based on the content of the current file. One possible scenario would be, if the file is in gzip format, I'd like to perform uncompression of it.
How to check if file is binary?
vimscript;binary file
Most programs (such as grep, git, mercurial, etc.) consider a file to be binary if it contains a NULL byte.You can check this with Vim like so:fun! IsBinary()    return !!search('\%u0000', 'wn')endfunNote that this searches the entire buffer; and won't be very fast for large files. A faster way would be to use the file utility:fun! IsBinary2()    return system('file -ib ' . shellescape(expand('%:p'))) !~# '^text/plain'endfunfile has a list known magic patterns it can check against, and will be a lot faster for some files. The downside is that file isn't available by default on some systems (Windows, some Linuxes).It gets the MIME type, so you can do more advanced matching, such as:fun! IsGzip()    return system('file -ib ' . shellescape(expand('%:p'))) =~# '^application/x-gzip'endfunif the file is in gzip format, I'd like to perform uncompression of it.gzip files pretty much always end in .gz; in fact, some gzip flavours won't even work if the file doesn't end in .gz; with GNU gzip:$ gzip z$ mv z.gz aaaaa$ gzip -d aaaaagzip: aaaaa: unknown suffix -- ignoredExit 2So matching the filename with an autocmd would probably be a better solution :-) In fact, there's already an example of this in the help: :help gzip-example ;-)