I'm used to programming in a terminal and in Vim, and would love the ability to send commands from a terminal window directly to the notebook front end.

Currently, I have to choose between:

  • Cutting and pasting between the terminal and the .nb frontend
  • Using the command line interface which lacks many nicities like native TeX and manipulate
  • Writing a .m file in the editor and then sourcing it in the frontend

This has been asked before, e.g. here, but I'm wondering if there have been any developments that would allow you to send text from a terminal to the .nb frontend in the time since that was posted?


2 Answers 2


Here's a quick demonstration of how something like this could be done:

socket = SocketOpen[1234];
SocketListen[socket, (
   SelectionMove[EvaluationNotebook[], After, Notebook];
   NotebookWrite[EvaluationNotebook[], BoxData@StringTrim[#Data, "\n"], All];
   ) &]

Now, evaluate something like this in the terminal:

echo Print[Range[3]] | netcat localhost 1234

enter image description here

The Mathematica code effectively does the following:

  • Close any existing socket
  • Open a new socket that listens on port 1234 and does the following when receiving a message:
    1. Move the selection to the end of the notebook
    2. Print the contents of the packet that was received as a new cell and select it
    3. Evaluate the cell
  • $\begingroup$ Thanks -- I'm having trouble getting this to work but I think the problem is on my side. I'll poke at it more. $\endgroup$
    – toomey8
    Dec 10, 2022 at 17:03

You can write the command that you want the front end to execute as a notebook file in box form and run it from Terminal, presumably using sudo in OSX. This should do to open the .nb file in Mathematica, (and start Mathematica if it's not already running).

Set the notebook to auto execute with the options here: How do I automatically evaluate a cell when a notebook is opened?

You can obtain a template of the general box form from the plain text of an existing notebook, or generate it from a separate, preliminary kernel operation using ToBoxes.

An alternative is to run a script to open a kernel that opens a front end, such as here & here. It's more long-winded but works with the default option settings. Maybe there are other ways too.

For the auto-executing notebook this is the sort of .nb file that needs to be written. You can try it without box form though. It won't look pretty but it might run.

  RowBox[{"Print", "[", "\"\<Hello\>\"", "]"}], ";"}]], "Input",
WindowSize->{808, 655},
WindowMargins->{{4, Automatic}, {Automatic, 4}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.