Alex Shum bio photo

Alex Shum

Data Scientist at Apple.

Twitter LinkedIn Github Stackoverflow

Summary of steps to setup sublime for use with R, Python, Java and C.

Command Line

For OS X, to work with files on the command line:

ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/usr/bin/subl

Then to open text files: subl file.txt.

Finally edit .profile. and add export EDITOR='subl -w'. This will specify to use sublime for commands that prompt for input.

Package Control

The command to install package control might change with later versions. See here.

To install package control for sublime open the console with ctrl + (`) and paste the following python code:

import urllib.request,os,hashlib; h = '7183a2d3e96f11eeadd761d777e62404' + 'e330c659d4bb41d3bdf022e94cab3cd0'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

Interpreted languages

command + shift + p (command palette) and open package control and install REPL.

To open an instance of python/r open the command palette and find SublimeREPL: Python or SublimeREPL: R. This will open a python/R console inside sublime.

It might be helpful to change the key bindings to send code to R/python code directly to the interpreter. Key bindings can be found using the command palette or under preferences.

// Executes a selection of text in REPL
{ "keys": ["control+shift+j"], "command": "repl_transfer_current", "args": {"scope": "selection"}},

// Executes the entire file (build) in REPL
{ "keys": ["control+shift+k"], "command": "repl_transfer_current", "args": {"scope": "file"}},
   
// Executes line(s) of text in REPL terminal
{ "keys": ["control+shift+l"], "command": "repl_transfer_current", "args": {"scope": "lines"}}

The above code will send and execute a code selection, the entire file and a line of code respectively. It’s possible to send the code without executing by including "action":"view_write" under args. Example:

{ "keys": ["control+shift+j", "r"], "command": "repl_transfer_current", "args": {"scope": "selection", "action":"view_write"}}

Linking with R

Install R-box using command palette. REPL links sublime to R but the R-Box package also integrates knitr documents and Rcpp. R-Box allows sublime to send r code to the R-gui window directly.

Compiled languages

Sublime has an extendable build system. It’s possible to compile a language with command + b and compile and run a language with command + shift + b. To add a new build system, go to tool > build system > new build system. The following will add C to the build system:

{
  "cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
	"selector" : "source.c",
	"shell": true,
	"working_dir" : "$file_path"
}

The following will add C to the build system and allow for both compile only and compile/run in a single key combination.

{
  "cmd" : ["gcc", "-o", "$file_base_name", "$file_name"],
	"selector" : "source.c",
	"shell" : false,
	"working_dir" : "$file_path",

	"variants" :
	[
		{
			"name" : "Run",
			"cmd" : ["./$file_base_name"]
		}
	]
}

Sublime over SSh

Sublime has a feature to edit files on a remote server based on rmate; rmate provides similar ssh access for textmate. This feature requires the rsub plugin in sublime and requires a script on the server. It’s possible to use this even without sudo access on the server.

From textmate’s github repo download the rmate ruby script under bin/rmate – it might be helpful to rename the script to rsub for sublime. Put this on the server under ~/bin or /usr/local/bin. Set the correct port under rsub settings in sublime. To test if this is correctly setup, SSH into the server using: ssh -R 52698:localhost:52698 user@address.com. Then once on the server rsub filename, this should open sublime on your local computer.

Quick summary here.