vim E213: Cannot convert (add ! to write without conversion)

本文解答了 Vim 6.2 用户在使用 UTF-8 编码时遇到的问题,并提供了如何正确设置文件编码及 BOM 的方法。通过调整 .vimrc 文件中的配置,可以解决文件编码不被识别的情况。
Subject:Re: vim 6.2 - encodings
From:Antoine J. Mechelynck (anto@belgacom.net)
Date:May 28, 2004 11:37:56 am
List:com.googlegroups.vim_use

ji@altern.org wrote:

Hi there,

I'm playing around with encodings in gvim62 (on winxp). Based on thescript set_utf8.vim by Antoine athttp://vim.sourceforge.net/scripts/script.php?script_id=789, I'veadded the following lines to my _vimrc :

if &termencoding == ""

 let &termencoding = &encoding

endif

if exists("+printencoding") && (&printencoding == "")

  let &printencoding = &encoding

endif

set fileencodings-=ucs-bom

set fileencodings-=utf-8

if (&fileencodings == "") && (&encoding != "utf-8")

 let &fileencodings = &encoding

endif

set fileencodings^=ucs-bom,utf-8

set encoding=utf-8

setglobal fileencoding=utf-8

setglobal bomb

Here's my problem :

o create a new file (which by default is utf-8 encoded)

o close this fileo reopen the file (fileencoding is recognised as utf-8)

o :set fenc=ucs-bom

o :w

o I got error E213: Cannot convert (add ! to write withoutconversion)

What am I doing wrong ?

Thanks in advance.

Cheers /jerome

1) "ucs-bom" is not a real encoding. it is not valid in 'fileencoding'(singular), but it is valid in 'fileencodings' (plural), to tell (g)vim torecognise all Unicode encodings when a "byte-order-mark" is present at thebeginning. (The byte-order-mark is code point U+FEFF "zero-width no-breakspace", which is represented by a different byte sequence in each of thevarious Unicode encodings, and each of whose representations is illegal inall Unicode encodings other than its own, with the exception that the BOMfor UCS-4le is equivalent to the sequence <FEFF><0000> in UCS-2le, so itmust be "assumed" that a file in UCS-2le will not have a null as its first"real" character.

2) If what you want to do is set a BOM at the head of your UTF-8 file, youshould have nothing to do ("setglobal bomb" takes care of that). To makesure of whether it is there or not, use ":setlocal bomb?" (without thequotes, but with the question mark). Vim should answer either " bomb" or"nobomb".

3) To place or remove a BOM on *any* Unicode file, set or unset 'bomb'(respectively).

See :help 'fileencoding' :help 'fileencodings' :help 'bomb'

HTH,Tony.

