Running Macaulay2 on Visual Studio Code

336 views
Skip to first unread message

Sebastian Calvo

unread,
Aug 16, 2023, 1:42:25 AM8/16/23
to Macaulay2
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: 
  1. 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. 
  2. 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. 
  3. 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. 
  4. 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).
  5. 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). 
  6. 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. 
    1. Press Ctrl+Shift+P to display command palette. Search for Preferences: Open Keyboard Shortcuts (JSON) and select it.  
    2. Copy and paste the following
       {"command": "runCommands",
              "key": "shift+enter",
              "args": {
                  "commands": [
                      "workbench.action.terminal.runSelectedText",
                      "cursorEnd",
                      "cursorWordEndRight"
                  ]
              },
          },
    3. 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 








KSchwede

unread,
Aug 29, 2023, 11:25:08 AM8/29/23
to Macaulay2
Hi Sebastion,

One thing I've frequently done in vscode is to actually run a non-windowed emacs in a terminal in vscode.  Then I can send things to the terminal, but I also have the nice highlighting and other feature of emacs in the instance of Macaulay2 that is running.  


Certainly it would be better if we had a nicer interface that wasn't emacs.

Note also that the Macaulay2 extension you linked does include the alt-enter command for sending a line of code from the text editor to the terminal.

Best wishes,

Karl

Reply all
Reply to author
Forward
0 new messages