通过脚本中的vim将文件编码更改为utf-8 [英] Change File Encoding to utf-8 via vim in a script

查看:641
本文介绍了通过脚本中的vim将文件编码更改为utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在我们的服务器从Debian 4更新到5之后就被淘汰了。
我们切换到UTF-8环境,现在我们在浏览器上正确打印文本有问题,因为所有文件都在非utf8编码,如iso-8859-1,ascii等。

i just got knocked down after our server has been updated from Debian 4 to 5. We switched to UTF-8 environment and now we have problems getting the text printed correctly on the browser, because all files are in non-utf8 encodings like iso-8859-1, ascii, etc.

我尝试过许多不同的脚本。

I tried many different scripts.

我试过的第一个是iconv。

The first one i tried is "iconv". That one doesnt work, it changes the content, but the files enconding is still non-utf8.

与enca,encamv,convmv和其他一些工具的安装相同的问题我通过安装通过apt-get。

Same problem with enca, encamv, convmv and some other tools i installed via apt-get.

然后我发现了一个python代码,它使用chardet通用检测器模块来检测文件的编码(工作正常),但使用unicode类或者编解码器类保存为utf-8不工作,没有任何错误。

Then i found a python code, which uses chardet Universal Detector module, to detect encoding of a file (which works fine), but using the unicode class or the codec class to save it as utf-8 doesnt work, without any errors.

我发现将文件及其内容转换为UTF-8 ,是vi。

The only way i found to get the file and its content converted to UTF-8, is vi.

这些是我为一个文件执行的步骤:

These are the steps i do for one file:

vi filename.php
:set bomb
:set fileencoding=utf-8
:wq

就是这样。那一个工作完美。但是如何可以通过脚本运行。
我想编写一个脚本(linux shell),该脚本遍历一个包含所有php文件的目录,然后使用vi使用上面的命令转换它们。
由于我需要启动vi应用程序,我不知道如何做这样的:

Thats it. That one works perfect. But how can get this running via a script. I would like to write a script (linux shell) which traverses a directory taking all php files, then converting them using vi with the commands above. As i need to start the vi app, i do not know how to do something like this:

vi --run -command =':set bomb,:set fileencoding = utf-8'filename.php

希望有人能帮助我。 p>

Hope someone can help me.

推荐答案

这是我知道的从命令行轻松完成这一操作的最简单的方法:

This is the simplest way I know of to do this easily from the command line:

vim +"argdo se bomb | se fileencoding=utf-8 | w" $(find . -type f -name *.php)

或者更好的是,如果文件数量预计相当大:

Or better yet if the number of files is expected to be pretty large:

find . -type f -name *.php | xargs vim +"argdo se bomb | se fileencoding=utf-8 | w"

这篇关于通过脚本中的vim将文件编码更改为utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