Hi all.
In an effort to widen my coding repertoire, I have started playing around in VS code. As a source-code editor for many languages, I wanted to see if I could run Macaulay2 on VS code. Here is how to set it up.
Prerequisites:
- (Windows) A functional WSL2 with M2 already installed. I personally use latest Ubuntu-22.04.
Steps:
- Upon installing VS code, it will notice you have WSL installed on your system and ask you to install the WSL extension. Continue to install this extension.
- In the bottom left hand corner, press the blue icon to connect to your WSL2. This tells VS code that the default terminal you want to use is WSL2 rather than Powershell or Command Prompt.
- Press Ctrl+Shift+` to open a New Terminal. This will open up your WSL2. At this point, you could use your WSL2 as normal. For example, you could run emacs and press f12 to start Macaulay2. Instead, simply start M2 in WSL2.
- With your cursor in your workspace (as opposed to the terminal), press Ctrl+Windows+Alt+N to create a new file. Select Text File for now (this is like opening up Notepad on Windows or Emacs on Linux).
- To send the line of code to the terminal, we have to keybind this action just as we would with f11 in emacs to send a line of code to M2. Press Ctrl+Shift+P to display the command palette. Search for Terminal: Run Selected Text In Active Terminal and press the configuration on the right hand side of the palette. Right click the command and select Change Keybinding... (I personally use Shift+Enter, similarly to the Macaulay2Web but I had to remove/replace previous commands that used Shift+Enter).
- Return to your open text file and your keybinding should now work.
A few quality of life changes that are essential:
- The extension Macaulay2 makes VS Code recognize Macaulay2 as a language. Once installed, VS Studio code can recognize it by pressing Ctrl+K then M and select Macaulay2. If done successfully, Macaulay2 will be displayed in the bottom right hand corner.
- Executing your
Terminal: Run Selected Text In Active Terminal keybind will not send your cursor to the next line as it would in Emacs or Macaulay2Web.
The following is to set up a command that consolidates sending the code to your terminal and moving to the next line to a single keybind.
- Press Ctrl+Shift+P to display command palette. Search for Preferences: Open Keyboard Shortcuts (JSON) and select it.
- Copy and paste the following
{"command": "runCommands", "key": "shift+enter",
"args": {
"commands": [
"workbench.action.terminal.runSelectedText",
"cursorEnd",
"cursorWordEndRight"
]
},
},
If you would prefer a keybinding other than Shift+Enter, you can modify the input of "key": " ____".
This (probably) isn't the cleanest way to set up Macaulay2 in VS code but it works. If someone has any suggestions to clean this up, please let me know.
Cheers,
Sebastian