Skip to content

Instantly share code, notes, and snippets.

@hugke729
Last active June 29, 2023 15:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugke729/69cf3e433ac55993966ead7ae062f817 to your computer and use it in GitHub Desktop.
Save hugke729/69cf3e433ac55993966ead7ae062f817 to your computer and use it in GitHub Desktop.
Use Sublime Text and Matlab together in a REPL-like fashion

Running an m-file or selected code from Sublime in Matlab's command window

A supplement to the Brushing Up Science post: Invest in a good text editor

I use both Matlab and Sublime Text. Although I edit m-files in Sublime, when I run the file or evaluate lines, I do so in Matlab. Detailed below is how I achieve this in such a way that if I want to run the file or evaluate the selected lines, I simply press Shift + Enter.

This example works for Linux using Autokey. For Windows instructions, click here

Step 1: Autokey code

In AutoKey, save the code below as "Matlab Run File" (note that this file name is referenced in step 2).

You will need to change the Matlab window title accordingly depending on your version (and possibly the shortcut for paste).

# Copy file path using shortcut defined in the sublime-keymap below
keyboard.send_keys('<super>+o')
# Switch to the Matlab window
window.activate('MATLAB R2016b - academic student use')
time.sleep(0.1)
# Focus the Command Window
keyboard.send_keys('<ctrl>+0')
time.sleep(0.1)
# Paste the file name surrounded by run('  ')
keyboard.send_keys("run('")
time.sleep(0.1)
keyboard.send_keys('<ctrl>+v')
time.sleep(0.1)
keyboard.send_keys("')")
# And run
keyboard.send_keys('<enter>')

Save a second file entitled "Run matlab selection" with the following content:

# Copy the selected text
keyboard.send_keys('<ctrl>+c')
time.sleep(0.2)
# Switch to the Matlab window
window.activate('MATLAB R2016b - academic student use')
time.sleep(0.1)
# Focus the Command Window
keyboard.send_keys("<ctrl>+0")
time.sleep(0.1)
# Paste and evaluate
keyboard.send_keys("<ctrl>+v")
time.sleep(0.1)
keyboard.send_keys("<enter>")

Step 2: Sublime Text preferences

In Sublime Text, add the following to the User Keymap (Preferences > Key Bindings)

(Note that "super+o" is an arbitrary shortcut that is unlikely to be in use, but required in the AutoKey files above)

{
    "keys": ["super+o"],
    "command": "copy_path"
},
{
    "keys": ["shift+enter"],
    "command": "exec",
    "args": {"cmd": ["autokey-run", "-s", "Matlab Run File"]},
    "context": [{"key": "selector", "operator":
                 "equal", "operand": "source.matlab"}]
},
{
    "keys": ["shift+enter"],
    "command": "exec",
    "args": {"cmd": ["autokey-run", "-s", "Run matlab selection"]},
    "context": [{"key": "selection_empty",
                 "operator": "equal",
                 "operand": false},
                {"key": "selector",
                 "operator": "equal",
                 "operand": "source.matlab"}]
}

The file above will need square brackets around the file if nothing else exists in the keymap file. See the default keymap for an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment