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

6,430

Solution 1

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!).

Solution 2

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.

Solution 3

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.

Solution 4

besides pa4080's answer above to use tmux, I'd like to suggest an extended version:

Give tmate a try. Its available as Ubuntu package as well. While it can be used as tmux replacement, it could do more: I can replicate your terminal session using a tmate public server accessible via ssh or http (read-only or full share). So your students could use either your beamer projection or have a read only view of your terminal session directly in their browser on their personal computer / tablet etc.

Share:
6,430

Related videos on Youtube

Jander
Author by

Jander

Updated on September 18, 2022

Comments

  • Jander
    Jander over 1 year

    I teach using a two-monitor setup, so what I see in my desktop is different from what my audience can see. This works fine for presentations: while the students see the slides, I can see my notes.

    But I want to use a terminal to run a shell. However, the terminal has to be on my desktop OR on the external screen.

    Is there a way to show the terminal contents in both screens simultaneously? I don't want to mirror the screens, but stick to the two-monitor mode.

    Any hints?

    I'm using Ubuntu with Gnome.

    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy about 6 years
      Well, you can mirror your desktop and show the terminal emulator, that's one way, if you just want to show terminal in any way so long as it works. Are you talking about TTY specifically ? Then the two already existing answers for screen and tmux can work. Really depends on your needs
    • psmears
      psmears about 6 years
      Not a duplicate, because this question is specifically about terminals which means there are other options (eg screen, tmux) available, but this question is very closely related and the answer there should work for this problem too.
    • Jander
      Jander about 6 years
      @Sergiy Kolodyazhnyy: I don't want to mirror the desktop because it messes up with the presentation. When I switch back to separate screens, the slides don't fit the screen anymore; sometimes the slides go to my desktop. tmux is working fine for me.
  • Jander
    Jander about 6 years
    Sorry I can't choose two answers as the correct one...
  • vidarlo
    vidarlo about 6 years
    That's perfectly fine:) I've upvoted the other answer myself :)
  • leftaroundabout
    leftaroundabout about 6 years
    Wow, interesting! The astonishing thing is that this seems to handle up-history browsing, deletions, colours... flawlessly. Wouldn't have thought this was possible with tail. (I think tmux is still better in practice, because it also has no problem with e.g. an editor session.)
  • Jander
    Jander about 6 years
    Really interesting approach! It can handle window size changes, while tmux and screen cannot, as far as I can tell.
  • scohe001
    scohe001 about 6 years
    @leftaroundabout the magic here is actually just how little tail is really doing. If you look at a script file with something like vim or less you'll see it adding ugly escapes since it's trying to control your screen scrolling and character placement.
  • scohe001
    scohe001 about 6 years
    @leftaroundabout by "editor session" do you mean something like vim? As far as I can tell, this seems to work fine (as long as the presentation terminal is bigger than the personal terminal)
  • pa4080
    pa4080 about 6 years
    tmate looks interesting. Here is a simple demo: youtube.com/watch?v=is_VpIx3Z4M