0

I use a custom Perl script to clean up left-over Vim swap files after a crash or power outage. The script automatically removes swap files for unchanged files and prints a list of files with unsaved modifications left in the swap file. This works well, except when I'm restoring files with a different encoding.

My environment, terminal and everything, are set up to use UTF-8 as default encoding. One legacy project stores files as Latin-1 (Latin-9 actually). When I restore a file from that project, saving the buffer changes the encoding.

Here's a minimal example to reproduce the behavior:

step 1: create a file in Latin-1 encoding

$ echo égal > latin.txt

$ file -i latin.txt
latin.txt: text/plain; charset=utf-8

$ recode utf8..latin1 latin.txt

$ file -i latin.txt
latin.txt: text/plain; charset=iso-8859-1

step 2: open file in Vim and simulate a crash

$ vim latin.txt

# (in Vim: Ctrl-Z to send to background)

[1]+  Stopped                 /usr/bin/vim latin.txt

$ ls -a
.  ..  .latin.txt.swp  latin.txt

$ jobs -l
[1]+  7294 Stopped                 /usr/bin/vim latin.txt

$ kill -KILL 7294

$
[1]+  Killed                  /usr/bin/vim latin.txt

step 3: recover the file, and save the buffer as "recovered.txt"

$ vim -r .latin.txt.swp

  ,--[in Vim]
  | Using swap file ".latin.txt.swp"
  | Original file "~/tmp/enctest/latin.txt"
  | Recovery completed. Buffer contents equals file contents.
  | You may want to delete the .swp file now.
  |
  | Press ENTER or type command to continue
  | :sav recovered.txt
  | "recovered.txt" [New] 1L, 6C written
  | :q

Result:

$ file -i recovered.txt
recovered.txt: text/plain; charset=utf-8

As you can see, the original is Latin-1, but the file recovered with vim -r and sav[eas] is stored as UTF-8.

This does not happen without the recovery step, i.e. when opening latin.txt and using saveas directly.

How can I get Vim to restore the original encoding as well?


EDIT: values of fileencoding

# When editing the original file:
    set fenc? -> latin1
    setlocal fenc? -> latin1
# During recovery:
    set fenc? -> ""
    setlocal fenc? -> ""
# During recovery, after `sav[eas] recovered.txt`:
    set fenc? -> ""
    setlocal fenc? -> ""
# When opening recovered.txt in a fresh Vim instance:
    set fenc? -> utf-8
    setlocal fenc? -> utf-8

I'm running Vim as vim --noplugin -u /dev/null for this test.

Zilk
  • 8,917
  • 7
  • 36
  • 44
  • This looks like a bug to me; what's `:setlocal fileencoding?` before and after the recovery. It should be `latin1`, but it's presumably `utf-8`, right? If so, please direct this to the [vim_dev mailing list](http://www.vim.org/community.php). – Ingo Karkat May 04 '14 at 18:51
  • @ingo-karkat I've added the values of `fileencoding` to the question. If this turns out not to be a user error, I will post to the vim_dev list. Thank you. – Zilk May 04 '14 at 20:57
  • Thanks! Indeed looks like during recovery, the encoding detection doesn't run. – Ingo Karkat May 05 '14 at 06:17

0 Answers0