加油努力,

翻译:打造基于Sublime Text 3的全能python开发环境

原文地址: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在另一个)特别游泳.

  1. Vintage Mode provides you with vi commands for use within ST3.

  2. Chrome-like Tabs make navigating and editing several files much simpler.
  3. 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.
  4. 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.

  1. Vintage模式允许你在st3中使用vi命令
  2. Chrome-like标签也让多文件导航和编辑变得更加容易
  3. 自动加载上一次会话,自动打开你上次关闭st3之前打开的文件和文件夹.作者st3常开,并且同时打开多个项目,如果重启电脑,立刻打开备份的文件和文件夹
  4. 代码片段让你能够通过一个关键词来创建代码段来提高你的效率.有很多默认的代码片段.例如,新建一个文件,输入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.

  1. 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.

  2. 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.

  3. 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包(插件).

  1. 为了安装插件你必须复制这里为st3设计的python代码.单击视图→显示控制台来打开st3控制台,把代码粘贴到控制带,回车并且重启st3
  2. 你现在可用通过ctrl+shift+p来安装插件.按快捷键后输入instal,直到Package Control: Install Package出现.敲回车并且搜索可用的插件.
  3. 一些其他的命令包括
    1. 列出你已经安装的插件
    2. 删除一个特定的插件
    3. 升级特定的插件
    4. 升级/覆盖所有包 升级你所有的插件

       

查看官方文档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
}

 

  1. 特定语言的配置,Sublime Text > Preferences > Settings – More > Syntax Specific – Use,用语言名.sulime-settings来保存,比如针对python的配置文件就是Python.sublime-settings
  2. 你可以对st做任意配置,但是我强力推荐你从配置基本配置和针对python的配置开始,接下来你将会看到改变
  3. 可选:你可以用Dropbox来同步你所有的配置,把文件上传到Dropbox并且在所有的电脑上加载并同步sublime环境.
  4. 一个很好的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的功能,如:

  • Autocompletion works by default, but there are a number of configuration options.默认自动完成,并且有一些配置选项
  • Code linting uses either PyLint or PyFlakes with pep8. I personally use a different linting package, as I will explain shortly, so I disable linting altogether within the user-defined Anaconda settings file, Anaconda.sublime-settings, via the file menu: Sublime > Preferences > Package Settings > Anaconda > Settings – User: {"anaconda_linting": false} 代码检查,用pylint或者pyflakes及pep8,作者使用不同的代码检查插件,所以作者简单的通过Anaconda用户配置文件来禁用Anaconda插件自带的代码检查功能
  • McCabe code complexity checker runs the McCabe complexity checker tool within a specific file. If you’re unfamiliar with a complexity checker, be sure to visit the link above.代码复杂度检测
  • Goto Definitions finds and displays the definition of any variable, function, or class throughout your entire project.查找并显示你工程中任何变量,函数和类.
  • Find Usage quickly searches where a variable, function, or class has been used in a specific file.特定文件中已经使用过的变量函数和类.
  • Show Documentation: shows the Docstring for functions or classes (if defined, of course).如果定义了文档,显示文档.

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.

posted @ 2015-07-21 23:16  活着挺好  阅读(4848)  评论(0编辑  收藏  举报

我是郎