Sublime Forum

Different encodings for different files extensions



I have, for example, file 1.p with enconding “KOI8-R”. And I have file 2.txt with encoding “Windows 1251”.

Is it a way to set for extension “.p” always open with encoding “KOI8-R”, and for extension “txt” with “Windows 1251”.

Sorry for my english. ))


How to automatically open with specific encoding for some file extensions?

No solution for this question?



It’s not built in, but you can write a simple plugin to do it. I would recommend taking a look at the ApplySyntax plugin as a guide. It does something very similar to what you want. It applies a particular syntax based on a variety of recognition methods. Rather than calling the “set_syntax_file” method, you would want to call the “set_encoding” method.

Hope that helps.



At a guess, here’s what you’d need to do:

You’'ll need to make a copy of the “Plain Text” syntax type first. You can then make settings for your new syntax type with custom default character encoding, and assign certain file extensions to be opened with the new syntax type…

  1. In your sublime profile folder (say c:\users\xxx\AppData\Roaming\Sublime Text 2\Packages) make a copy of the “Text” folder (this is the folder for “Plain Text” syntax type) and rename the folder copy to (say) “P Text”.

  2. Next open up a .p file and click bottom right to change syntax (where it says “Plain Text”). The big pop-up menu, choose “open all with current extension as…”, and choose your new syntax type “P Text”

  3. Finally, with a .p file open (and syntax as “P Text”), choose menu “Prefs…Settings more…Syntax specific”.

Add the following lines to the empty syntax file and save it:

    "default_encoding": "Cyrillic (KOI8-R)"

Give it a try and post back if it works!




Thank qgates very much! It really works!!!



Tnx for great solution, but i need to speak some words.
In my case, it works only with code:

	"fallback_encoding": "Cyrillic (Windows 866)"

in syntax-specific file, i mean…