How to have a terminal mirrored onto a second screen in a two-monitor setup?

One elegant way is to use tmux for this task: sudo apt install tmux. Here is an example:

  • Create a session called my_session (remove -d to attach during the creation):

    tmux new-session -d -s my_session
    
  • Open one or more new terminal windows and attach to the same session:

    tmux attach -t my_session
    
  • To detach from a session type:

    tmux detach
    

    Or press Ctrl+b, then release Ctrl and press d.

  • To send a command to the session without attaching to it:

    tmux send-keys -t my_session "echo Hello World!" ENTER  Enter
  • Note the exit command, executed from inside, will close the session.


Can't think of any way to directly achieve what you want - perhaps others can. But I can think of a workaround.

Install screen with sudo apt install screen. Start two terminals. In the first one, type screen and press enter at the nag screen you get. In the second one, type screen -x.

They will effectively show the same content. It's not the same terminal window, but it will be the same content.

Screen can do more tricks as well, such as multiple windows that you can switch between. This is a quick tutorial of the features available.


The Portable Solution

Use script! For example:

Personal terminal:

> script -f /tmp/lecture1.scrpt #use -F instead on MacOS
> ... #start doing things here!

Presentation terminal:

> #after this, terminal will continuously print whatever's written to personal terminal
> tail -F /tmp/lecture1.scrpt

How It Works

The script command copies everything written to the terminal screen (including what you type!) into a file it takes as a parameter. Ordinarily everything gets written to the file after you end the script (by typing exit). However, the -f option causes script to flush its buffer after every write (on MacOS, this will be -F or -t 0). Then, in the presentation terminal, you can use tail -F to see the contents continuously as they're written.


Things to Note

  • Since one terminal is writing to a file and the other is reading, this can be done between different users! This means you can have someone ssh in with very few permissions and as long as you place the script file in a location they can read, you'll still be able to present to them. (ie: if you have a server your students have access to, you could create a .scrpt file that'd be only readable for them so they could follow along on their own screens)

  • Given the nature of this method, one terminal is driving and the other is only watching.

  • This method also has the added bonus of making it easy for you to stop mirroring, do some secret work and begin mirorring again all without leaving your personal terminal. This can be done with the following:

Personal terminal:

> exit #end script session; stop writing to /tmp/lecture1.scrpt
> ... #do secret things not safe for student eyes!
> script -f -a /tmp/lecture1.scrpt #begin writing again with -a to append

More Fun With script!

The purpose of script is to record your terminal session so it can be played back later (we just happen to be the special case of playing back as it's recording). To help with this, script has the -t option to record timing along with what's written to the screen. To use it, start your script session with:

> script -f -t 2>/tmp/lecture1.timing /tmp/lecture1.scrpt

And play it back (with timing!) with:

> scriptreplay -t /tmp/lecture1.timing -s /tmp/lecture1.scrpt

Have a student that emailed you saying he'd be sick and can't make lecture? Or just want to give your students more lecture material? If you record your voice during the lecture (and start script at about the same time as the recording), then your students could play back your terminal session with your voice and get the full lecture experience!

Have a student who likes to play all of his videos at 2x speed? scriptreplay takes a "divisor" that it multiplies the play speed by! Just pass -d 2 to play at 2x speed (note this is a double value, so you could even do -d .5 for half speed!).