本文主要介绍我是如何把sublime配置成一个用着顺手的IDE的

安装Package Control

  1. 这部分参考官方安装说明,之后重启即可

几个插件的安装及配置

  1. ctrl + p,键入pcip,安装AutoPEP8SublimeREPLConvertToUTF8
    Sublime Text > Preferences > Package Settings > AutoPep8 > Settings-User中加入:
{
    "settings": {
        "sublimeautopep8": {
            "max-line-length": 79,
            "format_on_save": true,
            "show_output_panel": true,
            "ignore": "E24,E226,E501",
            "syntax_list": ["Python"],
            "file_menu_search_depth": 3
        }
    }
}
  1. Sublime Text> Preferences > Settings 中加入
{
    "font_size": 12,
    "ignored_packages":
    [
        "Vintage"
    ],
    "tab_size": 4,
    "translate_tabs_to_spaces": true
}

设置Project的Python解释器

  1. 打开我们的项目文件进行编辑:
  2. 在项目的设置中添加一个新键,指向所需的virtualenv:
"settings": {
    "python_interpreter": "/home/user/.virtualenvs/example/bin/python"
}

创建插件以获取此设置并启动SublimeREPL

  1. 浏览到Sublime Text’s Packages 目录:Preferences > Browse Packages
  2. 为我们的插件创建一个新的python文件,如: project_venv_repls.py
  3. 将以下python代码复制到此新文件中:
import sublime_plugin


class ProjectVenvReplCommand(sublime_plugin.TextCommand):
    """
    Starts a SublimeREPL, attempting to use project's specified
    python interpreter.
    """

    def run(self, edit, open_file='$file'):
        """Called on project_venv_repl command"""
        cmd_list = [self.get_project_interpreter(), '-i', '-u']

        if open_file:
            cmd_list.append(open_file)

        self.repl_open(cmd_list=cmd_list)

    def get_project_interpreter(self):
        """Return the project's specified python interpreter, if any"""
        settings = self.view.settings()
        return settings.get('python_interpreter', '/usr/bin/python')

    def repl_open(self, cmd_list):
        """Open a SublimeREPL using provided commands"""
        self.view.window().run_command(
            'repl_open', {
                'encoding': 'utf8',
                'type': 'subprocess',
                'cmd': cmd_list,
                'cwd': '$file_path',
                'syntax': 'Packages/Python/Python.tmLanguage'
            }
        )

设置热键

  1. 打开用户密钥绑定文件:Preferences > Key Bindings - User
  2. 添加一些键绑定以使用该插件。一些例子:
// Runs currently open file in repl
{
    "keys": ["f5"],
    "command": "project_venv_repl"
},
// Runs repl without any file
{
    "keys": ["f6"],
    "command": "project_venv_repl",
    "args": {
        "open_file": null
    }
},
// Runs a specific file in repl, change main.py to desired file
{
    "keys": ["f7"],
    "command": "project_venv_repl",
    "args": {
        "open_file": "/home/user/example/main.py"
    }
}

参考链接:
【记录】Ubuntu16.04装机
Sublime text3和virtualenvs

发表评论

电子邮件地址不会被公开。 必填项已用*标注