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

@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