DEV Community

Apiumhub
Apiumhub

Posted on • Originally published at apiumhub.com on

Stow: Alacritty

I’ve been working on mac for many years with the same terminal: iTerm2. It is well known by the mac community and not for less, a terminal is not that it has much work to do but the little it has to do, it does it very well (tabs, tmux integration, customization).

The problem comes when we go to another operating system. I recently had the pleasure (and it’s not ironic) of working with the Windows environment and its WSL2, and I went crazy to find something similar.

The first thing I needed was to see what I really needed, which came to be the theme and tabs. The theme is something very common in most of the terminals and the tabs, in the modern ones, too. But I added a third element to the equation of what I was looking for, and that was to be cross platform, not having to be configuring N terminals for each OS that I have to use throughout my career, that’s where Alacritty comes in.

Disclaimer: I don’t make intensive use of the terminal, as a developer I spend more time in IntelliJ.

Alacritty is sold as the fastest since it uses OpenGL, and it is true that I have noticed some speed when loading, although it may be placebo, I have no complaints about the speed of iTerm2. It is also a dotfile configurable terminal, a very positive point since I can include it in my dotfile collection.

In this article we are going to see a simple but very functional configuration of the Alacritty terminal in conjunction with ZSH and tmux.

As we have previously mentioned, Alacritty is configured using a dotfile that we will save in our stow directory, so we will create the whole file structure that we will then link in our $HOME with stow.

mnrcuH1xcr

The path we will use to link the configuration will be $HOME/.config/alacritty/alacritty.yml, although you have more directories in the documentation.

$ cd ~/.dotfiles

$ mkdir -p alacritty/.config/alacritty

$ touch alacritty/.config/alacritty/alacritty.yml

Configuring Alacritty

Before configuring anything, Alacritty already has some sensible default settings, among them, the option that the configuration refreshes automatically without having to restart the terminal.

Once the configuration file is created, we will begin to complete it, we will begin with the window section, where we will define a minimum opacity, that it is executed in window mode (not fullscreen) and with a padding to create distance between the text and the border of the terminal:

window:
 opacity: 0.965
 startup_mode: Windowed
 padding:
   x: 8
   y: 8

Enter fullscreen mode Exit fullscreen mode

The next step is the command history that the terminal saves as a cache:

scrolling:
 history: 100000
 multiplier: 3
Enter fullscreen mode Exit fullscreen mode

In History we will indicate the number of lines to save in the buffer and multiplier is the number of lines that we will move in each tick of the scroll.

Now we have to configure the shell that we will launch when Alacritty opens with the following snippet:

shell:
 program: /bin/zsh
 args:
   - --login

Enter fullscreen mode Exit fullscreen mode

The login argument is for the shell to read the configurations inside the user’s home, if it is not specified, it will not read the ~/.zshrc file.

Finally, Alacritty allows you to configure also key bindings to perform actions, I leave here mine (for the moment):

key_bindings:
 - { key: H, mods: Command, action: Hide }
 - { key: Q, mods: Command, action: Quit }
 - { key: V, mods: Command, action: Paste }
 - { key: C, mods: Command, action: Copy }
 - { key: Key0, mods: Command, action: ResetFontSize }
 - { key: Equals, mods: Command, action: IncreaseFontSize }
 - { key: Minus, mods: Command, action: DecreaseFontSize }

Enter fullscreen mode Exit fullscreen mode

Here I don’t think it is necessary to explain anything, since they are normally common to many applications, apart from the fact that the action defines very well what it does without any doubt.

There are some sections within the configuration where you indicate the font-family to use, sizes and colors, being something very personal I will not dedicate a section, below you will have a link to my configuration in case you are interested but it is quite self explanatory.

The perfect complement

If I have to take a downside to Alacritty is the lack of tabs and panels management in a native way, that’s why we have to use external tools like tmux. In this article we are not going to define what tmux is since we already talked about it 5 years ago in another article and it has not changed much since then, it continues doing its function in an excellent way.

Tmux also bases its configuration on a dotfile linked in $HOME/.tmux.conf

Here is my configuration for the keybindings I use on a daily basis (note that my prefix is C-a [Control + A])