<think>好的,用户遇到了Vim的E37错误,提示"No write since last change (add ! to override)"。我需要先理解这个错误的含义和常见原因,然后整理出解决方法。 首先,E37错误通常发生在用户试图退出Vim但未保存修改时。Vim为了防止数据丢失,默认要求用户明确是否保存。如果用户输入:q而没有保存,就会触发这个错误。这时候需要强制退出的话,应该使用:q!。 接下来,用户可能的操作场景是什么?可能是用户修改了文件,但忘记保存,或者不确定是否需要保存。也有可能用户误操作,想要退出而不保存。需要分情况说明解决方法。 然后,我需要确保回答的结构清晰,按照用户之前提供的模板,分错误原因、解决方法、常见问题扩展等部分。同时要遵循系统级指令,使用正确的LaTeX格式,虽然这里可能用不上数学公式,但要注意行内代码用反引号,独立代码块用三个反引号。 需要检查是否有其他相关情况,比如文件只读时如何保存,可能需要使用:w !sudo tee %这样的命令。另外,用户可能想保存修改,这时候应该说明如何用:w保存后再退出。 还要考虑用户可能的疑问,比如为什么会有这个错误,如何避免,或者是否配置Vim自动保存。可能需要提到自动保存插件或者设置选项,比如autowrite。 另外,用户可能分不清:q、:q!:wq的区别,需要做一个对比表格,帮助理解不同命令的用途。 最后,确保语言口语化,避免技术术语过多,但又要准确。比如用“工作区”而不是“buffer”可能会更易懂。还要注意步骤分解,让用户一步步跟着操作。 现在需要组织这些信息,按照之前的模板结构,确保每个部分都涵盖到,并且逻辑清晰。可能需要补充示例代码和命令,以及常见问题,比如文件只读的情况如何处理,或者如何设置Vim自动保存。 还要检查是否有遗漏的情况,比如如果用户强制退出后如何恢复修改,或者是否有其他相关错误,比如E45只读选项。这时候可能需要提到使用sudo或者修改文件权限。 总之,需要全面覆盖E37错误的各种情况和解决方法,同时保持回答简洁明了,结构清晰,符合用户的需求。</think>--- ### 一、错误原因 当你在 Vim 中修改了文件内容,但尝试用 `:q` 退出时,会触发 **E37: No write since last change** 错误。这是因为 Vim 检测到缓冲区(工作区)有未保存的更改,为防止数据丢失,**默认要求用户明确是否保存**。 --- ### 二、解决方法 #### 1. 不保存强制退出(放弃修改) | 步骤 | 命令 | 说明 | |------|------------|-------------------------------| | 1 | 按 `ESC` | 确保处于命令模式 | | 2 | 输入 `:q!` | 强制退出,丢弃所有未保存修改 | | 3 | 按回车 | 执行命令 | #### 2. 保存后退出(保留修改) | 步骤 | 命令 | 说明 | |------|------------|-------------------------------| | 1 | 按 `ESC` | 确保处于命令模式 | | 2 | 输入 `:wq` | 保存文件并退出 | | 3 | 按回车 | 执行命令 | --- ### 三、关键命令对比 | 命令 | 行为 | 适用场景 | |--------|--------------------------------|-----------------------------| | `:q` | 退出(无修改时可用) | 未修改文件且想退出 | | `:q!` | **强制退出不保存** | 放弃所有未保存修改 | | `:wq` | 保存并退出 | 保存修改后退出 | | `:w` | 仅保存不退出 | 临时保存但继续编辑 | | `:x` | 等同于 `:wq` | 快捷保存退出 | --- ### 四、常见问题扩展 #### 1. 为什么会出现 E37 错误? - **直接原因**:文件内容被修改但未保存。 - **深层原因**:用户可能误触键盘导致内容变化(如按 `i` 进入插入模式后意外输入字符),或忘记保存。 #### 2. 文件只读时如何保存? 若文件权限为只读(例如系统配置文件),需用管理员权限保存: ```bash :w !sudo tee % # 输入后会提示输入密码 ``` 保存后继续执行 `:q!` 退出。 #### 3. 如何避免频繁触发 E37? - **配置 Vim 自动保存**: 在 `~/.vimrc` 中添加以下配置,实现失去焦点时自动保存: ```vim autocmd FocusLost * silent! wall " 窗口失去焦点时保存所有文件 ``` - **使用插件**: 安装 [vim-auto-save](https://github.com/907th/vim-auto-save) 插件实现实时保存。 --- ### 五、操作示例 #### 场景:误修改文件后想放弃更改 1. 按 `ESC` 确保处于命令模式。 2. 输入 `:q!` 并按回车。 3. 重新打开文件验证内容是否恢复。 #### 场景:修改后需要保存 1. 按 `ESC` 确保处于命令模式。 2. 输入 `:wq` 并按回车。 3. 终端会返回命令行,文件已保存。 --- ### 六、特殊技巧 #### 1. 分屏编辑时退出所有窗口 若同时编辑多个文件分屏,使用 `:qa!` 放弃所有修改并退出,或 `:wqa` 保存所有修改并退出。 #### 2. 恢复误关闭的文件 Vim 默认会生成交换文件(`.swp`),若意外关闭可通过以下命令恢复: ```bash vim -r filename # 恢复未保存的修改 ``` --- ### 七、总结 E37 是 Vim 的**保护机制**,核心逻辑是: $$ \text{退出行为} = \begin{cases} \text{保存退出} & \text{if 需要保留修改} \\ \text{强制退出} & \text{if 放弃修改} \end{cases} $$ 掌握 `:q!` 和 `:wq` 的用法即可灵活应对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值