Skip to content

Instantly share code, notes, and snippets.

@avindra
Last active February 19, 2024 09:35
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

@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