# create new window and pane
bind c new-window -c "#{pane_current_path}"
bind-key h split-window -v -c '#{pane_current_path}'
bind-key v split-window -h -c '#{pane_current_path}'

# close window and pane without confirmation
bind-key C-w kill-pane
bind-key & kill-window

# utils
bind-key b break-pane -d
bind-key C-j choose-tree

Enter fullscreen mode Exit fullscreen mode

My day-to-day consists of opening tabs, either vertically (C-a v) or horizontally (C-a h), performing certain actions, and closing them (C-a w). If I see that the tab is too valuable and I want to give it more priority, I move it out to its own window (C-a b). When I finish using the window, I close it (C-a &). If things get complicated, I launch the tree of windows/tabs I have in the current session (C-a C-j) and close them directly from the tree.

There is only one more combination left that I don’t use much, but it’s always nice to have:

# reload tmux C-a + r
bind-key r source-file ~/.tmux.conf \; display-message "~/.tmux.conf reloaded"

Enter fullscreen mode Exit fullscreen mode

By pressing [C-a r] we reload the tmux configuration, avoiding having to log out and log in again.

Alacritty <3 tmux

Until now, we have configured Alacritty on one side and tmux on the other, little relationship they have had, far that one calls the other. Now we enter a part that I have seen a lot of value, especially when connecting with tmux, since it is such a powerful and versatile tool, that people usually have a lot of respect for it, and that is the creation of Alacritty bindings that launch tmux commands.

Here is the complete list of keybindings:

key_bindings:
 - { key: H, mods: Command, action: Hide }
 - { key: Q, mods: Command, action: Quit }

 - { key: V, mods: Command, action: Paste }
 - { key: C, mods: Command, action: Copy }

 - { key: Key0, mods: Command, action: ResetFontSize }
 - { key: Equals, mods: Command, action: IncreaseFontSize }
 - { key: Minus, mods: Command, action: DecreaseFontSize }

 - { key: T, mods: Command, chars: "\x01\x63" } # new window
 - { key: W, mods: Command|Shift, chars: "\x01\x77" } # close window

 - { key: V, mods: Shift, chars: "\x01\x76" } # new pane (vertical)
 - { key: H, mods: Shift, chars: "\x01\x68" } # new pane (horizontal)
 - { key: W, mods: Shift, chars: "\x01\x77" } # close pane

 - { key: B, mods: Shift, chars: "\x01\x62" } # make window from pane
 # move to window 1, 2, 3 with command+n
 - { key: Key1, mods: Command, chars: "\x01\x31" }
 - { key: Key2, mods: Command, chars: "\x01\x32" }
 - { key: Key3, mods: Command, chars: "\x01\x33" }
 - { key: Key4, mods: Command, chars: "\x01\x34" }
 - { key: Key5, mods: Command, chars: "\x01\x35" }
 - { key: Key6, mods: Command, chars: "\x01\x36" }
 - { key: Key7, mods: Command, chars: "\x01\x37" }
 - { key: Key8, mods: Command, chars: "\x01\x38" }
 - { key: Key9, mods: Command, chars: "\x01\x39" }
Enter fullscreen mode Exit fullscreen mode

The chars to send when we press the chosen key combination can be obtained with many tools, personally I have used “xxd”, together with the -xs parameters it starts waiting for an input, it is there where pressing the key combination that we have configured in tmux, we will obtain its hexadecimal value to configure in Alacritty, for example:

M ZC37EO F6x1e96mt8bNQpi0iemyuTkRe05VZMiH cV8Z4RFBrCvJgqXV3vY2XD7OHapciIQqVG 8TX9vQPucBSKlsLXVSteX h6g3TNdSZ3fJaWAMXY5tCQQtNUPe cvpJnbVOeOjNwV00jHNs2Q

Here we can see in the first line that we have pressed “C-a+c + enter”, which is the command that we will launch in tmux to create a new window.

The result would be as follows:

01 C-a

63 c

0a enter

In this case, we have configured the creation of a new window when we press CMD + t (it is not necessary to add the enter in hexadecimal to the command).

 - { key: T, mods: Command, chars: "\x01\x63" } # new window

Enter fullscreen mode Exit fullscreen mode

Top comments (0)