...making Linux just a little more fun!

<-- 2c Tips | TAG Index | 1 | 2 | 3 | Knowledge Base | News Bytes -->

The Answer Gang

By Jim Dennis, Karl-Heinz Herrmann, Breen, Chris, and... (meet the Gang) ... the Editors of Linux Gazette... and You!



(?) Opening bz2/Z file in ViM

From Ashwin N

Hi,

I am facing a strange problem. ViM has a plugin that enables users to edit a compressed file just like a normal file. Say you open a file.txt.gz in ViM, it directly shows and allows you to edit the uncompressed test. But, strangely on my system this is working for .gz files but not working for .bz2 files!

The plugin file in question is gzip.vim (on my system it is in /usr/share/vim/vim61/plugin/gzip.vim). The file insides look OK to me, the right commands are being called for .Z, .gz and .bz2 files. But, when I open a text file compressed using bzip2 I get junk in ViM, whereas .gz files open correctly.

Hoping a solution/lead from you guys :)

(!) [Kapil] It works here! What I have is:
Package: vim
Version: 6.1
Debian version: 320+1
You didn't say what version etc. you have!
One possible problem that you may have is that your gzip.vim calls "bunzip2" rather than "bzip2 -d". The former may not exist in some broken installations of "bzip2".

(?) Mine is ViM Version 6.1, from RedHat 8.0.

No, it uses "bzip2 -d". And both "bzip2 -d" and "bunzip2" work at the shell. I even changed "bzip2 -d" to "bunzip2" in the gzip.vim file, but it is still not working :-(

This strange problem is really bugging me. I am lost wrt to the solution for this. Any other things I need to check?

(!) [Jason] The 'gzip.vim' in /usr/share/vim/plugin has last change date as 2003 Apr 06
My version uses the '-d' flag and doesn't rely upon gunzip and bunzip2.
This is just a shot in the dark, but you might want to try list the autocommands in the 'gzip' group in vim, like this:
:au gzip
....which should dump a list that looks something like this:
--- Auto-Commands ---
gzip  BufRead
    *.gz      call s:read("gzip -d")
    *.bz2     call s:read("bzip2 -d")
    *.Z       call s:read("uncompress")
gzip  BufReadPre
    *.gz      setlocal bin
    *.bz2     setlocal bin
    *.Z       setlocal bin
gzip  BufWritePost
    *.gz      call s:write("gzip")
    *.bz2     call s:write("bzip2")
    *.Z       call s:write("compress -f")

(?) This was where I got the clue to the problem.

When I did a ":au gzip" I got -

--- Auto-Commands ---
gzip  BufRead
    *.gz      let ch_save = &ch|set ch=2
              '[,']!gunzip
              set nobin
              let &ch = ch_save|unlet ch_save
              execute ":doautocmd BufReadPost " . expand("%:r")
gzip  BufReadPre
    *.gz      set bin
gzip  BufWritePost
    *.gz      !mv <afile> <afile>:r
              !gzip <afile>:r
gzip  FileAppendPost
    *.gz      !mv <afile> <afile>:r
              !gzip <afile>:r
gzip  FileAppendPre
    *.gz      !gunzip <afile>
              !mv <afile>:r <afile>
gzip  FileReadPost
    *.gz      let ch_save = &ch|set ch=2
              '[,']!gunzip
              set nobin
              let &ch = ch_save|unlet ch_save
              execute ":doautocmd BufReadPost " . expand("%:r")
gzip  FileReadPre
    *.gz      set bin
gzip  FileWritePost
    *.gz      !mv <afile> <afile>:r
              !gzip <afile>:r

(?) All .gz related stuff, nothing to do at all with .bz2 and .Z. At this point, I realized that after the commands in gzip.vim were being loaded, they were being overridden by the above somewhere.

I checked the global vimrc file, which is in /usr/share/vim/vim61/macros and I hit bull's eye. In that file, the gzip command was getting overridden with the stuff shown above. So, I just deleted the gzip autocommands in the global vimrc file and everything is working fine now. All the three supported files (.gz, .Z, ,bz2) are opening properly.

(!) [Thomas] This incident was also reported on the Vim mailing list, but I was too slow on the uptake to mention it at the time.

This page edited and maintained by the Editors of Linux Gazette
HTML script maintained by Heather Stern of Starshine Technical Services, http://www.starshine.org/


Each TAG thread Copyright © its authors, 2004

Published in issue 103 of Linux Gazette June 2004

<-- 2c Tips | TAG Index | 1 | 2 | 3 | Knowledge Base | News Bytes -->
Tux