Skip to content

Instantly share code, notes, and snippets.

@avindra
Last active November 8, 2024 23:14
Show Gist options
  • Save avindra/dd2c6f14ec6e03b05261d370ef60c9d8 to your computer and use it in GitHub Desktop.
Save avindra/dd2c6f14ec6e03b05261d370ef60c9d8 to your computer and use it in GitHub Desktop.
Reset Terminal

Introduction

To reset your terminal, you can call reset from your shell, which is generally included with the target operating systems.

If you want to reset when you are not at a shell prompt (i.e., inside some other application), we can send an escape sequence in another way.

As an example, we can send a special escape sequence to the Nth tty:

echo -e "\ec" > /dev/pts/$N

Since Alacritty supports keybindings to execute arbitrary scripts, it is possible to map this reset action to some key combination.

This can be 1️⃣ wrapped into a script, which 2️⃣ Alacritty can be configured to execute.

1️⃣ Create script

You can use the script below to send the desired escape code.

ℹ️ First, the script extracts the PID of Alacritty from a known environment variable, ALACRITTY_LOG.

ℹ️ ALACRITTY_LOG has the form /tmp/Alacritty-1234.log, where 1234 corresponds to the process ID of Alacritty.

Copy the script to some well known location, for example bin in your home directory.

#!/bin/sh
if [ -z "${ALACRITTY_LOG}" ]; then exit 1; fi

TERM_PID="${ALACRITTY_LOG//[^0-9]/}"
tty=$(ps o tty= --ppid $TERM_PID)

echo -e "\ec" > /dev/$tty

2️⃣ Configure alacritty

In your alacritty.yml config, add a keybind to the script. For example, to bind CTRL+K to the reset action, add the following to the key_bindings section:

- { key: K,  mods: Ctrl,  command: path/to/reset-alacritty }

🎉 And you're all set. The rest of this doc just contains some info for the curious.

The config can be just the name of the script if your PATH is set load your expected binaries correctly.

- { key: K,  mods: Ctrl,  command: reset-alacritty }

A general solution

If you are on Linux and using X11, this alternative script using xdotool should be compatible with any given terminal emulator.

#!/bin/sh

focus_pid=$(xdotool getactivewindow getwindowpid)

tty=$(ps o tty= --ppid $focus_pid)

echo -e "\ec" > /dev/$tty

References

  1. alacritty/alacritty#4152 (comment)
  2. alacritty/alacritty#4199 (comment)

Discussions

  1. alacritty/alacritty#4494
  2. alacritty/alacritty#3997

Version History

@jtheoof
Copy link

jtheoof commented Sep 6, 2021

Thanks for this @avindra.

I changed the xdotool to a more sway friendly approach in my setup:

❯ bat ~/.local/bin/reset-active-tty
───────┬─────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: /home/jattali/.local/bin/reset-active-tty
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ #!/bin/sh
   2   │
   3   │ focus_pid=$(swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true).pid')
   4   │
   5   │ tty=$(ps o tty= --ppid $focus_pid)
   6   │
   7   │ echo -e "\ec" > /dev/$tty
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────

And my alacritty config:

❯ bat ~/.config/alacritty/alacritty.yml | rg reset-active-tty
  - { key: K,          mods: Control, command: { program: "sh", args: ["-c", "~/.local/bin/reset-active-tty"] } }

@artslob
Copy link

artslob commented Sep 12, 2022

This script almost works for me, thanks! The only problem I have right now is that when I execute this script with key binding or from another terminal, my target terminal is completely cleared, cursor is on first line in first column and prompt is not shown. In this case I need to hit Enter to get prompt or resize window or hit Ctrl-L. However if I run this command in target terminal as usual command then prompt is shown. Does anyone know how to make prompt appear?

Alacritty looks like this:
image

@tecfu
Copy link

tecfu commented Feb 22, 2023

@sakoula
Copy link

sakoula commented Mar 10, 2023

this is really amazing! I was looking for this! The only thing I am missing is that if I am not in a tail -f mode the prompt does not come back and I need to hit enter to do it. Is there a way to do that by echoing something to the device? I tried but could not really figure it out.

Thanks!

@avindra
Copy link
Author

avindra commented Mar 13, 2023

Hitting Enter after blanking the screen is necessary. There's probably a way to do that automatically but I'm not aware of the method.

@insilications
Copy link

If you want to avoid hitting enter, do the following. Setup everything according to this gist, but add a key binding action to the same key that sends a byte sequence to the running application using chars. According to the docs:

If the same trigger is assigned to multiple actions, all of them are executed in the order they were defined in.

So:

- { key: K,  mods: Ctrl,  command: path/to/reset-alacritty } 
- { key: K,  mods: Ctrl,  chars: "\x0d" } 

This will send/press enter right after the reset-alacritty command.

@artslob
Copy link

artslob commented Aug 22, 2023

@insilications This works for me! ⚡ Thanks, you are a genius! 👍

@icatalina
Copy link

it depends on what you are looking for, but this shortcut is good enough for me:

  - { key: K, mods: Ctrl, action: SpawnNewInstance }
  - { key: K, mods: Ctrl, action: Quit }

@axgkl
Copy link

axgkl commented Feb 19, 2024

This rocks. I was about to leave alacritty because of that, plus the main guys attitude regarding this MUST HAVE feature for everybody who has to work with logging applications.
Massive thanks. Whew... I was about to send ctrl-z before and fg+enter after the built in clearing sequence and I hated myself to have invested too much into wrong terminal ;-) Now I'm sort of fine again with it.

@electriquo
Copy link

the script does not work on mac. is there any mac version or alternative?

@schoesa
Copy link

schoesa commented Jul 22, 2024

This also does not work on my Mac. I have exactly followed the steps and created an executable script. But nothing happens if I enter the key binding.

@dxlr8r
Copy link

dxlr8r commented Sep 19, 2024

I execute alacritty ! (alacritty msg create-window) && alacritty, meaning ps o tty= --ppid gives multiple outputs. Meaning the workaround does not work in this case :/

@dxlr8r
Copy link

dxlr8r commented Sep 20, 2024

My workaround, put somewhere in your PS1 (prompt):

printf '\033]0;%s\007' "$USER@$HOSTNAME - $(ps -p $$ -o tty=)"

This will add your tty to the title of the window. Then read the title of the focused window, extract the tty, and send clear:

#!/bin/sh
if [ -z "${ALACRITTY_LOG}" ]; then exit 1; fi
tty=$(swaymsg -t get_tree | jq -r '.. | select(.type?) | select(.focused==true).name' | awk -F "-" '{print $NF}' |  tr -d ' ')
echo -e "\ec" > "/dev/${tty}"

@irees
Copy link

irees commented Nov 8, 2024

Absolutely blows my mind that a standard feature I've used in terminal emulators for OVER TWENTY YEARS is a "closed, wontfix" by the dev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment