Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save open files with new encoding in Sublime Text 3

Tags:

sublimetext3

I would like to save multiple files in Sublime Text 3 with a new character encoding.

I have tried using the following key command to achieve this with no luck. Basically nothing happens when I press the key combination.

{
 "keys" : ["ctrl+alt+s"],
 "command" : "save_all", "args" :{"encoding" : "Western (Windows 1252)"}
} 

If I check the key binding in the console with sublime.log_commands(True) I can see that the key binding is working because it returns command: save_all {"encoding": "Western (Windows 1252)"} But Sublime Text is still not saving the file as Western (Windows 1252). If I reopen the file it says UTF-8.

Is this at all possible or is there perhaps something wrong with the key binding I am using?

like image 917
Arete Avatar asked Mar 04 '16 13:03

Arete


People also ask

How do I save in Sublime Text 3?

Save a File with Sublime TextIn Sublime Text's top menu bar, choose File > New File. An untitled, blank file will appear. Next, choose File > Save or Save As.

What is UTF with BOM?

The UTF-8 file signature (commonly also called a "BOM") identifies the encoding format rather than the byte order of the document. UTF-8 is a linear sequence of bytes and not sequence of 2-byte or 4-byte units where the byte order is important. Encoding. Encoded BOM. UTF-8.


1 Answers

I would guess that the save_all command doesn't support the encoding argument.

You can save all open files with a different encoding with a short python snippet.


For a single use, you could simply run the following in Sublime's python console:

[view.run_command('save', { "encoding": "Western (Windows 1252)" }) for view in window.views()]

To create a keybinding:

  • Goto the Tools menu
  • Developer
  • New Plugin...

Replace the contents of the new file with the following:

import sublime, sublime_plugin

class SaveAllWithEncodingCommand(sublime_plugin.TextCommand):
    def run(self, edit, **kwargs):
        for view in self.view.window().views():
            view.run_command('save', kwargs)

Save it as save_all_with_encoding.py in the Packages/User folder (it should default to this folder when it shows the save as dialog...)

Then your keybinding will need to look like this:

{
   "keys" : ["ctrl+alt+s"],
   "command" : "save_all_with_encoding", "args" : {"encoding" : "Western (Windows 1252)" }
}
like image 193
Keith Hall Avatar answered Sep 20 '22 01:09

Keith Hall