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.