標籤:
原文地址:https://realpython.com/blog/python/setting-up-sublime-text-3-for-full-stack-python-development/
原文標題:Setting Up Sublime Text 3 for Full Stack Python Development
翻譯:打造基於sublime text 3的全能Python開發環境
Sublime Text 3 (ST3) is lightweight, cross-platform code editor known for its speed, ease of use, and strong community support. It’s an incredible editor right out of the box, but the real power comes from the ability to enhance its functionality using Package Control and creating custom settings.
In this article, we’ll look at how to setup Sublime Text for full stack Python development (from the front to back), enhance the basic functionality with custom themes and packages, and use many of the commands, features, and keyword shortcuts that make ST3 so powerful.
ST3是一個以運行速度,易用性和強大社區支援而著稱的輕量級跨平台代碼編輯器。它是一個難以置信的開箱即用的編輯器,但是通過包管理器和自訂設定來增強本身的功能其實st3強大力量的來源。
本文將介紹如何為全方位的Python開發(從前段到後端)配置ST3,通過自訂佈景主題和擴充報來增強準系統,大量命令,特性和基於關鍵詞快速鍵的使用讓st3變得十分強大.
This tutorial assumes you’re using a Mac and are comfortable with the terminal. If you’re using Windows or Linux, many of the commands will vary, but you should be able to use Google to find the answers quickly given the info in this tutorial.
本教程假設你用的是Mac作業系統並且能夠熟練的使用命令列終端.如果你用的是windows或者Linux作業系統,有些命令可能會不一樣,不過你應該可以通過google來找到如何在你自己系統中使用本教程提到的命令.
Before we start, let’s address what I mean exactly by “full stack”.
In today’s world of HTML5 and mobile development, Javascript is literally everywhere. EVERYWHERE. Python coupled with a framework such as Django or Flask is not enough. To really develop a website from end-to-end, you must be familiar with Javascript (and the various Javascript frameworks), REST APIs, responsive design, and of course HTML and CSS, among others. And let’s face it: Like any craftsman, in order to be the best programmer you can be, your tools must be sharp. Your development environment must be setup for full stack development – which is exactly what we are going to do right now…
在開始本教程之前,讓我們來明確一下我所指的全方位(full stack)是什麼意思.在當今的HTML5和行動裝置開發中,javascript被廣泛使用.everywhere!python結合諸如Django和Flask之類的架構是不能滿足需求的.為了開發一個端到端的網站,你必須熟悉javascript及其大量的js架構,rest apis,響應式設計,html&css和其他.為了成為最好的程式員,工欲善其事必先利其器!你必須把你的開發環境配置成適應全方位開發,這就是我們接下來將要做的
Features
Let’s start by looking at a few of the default features of Sublime Text 3…
Split Layouts allow you to arrange your files in various split-screens. This is useful for test driven development (Python code on one screen, test scripts on another) or when working on the front end (HTML on one screen, CSS and/or Javascript on another).
特性
首先我們來看sublime text 3提供的一系列預設的特性
分割布局允許你用多種螢幕分割方式來組織代碼.在測試驅動的開發中特別用用(python代碼在一個螢幕,測試指令碼在另外一個)或者前段開發(html在一個螢幕,css和js在另一個)特別遊泳.
Vintage Mode provides you with vi commands for use within ST3.
- Chrome-like Tabs make navigating and editing several files much simpler.
- Automatic loading of the last session re-opens all files and folders you had open when you closed the editor the last time. I leave ST3 open all the time, with various projects open – so if I reset the computer, it opens the files and folders right back up.
Code Snippets increase your productivity by giving you the ability to create common pieces of code with a single keyword. There are a number of default snippets. For example, open a new file and type in “lorem” then press tab. You should get a paragraph of lorem ipsum text. Also, if you type “defs” then press tab in a Python file it will setup a generic function.
You can also create your own snippets: Tools > New Snippet. Refer to the documentation for help, and also check out some of my snippets here.
- Vintage模式允許你在st3中使用vi命令
- Chrome-like標籤也讓多檔案導航和編輯變得更加容易
- 自動載入上一次會話,自動開啟你上次關閉st3之前開啟的檔案和檔案夾.作者st3常開,並且同時開啟多重專案,如果重啟電腦,立刻開啟備份的檔案和檔案夾
- 程式碼片段讓你能夠通過一個關鍵詞來建立程式碼片段來提高你的效率.有很多預設的程式碼片段.例如,建立一個檔案,輸入lorem,按tab鍵,你將會得到一大段文字.如果你在python檔案中輸入defs並且敲tab會的到一個通用的函數體.
Customizing Sublime Text 3
After downloading ST3 …
Install the
subl
command line tool
Like the mate
command for TextMate, Sublime Text includes a command line tool called subl
that allows you to open one file, or an entire directory of files and folders, from the terminal.
To enable this command, create a symbolic link to the subl binary:
定製st3
下載st3之後
安裝subl命令列工具
就像TextMate的mate命令,sublime text包含了一個叫做讓你能夠從終端開啟一個檔案或者一系列檔案檔案夾的命令列工具
為了使用這個命令,建立一個到subl庫的符號連結
$ sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/bin/subl
Ensure that the link works by opening Sublime:
通過開啟sublime來確認連結可用
$ subl
If that didn’t work, you probably need to add /bin to your Path:
如果subl不起作用,你可能需要把/bin添加到你的環境變數
$ echo "export PATH=~/bin:$PATH" >> ~/.profile
現在你可用下面的命令開啟檔案和目錄:
# open the current directory$ subl .# open a directory called tests$ subl ~/Documents/test# open a file called text.txt$ subl test.txt
譯註:我用的ubutnu 15.04安裝st3的deb包,命令列下直接就可用使用subl,所以這一步可用省略
$ subl hello.py # 直接就可以在sublime裡開啟,不用設定
$ subl "hello world" # 如果路徑中有空格,路徑必須加引號
$ subl --help # 可用查看所有的命令
Install Package Control
To begin taking advantage of the various packages for extending Sublime’s functionality, you need to install the package manager called Package Control – which you must install manually. Once installed, you can use Package Control to install/remove/upgrade all other ST3 packages.
To install, copy the Python code for Sublime Text 3 found here. Click View > Show Console to open the ST3 console. Paste the code into the console. Press enter. Reboot ST3.
You can now install packages by using the keyboard shortcut cmd+shift+P. Start typing install until Package Control: Install Package appears. Press enter and search for available packages.
Some other relevant commands are:
- List Packages shows all your installed packages
- Remove Packages removes a specific package
- Upgrade Package upgrades a specific package
- Upgrade/Overwrite All Packages upgrades all your installed packages
Check out the official documentation to view more commands.
安裝包管理器為了利用各種擴充sublime功能的包,你需要手動安裝一個叫做Package Control的包管理器.安裝之後,你可以通過它來安裝/卸載/升級所有st3包(外掛程式).
- 為了安裝外掛程式你必須複製這裡為st3設計的python代碼.單擊視圖→顯示控制台來開啟st3控制台,把代碼粘貼到控制帶,斷行符號並且重啟st3
- 你現在可用通過ctrl+shift+p來安裝外掛程式.按快速鍵後輸入instal,直到Package Control: Install Package出現.敲斷行符號並且搜尋可用的外掛程式.
- 一些其他的命令包括
- 列出你已經安裝的外掛程式
- 刪除一個特定的外掛程式
- 升級特定的外掛程式
- 升級/覆蓋所有包 升級你所有的外掛程式
查看官方文檔documentation擷取更多命令.
Create a Custom Settings File
You can fully configure Sublime Text using JSON-based settings files, making it easy to transfer, or synchronize, your customized settings to another system. First, we need to create our customized settings. It’s best to create a base file for all environments as well as language-specific settings files.
To set up a base file click Sublime Text > Preferences > Settings – User. Add an empty JSON object to the file and add your settings like so:
建立自訂設定檔
你可以通過json格式設定檔自由的配置sublime text,採用json格式使得設定檔易於傳輸,或者同步你的配置到其他系統.首先我們需要建立自己的自訂設定.建議分別設定針對所有的環境的基本設定檔和針對特定語言的設定檔.
通過sublime text→選項→配置-使用者來建立一個基本配置.在檔案中添加一個空的json對象,並且添加配置
{ // base settings "auto_complete": false, "sublimelinter": false, "tab_size": 2, "word_wrap": true}
- 特定語言的配置,Sublime Text > Preferences > Settings – More > Syntax Specific – Use,用語言名.sulime-settings來儲存,比如針對python的設定檔就是Python.sublime-settings
- 你可以對st做任意配置,但是我強力推薦你從配置基本配置和針對python的配置開始,接下來你將會看到改變
- 可選:你可以用Dropbox來同步你所有的配置,把檔案上傳到Dropbox並且在所有的電腦上載入並同步sublime環境.
- 一個很好的sublime配置參考Sublime Text Unofficial Documentation.
Themes
ST3 also gives you the option to change the overall theme to better suit your personality. Design your own. Or, if you’re not artistically inclined, you can download one of the various custom themes designed by the Sublime community through Package Control. Check out ColorSublime to preview themes before installing them.
The ever popular Soda Dark Theme and the minimal Flatland are two of my personal favorites.
After installing a theme, make sure to update your base settings, Sublime Text > Preferences > Settings – User:
Packages
除了打包的主題,我也用下面這些外掛程式來增加速我的工作流程.
SideBarEnhancements側邊欄增強
SideBarEnhancements extends the number of menu options in the sidebar, speeding up your overall workflow. Options such as “New file” and “Duplicate” are essential and should be part of ST3 out of the box. The “Delete” option alone makes it worth downloading. This feature simply sends files to the Trash, which may seem trivial but if you delete a file without it, it’s very difficult to recover unless you’re using a version control system.
這個外掛程式擴充了側邊欄冊單的數量,加速你算不的工作流程.比如建立和複製是st3內建的基本選項,而"刪除"選項讓這個外掛程式值得下載.這個功能把檔案放到資源回收筒看起來不是很重要的功能,但是你不用這個選項刪除檔案,除非你使用了版本控制系統否則你很難恢複檔案.
Anaconda
Anaconda是一個旗艦版的Python外掛程式,給st3添加了一系列類似IDE的功能,如:
You can view all of the features here, or within the README file in ST3’s Package Settings: Sublime Text > Preferences > Package Settings > Anaconda > README.點選連結查看所有功能,或者查看readme檔案.
SublimeCodeIntel is another popular package, which has many of the same features as Anaconda. I suggest testing them both out.
這個外掛程式是另外一個很受歡迎的外掛程式,有很多和Anaconda類似的功能,建議你們都試用一下.
Djaneiro
Djaneiro supports Django templating and keyword highlighting and provides useful code snippets (tab completions) for Sublime Text. The snippet system is an incredible timesaver. You can create common Django blocks with only a few keystrokes for templates, models, forms, and views. Check out the official documentation to see a list of snippets.
My personal favorites are for templating: var
creates {{ }}
and tag
creates {% %}
requirementstxt
Requirementstxt provides autocompletion and syntax highlight-lighting as well as a nice version management system for your requirements.txt files.
SublimeLinter
相關閱讀:http://segmentfault.com/a/1190000000389188學習使用sublime外掛程式sublimeLinter
SublimeLinter is a framework for ST3 linters. The package itself does not include any actual linters; those must be installed separately via Package Control using the SublimeLinter-[linter_name] naming syntax. You can view official linters here. There are also a number of third party linters, which can be viewed in Package Control. Check out the installation instructions here.
SublimeLinter是一個ST3代碼檢查架構.外掛程式本身不包含任何檢查器.必須通過外掛程式管理器用SublimeLinter-[linter_name]指定文法.點選連結查看所有官方檢查器.你也可以試用第三方檢查器,在外掛程式管理器力可用查看.點選連結查看安裝指導
For Python linting, I recommend using SublimeLinter-pyflakes and SublimeLinter-pep8.
對於Python代碼檢查,推薦SublimeLinter-pyflakes和SublimeLinter-pep8
I also use SublimeLinter-jshint, SublimeLinter-pyyaml, SublimeLinter-csslint, SublimeLinter-html-tidy, and SublimeLinter-json.
Most of these linters have dependencies associated with them, so please read the installation instructions before installing.
You can customize each linter in the user-defined SublimeLinter.sublime-settings file: Sublime Text > Preferences > Package Settings > SublimeLinter > Settings – User. For example, I ignore the following pep8 errors and warnings:自訂錯誤的方法
"pep8": { "@disable": false, "args": [], "excludes": [], "ignore": "E501,C0301,W0142,W0402,R0201,E1101,E1102,C0103,R0901,R0903,R0904,C1001,W0223,W0232,W0201,E1103,R0801,C0111", "max-line-length": 100, "select": ""},
GitGutter
GitGutter shows little icons in ST3’s gutter area that indicate whether a line has been inserted, modified, or deleted since the last commit.GitGutter 是一個相容Sublime Text 2 或者Sublime Text 3 的外掛程式,它可以在行數的前面顯示那一行是被增加,修改還是刪除。顯示每行變化的外掛程式.
Emmet
Emmet, previously known as Zen Coding, uses simple abbreviations to generate HTML or CSS code snippets.
For example, if you type a bang, !
, then press tab in an HTML file ,the HTML5 doctype and a few basic tags are generated:
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body></body></html>
|
Check out the official documentation as well as this handy cheat sheet for more info.
翻譯:打造基於Sublime Text 3的全能python開發環境