DEV Community

Cover image for Boost Your Linux Productivity: Remapping Useless Keys with Kanata
Shanu Kumawat
Shanu Kumawat

Posted on

Boost Your Linux Productivity: Remapping Useless Keys with Kanata

The Problem: The Useless Caps Lock Key

We've all been there - accidentally hitting the Caps Lock key and suddenly shouting at our screens in ALL CAPS. This relic from the typewriter era takes up prime keyboard real estate but offers little value in return. What if we could transform this useless key into a productivity powerhouse?

Enter Kanata: Your Key Remapping Solution

Kanata is a powerful, cross-platform key remapper written in Rust. It allows you to redefine your keyboard layout at a low level, opening up a world of possibilities for customization and efficiency. With Kanata, we can:

  1. Remap the Caps Lock key to serve dual purposes:
    • Tap for Escape (perfect for Neovim users)
    • Hold for Control (easier access to common shortcuts)
  2. Transform the Tab key into a symbol layer accessor:
    • Tap for normal Tab functionality
    • Hold to access a custom layer of symbols and navigation keys

Let's dive into how we can achieve this setup.

Step-by-Step Guide

1. Install Kanata

First, you'll need to install Kanata. The installation process varies depending on your Linux distribution. Check the official Kanata repository for detailed installation instructions.

2. Create Your Kanata Configuration File

Create a new file named kanata.kbd in your home directory. This is where we'll define our custom keyboard layout.

3. Define Your Source Layer

Start by defining your source layer. This represents your physical keyboard layout:

(defsrc
esc  f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12
grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
tab  q    w    e    r    t    y    u    i    o    p    [    ]    
caps a    s    d    f    g    h    j    k    l    ;    '    ret
lsft z    x    c    v    b    n    m    ,    .    /    rsft
lctl lmet lalt           spc            ralt rmet rctl
)

Enter fullscreen mode Exit fullscreen mode

This layout represents a standard QWERTY keyboard. Adjust it if your physical layout differs.

4. Create Custom Aliases

Next, we'll define aliases for our custom key behaviors:

(defalias
cec (tap-hold 200 200 esc lctl)
sym (tap-hold 200 200 tab (layer-toggle symbols))
)
Enter fullscreen mode Exit fullscreen mode

Here's what these aliases do:

  • cec: Caps as Escape (tap) and Control (hold)
  • sym: Tab as normal Tab (tap) and Symbol layer toggle (hold)

The numbers (200 200) represent the tap and hold timeouts in milliseconds.

5. Define Your Default Layer

Now, let's create our default layer, incorporating our new aliases:

(deflayer default
esc  f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12
grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
@sym q    w    e    r    t    y    u    i    o    p    [    ]    
@cec a    s    d    f    g    h    j    k    l    ;    '    ret
lsft z    x    c    v    b    n    m    ,    .    /    rsft
lctl lmet lalt           spc            ralt rmet rctl
)
Enter fullscreen mode Exit fullscreen mode

Note how we've replaced tab with @sym and caps with @cec.

6. Create Your Symbol Layer

Finally, let's define our symbol layer:

(deflayer symbols
  _    _    _    _    _    _    _    _    _    _    _    _    _
  _    S-1  S-2  S-3  S-4  S-5  S-6  S-7  S-8  S-9  S-0  _    _    _
  _    S-5  S-6  S-7  S-8  _    _    _    _    S-9  S-0  _    _    _
  _    _    _    del  _    _    left down up   rght _    _    _
  _    _    _    _    _    _    _    _    _    _    _    _
  _    _    _              _              _    _    _
)

Enter fullscreen mode Exit fullscreen mode

In this layer:

  • S- represents Shift, so S-1 outputs !, S-2 outputs @, etc.
  • We've added arrow key functionality to h, j, k, l for vim-style navigation
  • del is now easily accessible on the home row

So at the end you config should look like this:

(defsrc
  esc  f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12
  grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [    ]    \
  caps a    s    d    f    g    h    j    k    l    ;    '    ret
  lsft z    x    c    v    b    n    m    ,    .    /    rsft
  lctl lmet lalt           spc            ralt rmet rctl
)

(defalias
  cec (tap-hold 200 200 esc lctl)
  sym (tap-hold 200 200 tab (layer-toggle symbols))
)

