Skip to content

Instantly share code, notes, and snippets.

@avindra
Last active February 19, 2024 09:35
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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

@avindra
Copy link
Author

avindra commented Nov 28, 2020

Addendum: in fact, Alacritty supports this feature 👍 (it responds correctly to the escape code, which results in all of scrollback being destroyed, in addition to any data on the screen).

The CTRL+K you may be used to from iTerm2, gnome-terminal and other emulators now works in Alacritty 👌 if you use the workaround described.

Ideally, configuration should be as simple as configuring the escape code directly in the yml file.

But for some reason (likely an implementation difference), Alacritty does not respond to the clearing code correctly (via its own/standard config).

@samodadela
Copy link

Best workaround ever... don't know why this is alacritty does not add has it by default. It took longer to debate the "why not" than to code a solution :P

@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.

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