Custom key bindings with nixvim

Recently I’ve found the nixvim project and decided to learn something about NVim that goes beyond saving and exiting the editor.

I tried enabling a plugin and play with its keybinding. Apparently such an easy task was too complex for me.

The plugin I chose is “neo-tree”. I wanted to add a binding which toggles the file tree panel, so I added this to my nixvim module:

  programs.nixvim = {
    enable = true;
    # ...
    keymaps = [
      {
        action = "<cmd>Neotree toggle<CR>";
        key = "C-b";
        mode = "n";
        options = {
          desc = "Toggle Tree View.";
        };
      }
    ];
   # ...
};

However, pressing CTRL+b has no effect in the app, but typing the command “:Neotree toggle” manually in NVim has the desired effect.

The entire config file is here: https://github.com/bratfizyk/dotFiles/blob/f9f0a7775620552c38ec9ac04b8ffce0f83f117b/apps/nixvim.nix

Let me answer my own question so that someone might be saved from tears.

This issue is caused by my lack of experience with nvim. In fact all the mappings that require combination of keys needs to be surrounded with < >. Otherwise they represent a sequence of keys.

So the correct code is:

  programs.nixvim = {
    enable = true;
    # ...
    keymaps = [
      {
        action = "<cmd>Neotree toggle<CR>";
        key = "<C-b>";  # this line is changed
        mode = "n";
        options = {
          desc = "Toggle Tree View.";
        };
      }
    ];
   # ...
};
4 Likes