8

Problem: I have .glabels generated files, which are gzipped xml. When I rename file to have suffix .xml.gz, then VIM properly recognizes file and it's editable, human readable xml. After edit I have to rename back to original name.

What I try to open e.g. x.glabels file without renaming, what happens is that I see binary data. It does not ungzipped it.

How to force Vim from command like to treat given file like .gz ?

If possible please make answer more general, to address general problem : how to make Vim , treat file .extension1 as file .another_extension ?

Use case: http://glabels.org/ uses gzipped xml, so instead of .glabels extension, they could use .xml.gz.

When I rename file by adding .txt.gz or .xml.gz extension, VIM properly picks it up. However with .glabels extension it treats files like binary.

Question : How to force VIM from commandline to open given file (e.g. "x.glabels") like another type (e.g. "x.xml.gz") ?

No Answers: I know I can make symlink or temporarily rename file. I wonder about VIM flags.

Bonus Addition to answer: You are welcome to include in your answer nice way of configuring VIM to treat e.g. .glabels as .xml.gz, but this won't be accepted as full answer, as I look for onliner that I can use on any machine I will have to deal with. Still, information about config might be helpful for other readers (or my main machines;) ).

5
  • Are you referring to how Vim parses the file contents? What flags vim set? filetype? Please refine what you mean by "open as another file type"
    – Wolfie
    Commented Oct 12, 2016 at 16:14
  • @Wolfie I've provided "Problem" paragraph, that I hope clarifies use-case. Question lost not on being concise, but I hope use case is much more clear. If possible, I will be thankful for suggestions of making it more concise. Commented Oct 12, 2016 at 16:21
  • 1
    vim -c set filetype=xml.gz [filename] when starting vim?
    – Wolfie
    Commented Oct 12, 2016 at 16:30
  • :h zip-extension Commented Oct 12, 2016 at 17:13
  • If you want both a "one liner" that works on multiple machines, and a generic answer that treats A file as a B file (where A and B can be any types! no less), sorry, your request is too "tight" (you want something fast and short for something generic). Otherwise, the above answers and reading :h new-filetype-scripts would be enough :P
    – VanLaser
    Commented Oct 12, 2016 at 20:46

1 Answer 1

5

There are two answers to your question:

General

In order to open a file, overriding the default filetype detection, you can just specify a :setf[iletype] command as part of :edit. For example:

:edit +setf\ java strange.cpp

The default filetype detection does not change an existing filetype, that's how this works. Of course, you can also do this retroactively via :setl filetype=...

Extending the gzip plugin

As a binary format, Vim cannot directly edit [GZIP] archives. The built-in :help gzip plugin hooks into the filetype detection in order to transparently handle archives. This is done via :autocmds. If you want to support other file extensions, the default ones (cp. :autocmd gzip) need to be extended with the following:

autocmd! BufReadPre *.glabels setlocal binary
autocmd! BufRead *.glabels call gzip#read("gzip -S.glabels -dn")

Note the -S.glabels addition; without this, gzip wouldn't know what to do with the file.

1
  • The oneliner, of course, being echo -e "autocmd! BufReadPre *.glabels setlocal binary\nautocmd! BufRead *.glabels call gzip#read(\"gzip -S.glabels -dn)\" >> .temp & vim -S .temp & rm .temp, since he asked for a oneliner. Portable!
    – Wolfie
    Commented Oct 13, 2016 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.