简体   繁体   English

如何自动检测Vim中的文件格式?

[英]How to detect file format in vim automatically?

Though set ff? 虽然set ff? detects a fileformat manually, I don't know how to detect on startup. 手动检测文件格式,我不知道如何在启动时进行检测。

I tried if ($ff=='unix') but it didn't. 我试过了if ($ff=='unix')但是没有。

So, is there a better idea? 那么,有更好的主意吗?

要阅读选项,应使用&ff

As @Kent said, it's indeed &ff . 正如@Kent所说的,确实是&ff But be aware of one thing, this only applies to the current buffer. 但是请注意,这仅适用于当前缓冲区。 If you open another file ( :e , :sp ...), it'll come with its own value. 如果打开另一个文件( :e:sp ...),它将带有其自己的值。

A .vimrc is not the best place to analyze the file vim is opened with as it just do that: analyze this only file and react once only. .vimrc并不是分析文件vim的最佳位置,因为这样做是那样的:仅分析该文件,仅反应一次。

What are you trying to achieve exactly? 您到底想达到什么目标? (-> delete ^M if fileformat=unix) (->如果文件格式为unix,则删除^ M)

EDIT: OK. 编辑:好的。 Then, you need to listen for the BufWritePre event to fix the file on saving, or BufReadPost to fix it as soon as you read it. 然后,您需要监听BufWritePre事件以在保存时修复文件,或者监听BufReadPost以在读取文件时对其进行修复。 In that case, you may experience troubles with unmodifiable files, and you'll also have to play with &modifiable . 在这种情况下,您可能会遇到无法修改的文件的麻烦,并且还必须使用&modifiable

aug fix_eol
  au!
  BufReadPost * if &ff!=unix | keepp %s/\r$//e | endif
aug END

In all cases, the test shall not be plainly written in the .vimrc . 在任何情况下,都不应将测试明确地写在.vimrc These four lines can. 这四行就可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM