I started to learn Python and I installed Sublime Text 3. I had a problem with encoding and I solved that problem by adding the following lines
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Is it possible write these lines automatically at the opening of each file?
This can be done with a very simple plugin. Select Tools → Developer → New Plugin…
, delete the boilerplate content that is there, and enter the following:
import sublime
import sublime_plugin
class AddShebangAndCodingCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n")
class AddShebangListener(sublime_plugin.EventListener):
def on_new(self, view):
view.run_command("add_shebang_and_coding")
Hit Save, which will automatically put you in your Packages/User
directory, and save the file as add_shebang_and_coding.py
. As soon as you do that it will be loaded. Hit CtrlN (⌘N on macOS) to create a new file, and you should see the text at the top.