惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
月光博客
月光博客
博客园 - 司徒正美
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
量子位
Recent Announcements
Recent Announcements
V
V2EX
P
Proofpoint News Feed
小众软件
小众软件
云风的 BLOG
云风的 BLOG
腾讯CDC
宝玉的分享
宝玉的分享
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
B
Blog
博客园_首页
GbyAI
GbyAI
博客园 - Franky

博客园 - 立体风

autohotkey2.0 Send keys参数分析 autohotkey2.0 盲从模式 windows 10 下 vscode 编写编译一个 nana c++ 库的示例程序 vscode 插件 cmake-tools 输出内容解读 Claude Code 处理中文乱码问题 python 启动器命令 py 答惑 windows 下 python 3.11 的版本问题 pip 安装 老版本 pytorch 的一个容易迷惑的错误 sentence transformer 例子及说明 Sentence Transformers 介绍 Jina Reranker 替代方案:改用 ModelScope 模型 Webnovel Writer 6.2.1 项目分析 windows 10 LTSC 版 安装 Terminal autohotkey2.0 脚本运行机制 windows wsl2 安装 gentoo 的步骤 windows 10 LTSC版 打开 wsl2 autohotkey 提示词 编译安装最新版 perl rust 做到“快并且安全” 的逻辑思路 rust 和 go 语言的核心目的 throughput 和 efficient 词义 python 以字符数量分割文档 (三) python 以字符数量分割文档 (二) python 以字符数量分割文档 (一) GNU Bash 参考手册(中文版)(六) GNU Bash 参考手册(中文版)(五) GNU Bash 参考手册(中文版)(四) GNU Bash 参考手册(中文版)(三) GNU Bash 参考手册(中文版)(二) GNU Bash 参考手册(中文版)(一)
用 vim 查看文件的格式和编码
立体风 · 2026-08-09 · via 博客园 - 立体风

在 Vim 中查看 .ps1 文件的 换行格式(CRLF/LF)编码(UTF-8/BOM),主要使用以下几个命令。


1. 查看换行格式(CRLF / LF)

打开文件:

vim install.ps1

进入 Vim 后:

:set fileformat?

输出:

Windows 格式:

fileformat=dos

表示:

CRLF (\r\n)

Unix 格式:

fileformat=unix

表示:

LF (\n)

Mac 老格式:

fileformat=mac

表示:

CR (\r)

现在基本不用。


2. 查看文件编码

Vim:

:set fileencoding?

例如:

fileencoding=utf-8

表示:

UTF-8

如果输出:

fileencoding=

表示 Vim 没有检测到特殊编码,通常使用:

utf-8

或者 Vim 默认编码。

查看 Vim 编码默认设置:

:set encoding?

例如:

encoding=utf-8

3. 查看是否有 BOM

BOM(Byte Order Mark):

UTF-8 BOM:

EF BB BF

Vim 查看:

:set bomb?

结果:

有 BOM:

bomb

表示:

UTF-8 with BOM

无 BOM:

nobomb

表示:

UTF-8 without BOM

4. 一次查看全部信息

推荐:

:set fileformat? fileencoding? bomb?

例如:

推荐状态:

fileformat=dos
fileencoding=utf-8
nobomb

对应:

换行:
CRLF

编码:
UTF-8

BOM:
无

这就是 Windows PowerShell 脚本最佳格式。


5. 查看不可见字符(最直观)

Vim:

:set list

例如:

CRLF:

Write-Host "hello"^M$

其中:

^M$

表示:

\r\n

LF:

Write-Host "hello"$

只有:

$

表示:

\n

关闭:

:set nolist

6. 在 Vim 中转换格式

LF → CRLF

执行:

:set fileformat=dos
:w

CRLF → LF

执行:

:set fileformat=unix
:w

7. 设置 UTF-8 无 BOM

转换:

:set encoding=utf-8
:set fileencoding=utf-8
:set nobomb
:w

8. 推荐检查 Claude Code 的 install.ps1

打开:

vim install.ps1

执行:

:set fileformat?
:set fileencoding?
:set bomb?

理想结果:

fileformat=dos
fileencoding=utf-8
nobomb

即:

✔ Windows CRLF
✔ UTF-8
✔ 无 BOM
✔ PowerShell 兼容性最佳

补充:如果你是在 Windows Git Bash 里的 Vim 查看,Vim 仍然可能显示 fileformat=unix,因为 Git Bash 默认偏向 Unix 行尾;这不代表文件一定不能运行,PowerShell 7 基本可以兼容。对于 Windows PowerShell 5.1 + 安装脚本,转换成 dos 更稳。