Snowso
Snowso

Reputation: 5

How to detect file format in vim automatically?

Though set ff? detects a fileformat manually, I don't know how to detect on startup.

I tried if ($ff=='unix') but it didn't.

So, is there a better idea?

Upvotes: 0

Views: 525

Answers (2)

Luc Hermitte
Luc Hermitte

Reputation: 32926

As @Kent said, it's indeed &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.

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.

What are you trying to achieve exactly? (-> delete ^M if fileformat=unix)

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. In that case, you may experience troubles with unmodifiable files, and you'll also have to play with &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. These four lines can.

Upvotes: 1

Kent
Kent

Reputation: 195039

to read an option, you should use &ff.

Upvotes: 3

Related Questions