Relentless Coding

A Developer’s Blog

Open a ZIP File With Any Extension In Vim

What to do when you use Vim to open a zip file, but the file extension is not .zip or one of the other extensions known to Vim’s zip plugin?

Displaying ZIP File Content With Vim

Out of the box, Vim is able to open zip files and display its contents. You can even edit zipped files and save them.

Vim recognizes the following file extensions as containing zipped content:

:echo g:zipPlugin_ext
*.aar,*.apk,*.celzip,*.crtx,*.docm,*.docx,*.dotm,*.dotx,*.ear,*.epub,
*.gcsx,*.glox,*.gqsx,*.ja,*.jar,*.kmz,*.odb,*.odc,*.odf,*.odg,*.odi,
*.odm,*.odp,*.ods,*.odt,*.otc,*.otf,*.otg,*.oth,*.oti,*.otp,*.ots,*.ott,
*.oxt,*.potm,*.potx,*.ppam,*.ppsm,*.ppsx,*.pptm,*.pptx,*.sldx,*.thmx,
*.vdw,*.war,*.wsz,*.xap,*.xlam,*.xlsb,*.xlsm,*.xlsx,*.xltm,*.xltx,*.xpi,
*.zip

However, when we have a zip file that does not end in one of these extensions, we need to instruct Vim to execute the zip plugin regardless.

What You’ll See When The File Extension Is Not Supported

Say, we have a zip file called archive:

$ file --mime archive 
archive: application/zip; charset=binary

Opening it with Vim results in:

PK^C^D
^@^@^@^@^@^LKAZ^@^@^@^@^@^@^@^@^@^@^@^@^E^@^\^@a.txtUT
^@^C7Ú<9d>g7Ú<9d>gux^K^@^A^Dè^C^@^@^Dè^C^@^@PK^C^D
... snip ...

Not what we want.

Getting Vim to Execute the ZIP Plugin Regardless of Extension

Let’s replace the buffer with zip plugin’s file browser:

:enew!
:call zip#Browse("/path/to/archive")

Let’s break these commands down:

  • :enew! creates a new buffer, hiding the previous buffer. The exclamation mark discards any changes made to the original buffer.
  • :call calls a function. The zip#Browse() function takes a path to a zip file and shows the content of the unzipped file.

Now we get the desired result:

" zip.vim version v34
" Browsing zipfile archive
" Select a file with cursor and press ENTER

a.txt
b.txt

Always Opening Your File Extension With the ZIP Plugin

If you always want to open your file extension to be opened with the zip plugin, you can add the extension to this list and put the following in your .vimrc:

let g:zipPlugin_ext ..= ",*.mmm"

..= is the string concatenation operator. See :h expr-...