Search code examples
sublimetext

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


Is there any way to force sublime (ST3) to open/reopen some kind of file with specific encoding? My problem is: I don't want ST3 to show content for specific file extension (i.e. .log, .dump, and my any other custom files). The current solution is to set the encoding manually to Hexadecimal through the view.set_encoding() method. The other solution was proposed on this discussion. But it seems only set the encoding after the file was opened. Is there any way to automatically open, or reopen those files with Hexadecimal encoding?

NOTE: I've set the default_encoding to UTF-8 and fallback to Hexadecimal. Since ST3 detect the content of my custom files as UTF-8, the content was then shown. I just want ST3 to show Hexadecimal view for some kind of custom files, and have to manually change the encoding to show/modify the content.

Really appreciate for any help,


Solution

  • Demo:

    GIF Demo

    Code:

    Save this @ 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 the reopen + encoding command at This Thread, and wrapped it in an on_load EventListener where you can define key-value pairs of extensions and their associated encodings.

    Since the file is being reopened within an active buffer, the encoding be reset very easily with the stock key binding ctrl + z.   If you want this to be more secure, you may want find a way to defeat this loophole.   I looked into disabling the undo stack & creating syntax-specific key bindings, but did not find any quick results for either.   Try looking further into those & similar topics, and maybe also consider remapping your ctrl + z command if you are unable to find another solution.

    Encodings:

    Here is a list of acceptable encodings in 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)"
    

    * Extracted from Packages\Default\Encoding.sublime-menu *