0%

zsh 历史文件编码问题

我为了清理 bash 和 zsh 的重复历史记录写了个脚本(remove_duplicate_cmds.py),在每次登录的时候(间隔时间超过阈值)都会执行一次,最近每次都会报错,报错信息如下所示,挺烦的,每次我都会手动找出有问题的那一行,然后重新运行,但是这样治标不治本,隔两天又报错,需要想一个彻底解决问题的办法。

1
2
3
4
5
6
7
8
9
10
11
/home/taseikyo/.bash_history 198 -> 198
Traceback (most recent call last):
File "/home/taseikyo/remove_duplicate_cmds.py", line 99, in <module>
main()
File "/home/taseikyo/remove_duplicate_cmds.py", line 95, in main
zsh_history()
File "/home/taseikyo/remove_duplicate_cmds.py", line 76, in zsh_history
for cmd in f:
File "/usr/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 3549: invalid start byte

其实就是 .zsh_history 这个文件的编码问题,就算你设置它为 utf-8 编码,它在写历史记录的时候又改回去了就很蠢,网上找的方法说是设置 LC_ALL 和 LANG 为 en_US.UTF-8,其实没啥用,咋办呢,只能每次出错就手动改编码。。。用 vim 打开,使用命令 set encoding 可以看到它的编码是 latin-1,需要改为 utf-8,set encoding=utf-8 就行了。

话说回来,这也是个指标不治本的方法,除非找到它写历史记录的位置,让其以 utf-8 编码打开。都啥年代了,为啥还不改成 utf-8 编码,别人 bash 的历史记录就是,这就是所谓的有得有失?

要是有更好的 xsh 出现,我直接换,zsh 虽说提示方便,但是很多地方不如 bash,明明有的命令存在,给我报错找不到,服了。

Welcome to my other publishing channels