(deflayer default
  esc  f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12
  grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
  @sym q    w    e    r    t    y    u    i    o    p    [    ]    \
  @cec a    s    d    f    g    h    j    k    l    ;    '    ret
  lsft z    x    c    v    b    n    m    ,    .    /    rsft
  lctl lmet lalt           spc            ralt rmet rctl
)

(deflayer symbols
  _    _    _    _    _    _    _    _    _    _    _    _    _
  _    S-1  S-2  S-3  S-4  S-5  S-6  S-7  S-8  S-9  S-0  _    _    _
  _    S-5  S-6  S-7  S-8  _    _    _    _    S-9  S-0  _    _    _
  _    _    _    del  _    _    left down up   rght _    _    _
  _    _    _    _    _    _    _    _    _    _    _    _
  _    _    _              _              _    _    _
)

Enter fullscreen mode Exit fullscreen mode

7. Run Kanata

Save your kanata.kbd file and run Kanata:

kanata -c /path/to/your/kanata.kbd
Enter fullscreen mode Exit fullscreen mode

Instructions

In Linux, kanata needs to be able to access the input and uinput subsystem to inject events. To do this, your user needs to have permissions. Follow the steps in this page to obtain user permissions.

1. If the uinput group does not exist, create a new group

sudo groupadd uinput
Enter fullscreen mode Exit fullscreen mode

2. Add your user to the input and the uinput group

sudo usermod -aG input $USER
sudo usermod -aG uinput $USER
Enter fullscreen mode Exit fullscreen mode

Make sure that it's effective by running groups. You might have to logout and login.

3. Make sure the uinput device file has the right permissions.

Create a new file:

/etc/udev/rules.d/99-input.rules

Insert the following in the code

KERNEL=="uinput", MODE="0660", GROUP="uinput", OPTIONS+="static_node=uinput"
Enter fullscreen mode Exit fullscreen mode

Machine reboot or run this to reload

sudo udevadm control --reload-rules && sudo udevadm trigger
Enter fullscreen mode Exit fullscreen mode

Verify settings by following command:

ls -l /dev/uinput
Enter fullscreen mode Exit fullscreen mode

Output:

crw-rw---- 1 root date uinput /dev/uinput
Enter fullscreen mode Exit fullscreen mode

4. Make sure the uinput drivers are loaded

You may need to run this command whenever you start kanata for the first time:

sudo modprobe uinput
Enter fullscreen mode Exit fullscreen mode

5a. To create and enable a systemd daemon service

Run this command first:

mkdir -p ~/.config/systemd/user
Enter fullscreen mode Exit fullscreen mode

Then add this to: ~/.config/systemd/user/kanata.service:

[Unit]
Description=Kanata keyboard remapper
Documentation=https://github.com/jtroo/kanata

[Service]
Environment=PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin
Environment=DISPLAY=:0
Type=simple
ExecStart=/usr/bin/sh -c 'exec $$(which kanata) --cfg $${HOME}/.config/kanata/config.kbd'
Restart=no

[Install]
WantedBy=default.target
Enter fullscreen mode Exit fullscreen mode

Make sure to update the executable location for sh in the snippet above.
This would be the line starting with ExecStart=/usr/bin/sh -c.
You can check the executable path with:

which sh
Enter fullscreen mode Exit fullscreen mode

Then run:

systemctl --user daemon-reload
systemctl --user enable kanata.service
systemctl --user start kanata.service
systemctl --user status kanata.service   # check whether the service is running
Enter fullscreen mode Exit fullscreen mode

Conclusion

With this setup, you've transformed your Caps Lock and Tab keys into productivity powerhouses:

  • Tap Caps Lock for Escape, hold for Control
  • Tap Tab for normal Tab, hold to access your symbol layer

This configuration is especially powerful for Neovim users, providing quick access to Escape, and for developers who frequently use symbols and navigation keys.

Remember, this is just the beginning. Kanata offers a wealth of customization options. Experiment, refine, and create a keyboard layout that perfectly suits your workflow.

Happy typing!

Top comments (1)

Collapse
 
shanu-kumawat profile image
Shanu Kumawat

Any suggestions for next post?