参考资料:
1、首先什么是REPL?
A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily includable in other programs. REPL provides a way to interactively run JavaScript and see the results. It can be used for debugging, testing, or just trying things out.
交互式解释器(REPL)既可以作为一个独立的程序运行,也可以很容易地包含在其他程序中作为整体程序的一部分使用。REPL为运行JavaScript脚本与查看运行结果提供了一种交互方式,通常REPL交互方式可以用于调试、测试以及试验某种想法。
可以理解执行的交互式命令行界面,相当于一个DOS界面的Shell.
Python是支持REPL的语言。
2、配置运行Python3.5
首先点击Preferences->Browse Package,在打开的文件夹中找到 SublimeREPL文件夹,再进入config文件夹,可以看到许多语言的配置文件,Python也在里面。
模仿python文件夹来配置python3。
首先在这里新建一个Python3.5文件夹,然后在里面新建Default.sublime-commands和Menu.sublime-menu两个文件(模仿Python文件夹)。我们Python3.5目前只要能打开shell运行和运行这个脚本两个功能,因此就只要包含Python3.5 和 Python3.5 – Run current file两项就好了。
Default.sublime-commands文件配置:
[
{
"caption": "SublimeREPL: Python3.5",
"command": "run_existing_window_command", "args":
{
"id": "repl_python3.5",
"file": "config/Python3.5/Main.sublime-menu"
}
},
{
"caption": "SublimeREPL: Python3.5 - RUN current file",
"command": "run_existing_window_command", "args":
{
"id": "repl_python3.5_run",
"file": "config/Python3.5/Main.sublime-menu"
}
}
]
Menu.sublime-menu文件配置:
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "R",
"id": "SublimeREPL",
"children":
[{
"caption": "Python3.5",
"id": "Python3.5",
"children":
[{
"command": "repl_open",
"caption": "Python3.5",
"id": "repl_python3.5",
"mnemonic": "P",
"args":
{
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python3.5", "-i", "-u"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python3.5",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
},
{
"command": "repl_open",
"caption": "Python3.5 - RUN current file",
"id": "repl_python3.5_run",
"mnemonic": "R",
"args":
{
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python3.5", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python3.5",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
}
]
}
]
}]
}
]
注意Default.sublime-commands文件里的id 和Menu.sublime-menu文件里的id要一致,Menu.sublime-menu里的caption就是菜单栏的层级内容,cmd里的内容python3.5就是你直接在ubuntu terminal里打的命令,而Default.sublime-commands文件里的file指向了Menu.sublime-menu文件,通过id和文件名,就可以找到相应命令的配置。
保存文件后可以从菜单栏Tools->SublimeREPL->Python3.5运行命令。
3、设置key binding
每次这样到菜单栏里去找,太麻烦,在Preference->key Bindings-User里设置快捷键,在文件中写:
[
{"keys":["f4"],
"caption": "SublimeREPL: Python - RUN current file",
"command": "run_existing_window_command", "args":
{
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
},
{"keys":["f5"],
"caption": "SublimeREPL: Python3.5 - RUN current file",
"command": "run_existing_window_command", "args":
{
"id": "repl_python3.5_run",
"file": "config/Python3.5/Main.sublime-menu"
}
}
]
即设置F4运行python,F5运行python3.5。