Link Search Menu Expand Document

Use Sublime!

Switching to Sublime has been the single biggest productivity booster in years. First, take a look at how beautiful it is:

ST3 in Win10 - Stata and Python

What is it?

Sublime Text is a free, cross-platform source code editor. You can easily add multiple plugins to extend its functionality, making it possible to edit and compile Stata code, Python scripts, R, LaTeX, Julia… the list is endless. You can also customize several other aspects like multiple-column layout or keyboard shortcuts, just to name a couple of examples.

You may be wondering what’s so special about running a Python script and compiling a LaTeX document from the same editor, since you may already use a Python IDE like PyCharms and a perfectly fine LaTeX editor like Texmaker. Thing is, large projects are usually complex and require handling a wide variety of files, so having one lightweight, customizable editor that can handle all of them makes it easier to both stay on top of the big picture and quickly dive into any kind of code for additional work.

Initial set up

  1. Head over to sublimetext.com, download and install Sublime Text 3 (ST3) for your operating system.

  2. After installing ST3 it is essential that you also install Package Control, which lets you easily add and remove additional plugins. Just follow these instructions to install it.

That’s it!

Customization

ST3 is a bit bare bones when first installed. However, it is extremely customizable. Below I share all my custom options, which have been thoroughly tested in MacOS, Linux and Windows.

Theme

The whole look of ST3 can be customized by installing themes. I know this is a matter of personal taste, but after trying many (many) themes, my current personal favorite is Monokai Pro. It is dark (of course), minimal and looks great in Windows and MacOS. To install it just invoke the command palette (Tools ‣ Command Palette), type install and hit Return. Then search for Monokai Pro and hit Return again to install it.

Monokai Pro theme and color scheme

Monokai Pro also has a bunch of “color filters” which let you change the overall feel of the theme. You can play around with them by going to Tools ‣ Command Palette ‣ Monokai Pro: select theme.

Although it isn’t visible at first, ST3 comes with a handy sidebar in which you can drop your project folders for easy access, besides listing all your open files. To activate it just go to View ‣ Side Bar ‣ Show Side Bar, and you will see something like the screenshot below:

ST3 sidebar

I also recommend that you install SideBarEnhancements, a plugin that adds a ton of useful functions for manipulating files and folders in the sidebar (you cannot even Move to trash without it!). To install it just invoke the command palette (Tools ‣ Command Palette), type install and hit Return. Then search for SideBarEnhancements and hit Return again to install it.

Development environment

Using Package Control we can easily add plugins that will let us use ST3 to edit and compile the following programs:

32 or 64 bits?

Before you continue, it is important to be sure whether you have a 32 or 64 bit operating system. The answer will determine which version of software you should install: if you have 32 bits, then you must choose 32 bit versions. If you have a 64 bits OS, then you can choose either… but it usually makes more sense to go for 64 bits software as well.

In Windows you can find out your system type by right-clicking on “This PC” (or “My Computer”, in older Windows) and selecting “Properties”. Look for “System type”, like in the screenshot below:

Windows 10 - System properties

On macOS (macOS?), select “About This Mac” from the Apple menu () and you should see a pane like the one below:

About this Mac


R

Getting R to work with ST3 is pretty easy. If you haven’t already, go to the R-project and install the appropriate version for your OS. Once you’ve done that, you need to add R to you environment path. First locate your installation folder, which in Windows is something like

C:\Program Files\R

Inside you should see a folder for every version of R you have in your system. Go into the latest one and locate the bin\x64 or bin\x32 folder, depending on which version of R you installed. For example, the full directory in my Windows system is

C:\Program Files\R\R-3.4.0\bin\x64

To add this path, right-click on My Computer and choose Properties > Advanced (System Settings) > Environment Variables. Edit the Path variable (User or System, your choice) and add a line with the path you just located above. Accept these changes and then fire up the command line, where you should be able to execute R now:

Alvaro@DESKTOP-3U3RLE3 C:\Users\Alvaro
> R

R version 3.4.0 (2017-04-21) -- "You Stupid Darkness"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
...

>

Now you can run R scripts directly from within Sublime. To test it, create a new file with .R extension and write some R code, for example,

seq(1,10)

In order to run this simple script with R you need to choose your build system: go to Tools > Build System and choose R. Now you can select Tools > Build (or just hit Ctrl+B) to run this code! You should see the output on the Console pane of ST, on the bottom of your screen.


Python

Setting up a Python developing environment in ST3 is very easy, and the process is similar to R. First you’ll need to install Python in your machine. For data science, I recommend getting the Anaconda Python distribution.

Python 2.7 or 3.X?

Whether to use Python 2 or 3 is still a hotly debated topic, as a Google search will reveal. My recommendation is to take it easy, because (a) their syntax is fairly similar, (b) you can later switch and (c) the “typical” data science packages work for both versions. So I’d choose whatever version the book you’re using or the colleague you’re working with is using. If you have no reason to choose one or the other, then I’d probably go with Python 3, because that is the future, after all.

Install Python

Download and install the appropriate version of Anaconda Python (the process is fairly straightforward). Once you’re done, check that it is properly installed by firing up the Command prompt (Windows) or the Terminal (macOS) and just write down python. You should see the Python version and the Python shell ready:

Alvaro@DESKTOP-3U3RLE3 C:\Users\Alvaro
> python
Python 2.7.13 |Anaconda 4.3.0 (64-bit)| (default, Dec 19 2016, 13:29:36) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>>

Cool! Now you can use it as a fancy calculator:

>>> 3*2
6

Just type quit() to exit the Python shell.

Run a Python script in ST

Sublime supports Python without any further configuration, so you can just create a new file ending with .py extension and write some Python code:

import random

onetoten = range(10)
random.shuffle(onetoten)
print(onetoten)

You should be seeing the code with syntax highlighting, similar to the above code block. If you don’t, make sure you see “Python” at the bottom right corner of your ST editor window.

Now, in order to build with Python just go to Tools > Build. The output should be visible in the Console pane of ST3, which is in the bottom. If you run into any issues, make sure to check whether Tools > Build System is set to “Python”.

Further enhancements

If you wish to turn ST into a fully featured Python IDE, then you should install the Anaconda plugin (totally unrelated to the Anaconda Python distribution, btw). It enables autocompletion, code linting and a myriad of other fancy stuff.

To install it, just bring up the Command Palette with Ctrl+Shift+P, type “install”, hit Enter and search for “Anaconda”. Just hit Enter again to install, and restart ST to see the changes.


Stata

Getting Stata to work with ST3 requires a few steps, but it works very well when all is said and done. The process is quite different for Windows and macOS, so I’ve split this tutorial in two sections. I’m assuming that you already have Stata up and running (these steps will work with all versions > 10).

Windows

There are two Stata plugins for ST in Windows: StataEditor and Stata Enhanced. I tried both and finally preferred StataEditor, so that’s the one we’ll install and configure.

  1. Install StataEditor: in ST3, start the command palette with Ctrl+Shift+P, type “install” and hit Enter. Now search for StataEditor and just click on it to install.
  2. Install Pywin32: repeat the process described above, now installing the Pywin32 plugin.
  3. Configure StataEditor: In ST3 go to Preferences > Package Settings > Stata​Editor > Settings - User. In this blank file you need to write down the path to your Stata executable and the version. For example, my configuration file in Windows looks like this:

     {
         "stata_path": "C:/Program Files/Stata14/StataMP-64.exe",
         "stata_version": 14,
     }
    
  4. Register Stata Automation library: go to the Stata executable for which you copied the path (e.g. StataMP-64.exe) and create an shortcut for it. Now right click on this new shortcut and select “Properties”. In the “Target” field, add /Register at the end (with a preceding space). For example:

     "C:\Program Files\Stata14\StataMP-64.exe"  /Register
    

    Apply and accept those changes and now right click again on the shortcut and choose to “Run as administrator”. Nothing is going to happen, but if you followed the instructions, it should be ready.

Restart ST3 and try opening an existing do-file (or creating a new one with .do extension). You should read “Stata” on the bottom right corner, and should be able to execute selections or complete do-files with Ctrl+D.

macOS

There are two packages available in Package Control to use ST3 as a Stata editor: Stata Enhanced and Stata Improved Editor. I’ve tried both and prefer Stata Improved Editor, mainly because it has a much finer control on the chunks you can execute. Shout out to Zizhong Yan for incorporating my comments in v1.1.2.

In order to Install Stata Improved Editor in ST3, start the command palette with Cmd+Shift+P, type “install” and hit Enter. Now search for Stata Improved Editor and just click on it to install. Reboot ST3, and that’s it!

CTRL+d sends the current line or the selected chunk to Stata. Also, you can execute the complete do-file with CTRL+SHIFT+d. The plugin has a lot of shortcuts and other nice features, so be sure to check out its repository to make the most out of it.


LaTeX

ST3 can be configured to work as a LaTeX editor pretty easily, specially if you’ve installed one of the “typical” TeX distributions: TeX Live, MiKTeX or MacTeX. If you don’t have LaTeX in your system, I suggest you get either TeX Live in Windows or MacTeX in MacOS. Just ensure you have one of these TeX distributions installed before proceeding.

Besides the LaTeX distribution, another important component is the PDF viewer… and forget about the defaults! On Windows I recommend you get Sumatra (thank me later for this awesome alternative to Adobe’s piece of crap). Just go to https://www.sumatrapdfreader.org/free-pdf-reader.html to download, and then install it.

On macOS I recommend Skim, a lightweight PDF viewer specifically tailored to reading and annotating scientific papers. Head over to http://skim-app.sourceforge.net/ to download and install it. After installing it make sure you go to Skim ‣ Preferences ‣ Sync and set the PDF-TeX Sync support Preset to Sublime, as shown here:

Sync options in Skim

Both of these viewers are super lightweight, work great with LaTeXTools and provide forward and inverse search capabilities, which is really handy (go here to read more about this feature).

Without regard of your operating system, you’ll want to install LaTeXTools, which adds extensive LaTeX support for ST3. Install by invoking the command palette (Tools ‣ Command Palette), type install and hit Return. Then search for LaTeXTools and hit Return again to install it. You should use the same method to install LaTeX-cwl, which allows for autocompletion of LaTeX commands:

LaTeX-cwl provides tab autocomplete

At this point you should run a system check with LaTeXTools, which ensures everything is well configured for LaTeX compilation. To do this, fire up the command palette (Tools ‣ Command Palette), search for LaTeXTools: Check System and hit Return. After a few seconds you should see a new tab with something like the following:

LaTeXTools system check

System is checked and we’re good to go! Don’t panic if ghostscript or magick are unavailable (like above): the former is used for equation and image previewing, while the latter is for previewing non-standard image formats.

So that’s it! You can now open any .tex file or create a new one, and compile it with Ctrl+B. If a dialogue pops up, just choose PDFLaTeX (or whatever you prefer) and LaTeX away!