Sublime Text: how to automatically open with a specific encoding for some file extensions?

Is there a way to force sublime (ST3) to open / open some kind of file with a specific encoding? My problem: I do not want ST3 to show content for a specific file extension (i.e. .log, .dump and my other user files). The current solution is to manually set the encoding in hexadecimal using the view.set_encoding () method. In this discussion, another solution was proposed. But it seems that only after the file has been opened does it establish the encoding. Is there a way to automatically open or reopen these files using hexadecimal encoding?

NOTE. I set default_encoding to UTF-8 and will return to hex. Since ST3 discovers the contents of my user files as UTF-8, the contents have been shown. I just want ST3 to display a hexadecimal view for some custom files and have to manually change the encoding to display / change the content.

Really appreciate any help

+4
source share
1 answer

Demonstration:

GIF Demo

The code:

Save it @Packages/YourPluginName/YourPluginName.py

import sublime, sublime_plugin

class EventListener( sublime_plugin.EventListener ):

    def on_load ( self, view ):

        fileExtension = view.window().extract_variables() [ "file_extension" ]

        encodingSets = \
            {
                "log"  : "Hexadecimal",
                "dump" : "Hexadecimal",
            }

        if fileExtension in encodingSets:
            encoding = encodingSets[ fileExtension ]
            view.run_command ( "reopen", { "encoding" : encoding } )

Notes:

I found a command reopen + encodingin This thread and wrapped it in , where you can define key-value pairs of extensions and their associated encodings. on_load EventListener

, reset ctrl + z. , , . , . , ctrl + z, .

:

Sublime Text:

"Hexadecimal"
"UTF-8"
"UTF-16 LE"
"UTF-16 BE"
"Western (Windows 1252)"
"Western (ISO 8859-1)"
"Western (ISO 8859-3)"
"Western (ISO 8859-15)"
"Western (Mac Roman)"
"DOS (CP 437)"
"Arabic (Windows 1256)"
"Arabic (ISO 8859-6)"
"Baltic (Windows 1257)"
"Baltic (ISO 8859-4)"
"Celtic (ISO 8859-14)"
"Central European (Windows 1250)"
"Central European (ISO 8859-2)"
"Cyrillic (Windows 1251)"
"Cyrillic (Windows 866)"
"Cyrillic (ISO 8859-5)"
"Cyrillic (KOI8-R)"
"Cyrillic (KOI8-U)"
"Estonian (ISO 8859-13)"
"Greek (Windows 1253)"
"Greek (ISO 8859-7)"
"Hebrew (Windows 1255)"
"Hebrew (ISO 8859-8)"
"Nordic (ISO 8859-10)"
"Romanian (ISO 8859-16)"
"Turkish (Windows 1254)"
"Turkish (ISO 8859-9)"
"Vietnamese (Windows 1258)"

* Packages\Default\Encoding.sublime-menu *

+3

All Articles