设置 Sublime Text 的 Py

发布时间:2019-08-26 07:16:59编辑:auto阅读(1763)

    最近,当我主要使用Python开发环境编辑的时候,我开始越来越多地用到Sublinme Text 2.

    这篇文章主要说明了能让Python的编程者使用更方便的一些设置和调整。

    设置 Sublime Text 的 Python 开发环境

    为何选择Sublime Text?

    最近几周我开始越来越多的使用 Sublime Text。当我将它安装好之后,感觉非常不错。它确实非常快,自动定期的更新,以及更棒的是完全支持跨平台。对我来说,它最终胜过TextMate的地方是Sublime强大的插件子系统。对于Python开发,有不少插件可以让你开发起来更流畅、更有乐趣。

    我现在仍然在不同的项目之间切换编辑器。不过我发现对应Python开发,Sublime在轻量级的编辑器和全功能的IDE之间有着很好的平衡。

    字体的选择

    Ubuntu Mono 是非常非常不错的字体。前些天我刚从 Menlo 切换过来,这绝对不让人后悔。

    在我的15寸的MacBook上,Ubuntu Mono的16号字非常适合。1680 × 1050的分辨率对于一个边栏加两个编辑器窗口(自动调整到80个字符宽)刚好合适。

    如果你打算认真的挑选一下字体, slant.co的这篇文章 写的不错。它包含了大部分流行的编程方面的字体的截图及下载链接。

    安装插件

    正如之前提到的, Sublime 有一个非常丰富的插件系统。而我当前使用的插件如下:

    • Package Control 在 Sublime 里直接安装附加插件的包管理器。这是唯一一个你必须手动安装的插件。这边列出的其他所有插件都可以通过 Package Control 来安装。也可以通过它来更新已安装过的插件。简单得想做是 Sublime packages 的 apt-get 就行了。

      Package for sublime text 2安装步骤:

          通过ctrl+` shortcut or the View > Show Console   打开Sublime Text console.粘贴下列行:

    import urllib2,os,hashlib; h = '7183a2d3e96f11eeadd761d777e62404e330c659d4bb41d3bdf022e94cab3cd0'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler()) ); by = urllib2.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); open( os.path.join( ipp, pf), 'wb' ).write(by) if dh == h else None; print('Error validating download (got %s instead of %s), please try manual install' % (dh, h) if dh != h else 'Please restart Sublime Text to finish installation')



    • Color Scheme - Tomorrow NightColor schemes 决定了编辑器界面语法高亮的字体颜色。这是一个非常酷的暗黑系样式。

    • Theme - Soda DarkThemes 影响 Sublime 界面元素的颜色和风格。这个非常适合 Tomorrow Night 的配色方案。

    • SideBarEnhancements 这个插件提供了侧边栏附加的上下文菜单选项,例如"New file","New Floder"等。这些本应当默认就该有的,却没有。

    • All Autocomplete Sublime 默认的自动完成只关注当前文件的单词。这个插件扩展了其自动完成的单词列表到所有打开的文件。

    • SublimeCodeIntel 为部分语言增强自动完成功能,包括了 Python 。这个插件同时也可以让你跳转到符号定义的地方,通过按住 alt 并点击符号。非常方便。

    • SublimeREPL 允许你在编辑界面直接运行 Python 解释器。我倾向于在单独的终端窗口用 bpython 来运行,但有时 SublimeREPL 是很有帮助的。

    • GitGutter 在编辑器的凹槽区,依照 Git ,增加小图标来标识一行是否被插入、修改或删除。在 GitGutter 的 readme 中有说明如何更改颜×××标来更新你的配色方案文件。

    • Pylinter 这个插件提供了目前我所见到的最好的 pylint 编辑器整合。它自动检查 .py 文件,无论其何时被保存,并且会直接在编辑界面显示 pylint 违规。它还有一个快捷方式来禁用局部的 pylint 检查,通过插入一个 #pylint: 禁用注释。这个插件对于我确实非常有用。

    配置文件

    Sublime Text 的一个优点就是它的所有配置都是简单的基于 JSON 的配置文件。这使得你可以很容易的将配置转到另一个系统中。我也见过一些人使用 Dropbox 自动同步他们所有电脑上的配置。

    Preferences.sublime-settings 配置了 Sublimede 的显示和行为.你可以在sublime 中通过 Preferences > Settings — User 打开并编辑此文件。我使用如下配置:

    01{
    02// Colors
    03"color_scheme": "Packages/Tomorrow Color Schemes/Tomorrow-Night.tmTheme",
    04"theme": "Soda Dark.sublime-theme",
    05
    06// Font
    07"font_face": "Ubuntu Mono",
    08"font_size": 16.0,
    09"font_options": ["subpixel_antialias", "no_bold"],
    10"line_padding_bottom": 0,
    11"line_padding_top": 0,
    12
    13// Cursor style - no blinking and slightly wider than default
    14"caret_style": "solid",
    15"wide_caret": true,
    16
    17// Editor view look-and-feel
    18"draw_white_space": "all",
    19"fold_buttons": false,
    20"highlight_line": true,
    21"auto_complete": false,
    22"show_minimap": false,
    23
    24// Editor behavior
    25"scroll_past_end": false,
    26"highlight_modified_tabs": true,
    27"find_selected_text": true,
    28
    29// Word wrapping - follow PEP 8 recommendations
    30"rulers": [ 72, 79 ],
    31"word_wrap": true,
    32"wrap_width": 80,
    33
    34// Whitespace - no tabs, trimming, end files with \n
    35"tab_size": 4,
    36"translate_tabs_to_spaces": true,
    37"trim_trailing_white_space_on_save": true,
    38"ensure_newline_at_eof_on_save": true,
    39
    40// Sidebar - exclude distracting files and folders
    41"file_exclude_patterns":
    42[
    43".DS_Store",
    44"*.pid",
    45"*.pyc"
    46],
    47"folder_exclude_patterns":
    48[
    49".git",
    50"__pycache__",
    51"env",
    52"env3"
    53]
    54}

    Pylinter.sublime-settings配置了pylinter 插件。我使用下面的配置让 Pyhton 在保存时自动规范,并对违反规范显示图标。

    01{
    02// Configure pylint's behavior
    03"pylint_rc": "/Users/daniel/dev/pylintrc",
    04
    05// Show different icons for errors, warnings, etc.
    06"use_icons": true,
    07
    08// Automatically run Pylinter when saving a Python document
    09"run_on_save": true,
    10
    11// Don't hide pylint messages when moving the cursor
    12"message_stay": true
    13}

    按键绑定

    Sublime 的按键绑定也是全部可配置的基于JSON的 sublime-keymap 配置文件。我修改了一些默认配置以更好的配合我的 TextMate / IntelliJ 肌肉记忆。你可以完全不修改。如果你想,修改很简单,并可以跨平台使用。我使用如下的绑定:

    01[
    02// Rebind "go to file" to cmd+shift+O
    03{ "keys": ["super+shift+o"], "command": "show_overlay", "args": {
    04"overlay": "goto",
    05"show_files": true
    06}},
    07
    08// Rebind swap line up/down to cmd+shift+up/down
    09{ "keys": ["super+shift+up"], "command": "swap_line_up"},
    10{ "keys": ["super+shift+down"], "command": "swap_line_down"},
    11
    12// Delete a line with cmd+delete
    13{ "keys": ["super+backspace"], "command": "run_macro_file", "args": {
    14"file": "Packages/Default/Delete Line.sublime-macro"
    15}},
    16
    17// Reindent selection with cmd+alt+L
    18{ "keys": ["super+alt+l"], "command": "reindent"}
    19]

    命令行工具

    同 TextMate 的 mate 类似,Sublime Text 包含了一个命令行工具,允许你通过 shell 打开编辑器。工具名为 sublis,默认不可用。要使之生效,在任一 shell 中运行下面:

    1ln-s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl

    要将 Sublime 作为 git 互动命令的默认编辑器使用——举例,撰写提交信息——只需添加下面一行到你的 ~/.profile 文件:

    1exportGIT_EDITOR="subl --wait --new-window"



    SublimeREPL添加快捷键

    转自自由的风 基于Sublime Text搭建Python IDE

    SublimeREPL安装之后没有快捷键,每次运行程序必须用鼠标去点工具栏,有些不爽,所以需要给SublimeREPL添加快捷键。

    这里可以看到所有Python方法的名称及id,根据此可以自定义用户快捷键,在偏好--键绑定–用户中输入:

    [{"keys":["f5"],
    "caption":"SublimeREPL: Python - RUN current file",
    "command":"run_existing_window_command", "args":
    {
    "id":"repl_python_run",
    "file": "config/Python/Main.sublime-menu"
    }}
    ]

    本文仅定义了Python- RUN current file的快捷键,我在这里使用的是F5,可以根据自己的需要进行灵活的调整。


关键字