Skip to content

Instantly share code, notes, and snippets.

@hugke729
Last active July 30, 2021 14:31
Show Gist options
  • Save hugke729/56c70bf9603e708a4c9dadcd928e08ce to your computer and use it in GitHub Desktop.
Save hugke729/56c70bf9603e708a4c9dadcd928e08ce 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 Windows using AutoHotKey. For Linux instructions, click here

Step 1: Autokey code

An AutoHotKey file is simply a text file with a .ahk extension. Save the code below into a file named matlab_run_file.ahk. In part 2, I assume that this file is saved in my Documents folder.

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
Send #+o
Sleep 100

; Get the just copied path name, divide it into parts and take last part without .m
filename =  %clipboard%
SplitPath, filename, name, dir, ext, name_no_ext, drive
clipboard = %name_no_ext%
Sleep 100

WinActivate, <Student Version> MATLAB
; Send ^0 ; Focus the Command Window
Sleep 200
Send, ^v ; Paste the name that's now in the clipboard
Sleep 100
Send {Enter}

Save a second file entitled matlab_run_selection.ahk with the following content:

Send ^c ; Copy the selected text
Sleep 100
WinActivate, <Student Version> MATLAB
Sleep 100
Send ^0 ; Focus the Command Window
Sleep 100
Send ^v ; Paste the selected text
Sleep 100
Send {Enter}

Note that certain versions of Matlab may not accept the keystrokes sent by AutoHotKey. Hence, the command window focus and paste steps will not work. I'm not sure of a work-around.

Step 2: Sublime Text preferences

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

(Note that super+shift+o is an arbitrary shortcut that is unlikely to be in use, but required in one of the AutoHotKey files above)

Be sure to change the file paths for the AutoHotKey scripts. I've left C:/Users/Ken/Documents/matlab_run_file.ahk in as an example.

{
    "keys": ["super+shift+o"],
    "command": "copy_path"
},
{
    "keys": ["shift+enter"],
    "command": "exec",
    "args": {"cmd": ["C:/Program Files/AutoHotKey/AutoHotKey.exe", "C:/Users/Ken/Documents/matlab_run_file.ahk"]},
    "context": [{"key": "selector", "operator":
                 "equal", "operand": "source.matlab"}]
},
{
    "keys": ["shift+enter"],
    "command": "exec",
    "args": {"cmd": ["C:/Program Files/AutoHotKey/AutoHotKey.exe", "C:/Users/Ken/Documents/matlab_run_selection.ahk"]},
    "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.

@LINCH8
Copy link

LINCH8 commented Mar 17, 2019

Can you write an example for mac os?

@hugke729
Copy link
Author

@jarvisbro. Can't help sorry. Not at all familiar with Macs

@swun90
Copy link

swun90 commented Dec 27, 2019

Hi! Is it known by everyone so you guys just use / in windows for fun? In case some rookies see this, use \\ instead

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