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
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>")
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.