Skip to content

Using StataEditor

Below are the keyboard shortcuts and code snippets that come with this plugin. At the bottom there is a video demo.

Keyboard shortcuts

Shortcut Description
Nothing selected, Ctrl+D Run the entire do file
Some lines/characters selected, Ctrl+D Run the selected lines only. If any part of the line is selected, the entire line is included
Ctrl+R Goto Symbol. A panel will appear where you can navigate through different code sections within the do file
Code snippet trigger+Tab Use code snippet

Code snippets

A code snippet is a reusable code block. You can find all snippets in Sublime Text > Tools > Snippets. For example, to use the snippet for -forvalues-, type forv and press Tab, and you will see

forvalues i = 1(1)n {

}

The cursor now selects the start index 1. To change it, simply type in the desired number. Then press Tab, and the cursor will jump to the 1 inside the parenthesis, where you can change the step size. Press Tab again to move the cursor to n, where you can specify the end index. Finally, press Tab one last time to jump inside the curly brackets for the main loop body. This entire process can be completed without using a mouse or arrow keys!

Auto-completion

The package comes with variable, command, function, and filename auto-completion. All of them support fuzzy match. Variable refers to all variables in the .dta file we currently open. Command and function find stuff like -log-, -clear-, -logit-, etc.

Imperfect fuzzy match

Sublime Text's fuzzy match algorithm (and in fact the entire Sublime Text) is not open source. So it may not work as you expected. My personal experience is to fuzzy match “sequentially”. E.g. to match “auto-completion”, it's fine to type “atpn”, since “t” is after “a”, “p” is after “t”, and so on. However, if you type “apcu”, though all of the four letters are in the word, Sublime Text is not able to find “auto-completion” for you. That being said, in most cases, however you type it, Sublime Text can always find the word for you!

If you are interested in the implementation of fuzzy match algorithm, here is a wonder blog to reverse engineer Sublime Text's fuzzy match. Moreover, tajmone/fuzzy-search collects a set of useful fuzzy search algorithm.

Filename auto-completion is slightly more complicated. In the initial folder and all subfolders, the package recursively gets all files with certain extensions, and save their path in a list, when you establish the initial Stata connection from Sublime Text. Then, during your coding, it turns to the list and do (fuzzy) completion for you.

Detailed explanation

The “initial” folder refers to the directory containing the do file where you run the initial line(s). In the example project folder structure below, suppose you open some_code.do and run the first lines there, the file auto-completion will only suggest files located within code folder, and files like something.docs will not appear in the list.

root directory
├── something.docs
├── main_do_file.do
├── data folder
│   │
│   ├── your_data.dta
│   └── a sub data folder
│       │
│       ├── data_a.xlsx
│       └── data_b.xlsx
└── code folder
    └── some_code.do

The file list contains all files with certain extensions. You may want all .dta files, all .xlsx spreadsheets, and so forth. But it's less likely that your code involves .docs. You can change the extensions in Sublime Text > Preferences > Package Settings > StataEditor > Settings – User. You can add/delete extensions in file_completions. For example, to include .exe and .rtf, your setting file looks like:

{
"ensure_newline_at_eof_on_save": true,
    "stata_path": "C:/Program Files/Stata18/StataMP-64.exe",
    "stata_version": 18,
    "character_encoding": "utf-8",
    "variable_completions": true,
    "function_completions": true,
    "command_completions": true,
    "default_path": "current_path",
    "file_completions": "exe, rtf",
    "waiting_time": 0.5
}

The auto-completed path will be relative to your initial folder. To illustrate it, we use the same example folder structure. If the initial Stata connection is established when running main_do_file.do, then the auto-completed path for data_a.xlsx will be data folder/a sub data folder/data_a.xlsx.

root directory
├── something.docs
├── main_do_file.do
├── data folder
│   │
│   ├── your_data.dta
│   └── a sub data folder
│       │
│       ├── data_a.xlsx
│       └── data_b.xlsx
└── code folder
    └── some_code.do

Code snippets has the highest priority. Suppose you have a variable called forgive, a command called -forvalues-, a file called forever.csv, and the built-in forv code snippet. The forv code snippet will pop up when you type “forv” and press Tab. So to match the file forever.csv, you may type something like fve, which avoids forv snippet trigger.

Memory leak

As you can imagine, if your folder has hundreds of thousands of files, it can be slow and lead to memory leak. My personal experience is, as long as the number of files is at the level of thousands, it works efficiently without any issues.

When you establish the “initial” Stata connection

One shortcoming is that the file list never gets updated, unless you shutdown Sublime Text and Stata, and re-open them. For example, if you've opened Stata, run a few lines, and created a new .dta. This new .dta will not appear in the file auto-completion list, until you launch Sublime Text again.

I'm working on this. Suggestions welcome!

Video demo

Below is a video demo for summary statistics, scatter plot, and regression.