I have written a file with :set fileencoding=ISO-2022-JP :wq. When I try to open it again it is completely unreadable. :set encoding? shows that vim has misinterpreted the content as encoding=utf-8. When I try to change the encoding with :set encoding=ISO-2022-JP vim show as a red text for a shorter time than I could read it, but :messages shows the error message E474: Invalid argument: encoding=ISO-2022-JP.

I have also tried :e ++encoding=ISO-2022-JP but without success, no error message, the text stays the same gibberish.

What am I doing wrong?

  • Note that the option encoding is the coding used in Vim internally. This should be of no consequence to your problem. What does :set fileencoding? show after loading the file?
    – Friedrich
    Commented Sep 8, 2023 at 9:37

2 Answers 2


Vivian's answer covers the one-shot case. If you regularly handle files saved in an esoteric (read: non-western) encoding, it will help to tweak the fileencodings (note the trailing 's') option to your needs.

In your vimrc you could do something like:

set fileencodings=iso-2022-jp,ucs-bom,utf-8,default

Order is important here as Vim will pick the first encoding that works without errors. The actual encodings to try depend on the files you expect to encounter. Spend some thought on getting this option right and do not copy this line verbatim.

If set up correctly, it will make Vim guess the right fileencoding when opening a file.

See :help 'fileencoding' and :help 'fileencodings' for more information.

  • interesting, with :set fileencodings=iso-2022-jp :e it loads the file correctly, with :set fileencodings=utf-8,iso-2022-jp :e it loads it as utf-8
    – jakun
    Commented Sep 8, 2023 at 10:25
  • As I wrote (and as :help fileencodings says as well) the order is important. Vim will try the entries in the list from the start and pick the first one that works. So if whatever you load could be valid utf-8, it will pick it.
    – Friedrich
    Commented Sep 8, 2023 at 10:31
  • yes, makes sense. I had realized that the order is important, I wanted it to try utf-8 first because utf-8 is more common. What I failed to realize was that those weird unprintable characters vim was showing are actually valid utf-8 characters so there is no way for vim to know that this was not the correct encoding.
    – jakun
    Commented Sep 8, 2023 at 10:44
  • You may run into problems either way. Guessing encodings is error prone. If you want to use a specific encoding for a file, use ++enc as per Vivian's answer.
    – Friedrich
    Commented Sep 8, 2023 at 11:01
  • ++enc does not work for me, so I'll just temporarily set fileencodings to the one encoding that I want to use
    – jakun
    Commented Sep 8, 2023 at 11:16

To help Vim open the file (e.g. foo.txt) with the correct encoding (e.g. cp932) you can do:

:e ++enc=cp932 foo.txt

I suppose you have to convert your file first into a supported Japanese encoding (e.g. cp932, euc-jp, sjis)

More information about the supported encoding with :help mbyte-encoding.

I'm not sure ISO-2022-JP is supported.

It seems that ISO-2022-JP is somehow related to sjis the following is probably worth trying:

:e ++enc=sjis foo.txt

The documentation recommends nkf to convert the Kanji file. With the tool installed you can do:

:e ++enc=euc-jp foo.txt
:%!nkf -e

Remark: If the file is open with the wrong encoding to my knowledge you have to reopen it using the correct one.

  • 1
    Thanks for your answer. Not sure about the "ISO-2022-JP not supported", e.g. :help 'fileencodings' features an example on iso-2022-jp. Maybe the caps are the problem?
    – Friedrich
    Commented Sep 8, 2023 at 9:56
  • @Friedrich no, :set encoding=iso-2022-jp gives the same error message
    – jakun
    Commented Sep 8, 2023 at 10:05
  • @jakun encoding is the wrong option as I wrote in my comment on your question. Do not :set encoding.
    – Friedrich
    Commented Sep 8, 2023 at 10:06
  • @Friedrich thank you, I had not yet seen your comment on my question. :set fileencoding? shows fileencoding=utf-8.
    – jakun
    Commented Sep 8, 2023 at 10:11
  • 1
    I see iso-2022-jp is not in the list of supported values for encoding (which is why it doesn't work). For fileencoding these and more (depending on OS and installed libs) are supported so there's a chance for iso-2022-jp to work as fileencoding only. A definitive list would be nice but I see how it would be hard to write one.
    – Friedrich
    Commented Sep 8, 2023 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.