DEV Community

Felix Terkhorn
Felix Terkhorn

Posted on • Edited on

Use Keyboard Shortcut to Focus alacritty in Gnome

Use Keyboard Shortcut to Focus alacritty in Gnome

alacritty is a πŸ”₯ hot, fast terminal emulator πŸ”₯ written in rust. We've been using it for the last 38 microseconds and really enjoy it. It pairs nicely with tmux.

We want to hit a single key and focus on alacritty while using GNOME. If alacritty isn't already open, it should start up. This would give us functionality somewhat equivalent to Guake.

As a bonus, we'll use deno to write a quick helper script and πŸ˜‡ avoid learning bash. πŸ˜‡

Here's what we did to accomplish this task.

Install wmctrl

Install wmctrl so that we can focus on an existing alacritty window using a program.

For debian & ubuntu-flavored linux:

sudo apt install wmctrl
Enter fullscreen mode Exit fullscreen mode

For the redhats:

sudo yum install wmctrl
Enter fullscreen mode Exit fullscreen mode

Start alacritty up for just a moment and take note of its handle as given by wmctrl -xl:

...snip...
0x04800002  1 Alacritty.Alacritty   mybox Alacritty
...snip...
Enter fullscreen mode Exit fullscreen mode

We can then use wmctrl to focus on a running instance of alacritty:

wmctrl -xa Alacritty.Alacritty
Enter fullscreen mode Exit fullscreen mode

Create script to raise or start-up terminal

Create a deno script to either focus on the existing alacritty window, or start a new one:

// saved to /home/nope/bin/raise_alacritty.ts
const p = Deno.run({
    cmd: [ "/usr/bin/pgrep", "alacritty" ]
});

const { code } = await p.status();

if (code === 0) {
    await Deno.run({ cmd: [ "wmctrl", "-xa", "Alacritty.Alacritty" ] }).status();
} else {
    await Deno.run({ cmd: [ "alacritty" ] }).status();
}
Enter fullscreen mode Exit fullscreen mode

Bind keyboard shortcut

Finally, in gnome settings,
add a keyboard shortcut which runs our script.

keybinding

In our case, we assigned the special MENU button on our keyboard to focus on alacritty. We use the default GNOME shortcut (Super H/WindowsKey H) to hide the window when we're done with it.

Updated for deno 1.0.0-rc3

Please note that as of deno 1.0.0-rc3, the command line invocation needed to make this work has changed. You now need to use the run subcommand:

deno run --allow-run /path/to/raise_alacritty.ts
Enter fullscreen mode Exit fullscreen mode

This article was originally published under an older version, which did not require the subcommand.

deno  --allow-run /path/to/raise_alacritty.ts
Enter fullscreen mode Exit fullscreen mode

If you're having trouble getting this to work under the new version of Deno, try adding run!

Top comments (2)

Collapse
 
cnu profile image
Srinivasan Rangarajan

Needs a .status() at the end of both the Deno.run statements. Else the script doesn't wait for subprocess to complete.

Collapse
 
terkwood profile image
Felix Terkhorn

Thank you, I will update it! 🌟