Home » Questions » Computers [ Ask a new question ]

How do I find the encoding of the current buffer in vim?

How do I find the encoding of the current buffer in vim?

Say I am editing some file with vim (or gvim). I have no idea about the file's encoding and I want to know whether it is in UTF-8 or ISO-8859-1 or whatever? Can I somehow tell vim to show me what encoding is used?

Asked by: Guest | Views: 265
Total answers/comments: 2
bert [Entry]

"The fileencoding setting shows the current buffer's encoding:

:set fileencoding
fileencoding=utf8

There really isn't a common way to determine the encoding of a plaintext file, as that information isn't saved in the file itself - except UTF-8 Files where you've got a so called BOM which indicates the Encoding. This is why xml and html files have charset metatags.

You can enforce a particular encoding with the 'encoding' setting. See :help encoding and :help fileencoding in Vim for how the editor handles these settings. You can also add several fileencoding settings to your vimrc to have vim try detecting based on the ones listed."
bert [Entry]

"I found that : vim.fandom.com/wiki/Reloading_a_file_using_a_different_encoding

You can reload a file using a different encoding if Vim was not able to detect the correct encoding :

:e ++enc=<encoding>

where encoding could be cp850, ISO-8859-1, UTF-8, ...

You can use file yourfilename to find encoding or chardetect (provided by python-chardet or uchardet depending your Linux distribution) as suggested by dotancohen."