首頁  >  文章  >  後端開發  >  設定Sublime Text編輯器的Python開發環境

設定Sublime Text編輯器的Python開發環境

巴扎黑
巴扎黑原創
2017-04-05 15:07:351309瀏覽

  最近,當我主要使用Python開發環境編輯的時候,我開始越來越多地用到Sublinme Text 2.這篇文章主要說明了能讓Python的程式設計師使用更方便的一些設定和調整。

My Sublime Text setup

#  為何選擇Sublime Text?

  我以前一直是TextMate的忠實用戶。這是一個輕量級的、開源的軟體,作為OS X的本地應用,具有很好的Mac風格。不過,雖然TextMate是個很棒的編輯器,有時候還是顯得功能不夠。

  我用過一些功能較強的軟體,例如 加上Python插件的IntelliJ IDEA。 我特別喜歡它的debugger和test runner。不過,像IntelliJ這樣的全功能的IDE對於中小專案來說還是顯得過於龐大。

  最近幾週我開始越來越多的使用 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 就行了。 


  • # Color Scheme - Tomorrow Night Color schemes 決定了編輯器介面語法高亮的字體顏色。這是一個非常酷的暗黑系樣式。 


  • # Theme - Soda Dark Themes 影響 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 Configures the display and behavior of Sublimede. You can open and edit this file in sublime through Preferences > Settings — User. I use the following configuration:

{
    // Colors
    "color_scheme": "Packages/Tomorrow Color Schemes/Tomorrow-Night.tmTheme",
    "theme": "Soda Dark.sublime-theme",

    // Font
    "font_face": "Ubuntu Mono",
    "font_size": 16.0,
    "font_options": ["subpixel_antialias", "no_bold"],
    "line_padding_bottom": 0,
    "line_padding_top": 0,

    // Cursor style - no blinking and slightly wider than default
    "caret_style": "solid",
    "wide_caret": true,

    // Editor view look-and-feel
    "draw_white_space": "all",
    "fold_buttons": false,
    "highlight_line": true,
    "auto_complete": false,
    "show_minimap": false,

    // Editor behavior
    "scroll_past_end": false,
    "highlight_modified_tabs": true,
    "find_selected_text": true,

    // Word wrapping - follow PEP 8 recommendations
    "rulers": [ 72, 79 ],
    "word_wrap": true,
    "wrap_width": 80,

    // Whitespace - no tabs, trimming, end files with \n
    "tab_size": 4,
    "translate_tabs_to_spaces": true,
    "trim_trailing_white_space_on_save": true,
    "ensure_newline_at_eof_on_save": true,

    // Sidebar - exclude distracting files and folders
    "file_exclude_patterns":
    [
        ".DS_Store",
        "*.pid",
        "*.pyc"
    ],
    "folder_exclude_patterns":
    [
        ".git",
        "__pycache__",
        "env",
        "env3"
    ]
}

Pylinter.sublime-settings Configures the pylinter plug-in. I use the following configuration to have Pyhton automatically normalize on save and display icons for violations.

{
    // Configure pylint's behavior
    "pylint_rc": "/Users/daniel/dev/pylintrc",

    // Show different icons for errors, warnings, etc.
    "use_icons": true,

    // Automatically run Pylinter when saving a Python document
    "run_on_save": true,

    // Don't hide pylint messages when moving the cursor
    "message_stay": true
}

Key binding

Sublime's key bindings are also fully configurable based on the JSON-based sublime-keymap configuration file. I modified some of the default configurations to better suit my TextMate / IntelliJ muscle memory. You don't have to modify it at all. It's easy to modify and can be used cross-platform if you want. I use the following binding:

[
    // Rebind "go to file" to cmd+shift+O
    { "keys": ["super+shift+o"], "command": "show_overlay", "args": {
        "overlay": "goto",
        "show_files": true
    }},

    // Rebind swap line up/down to cmd+shift+up/down
    { "keys": ["super+shift+up"], "command": "swap_line_up" },
    { "keys": ["super+shift+down"], "command": "swap_line_down" },

    // Delete a line with cmd+delete
    { "keys": ["super+backspace"], "command": "run_macro_file", "args": {
        "file": "Packages/Default/Delete Line.sublime-macro"
    }},

    // Reindent selection with cmd+alt+L
    { "keys": ["super+alt+l"], "command": "reindent"}
]

Command line tools

Similar to TextMate's mate, Sublime Text includes a command line tool that allows you to open the editor through a shell. The tool is called sublis and is not available by default. To make it take effect, run the following in any shell:

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

To use Sublime as the default editor for interactive git commands—for example, writing commit messages—just add the following line to your ~/.profile file:

export GIT_EDITOR="subl --wait --new-window"

More inspiration

I hope this installation guide can help you. If you have any suggestions or comments, please tweet me or send me an email. Also, thanks to the author below and his work on configuring sublime. They inspire me a lot:

  • Kenneth Reitz: Sublime Text 2 Love


  • Drew Barontini: Sublime (2)


  • Filippo Pacifici: Python development with Sublime Text 2 tips and tricks


  • opensourcehacker.org: Sublime Text 2 tips for Python and web developers

以上是設定Sublime Text編輯器的Python開發環境的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn