nvf/docs/manual/hacking/keybinds.md

93 lines
3 KiB
Markdown
Raw Normal View History

2023-12-09 19:03:58 +00:00
# Keybinds {#sec-keybinds}
As of 0.4, there exists an API for writing your own keybinds and a couple of
useful utility functions are available in the [extended standard
library](https://github.com/NotAShelf/nvf/tree/main/lib). The following
section contains a general overview to how you may utilize said functions.
2023-12-09 19:03:58 +00:00
## Custom Key Mappings Support for a Plugin {#sec-custom-key-mappings}
To set a mapping, you should define it in `vim.keymaps`.
2023-12-09 19:03:58 +00:00
An example, simple keybinding, can look like this:
```nix
{
vim.keymaps = [
{
key = "<leader>wq";
mode = ["n"];
2023-12-09 19:03:58 +00:00
action = ":wq<CR>";
silent = true;
desc = "Save file and quit";
}
];
2023-12-09 19:03:58 +00:00
}
```
There are many settings available in the options. Please refer to the
[documentation](https://notashelf.github.io/nvf/options.html#opt-vim.keymaps)
to see a list of them.
2023-12-09 19:03:58 +00:00
2024-11-17 18:49:27 +00:00
**nvf** provides a helper function, so that you don't have to write the
mapping attribute sets every time:
2024-11-17 18:49:27 +00:00
- `mkKeymap`, which mimics neovim's `vim.keymap.set` function
2024-11-17 18:49:27 +00:00
You can read the source code of some modules to see them in action, but the
usage should look something like this:
2023-12-09 19:03:58 +00:00
```nix
# plugindefinition.nix
{lib, ...}: with lib; {
options.vim.plugin = {
enable = mkEnableOption "Enable plugin";
# Mappings should always be inside an attrset called mappings
mappings = {
2024-11-17 18:49:27 +00:00
workspaceDiagnostics = mkMappingOption "Workspace diagnostics [trouble]" "<leader>lwd";
documentDiagnostics = mkMappingOption "Document diagnostics [trouble]" "<leader>ld";
lspReferences = mkMappingOption "LSP References [trouble]" "<leader>lr";
quickfix = mkMappingOption "QuickFix [trouble]" "<leader>xq";
locList = mkMappingOption "LOCList [trouble]" "<leader>xl";
symbols = mkMappingOption "Symbols [trouble]" "<leader>xs";
2023-12-09 19:03:58 +00:00
};
}
```
```nix
# config.nix
{
config,
lib,
2024-11-17 18:49:27 +00:00
options,
2023-12-09 19:03:58 +00:00
...
2024-11-17 18:49:27 +00:00
}: let
inherit (lib.modules) mkIf;
inherit (lib.nvim.binds) mkKeymap;
2023-12-09 19:03:58 +00:00
2024-11-17 18:49:27 +00:00
cfg = config.vim.plugin;
2023-12-09 19:03:58 +00:00
2024-11-17 18:49:27 +00:00
keys = cfg.mappings;
inherit (options.vim.lsp.trouble) mappings;
in {
config = mkIf cfg.enable {
vim.keymaps = [
(mkKeymap "n" keys.workspaceDiagnostics "<cmd>Trouble toggle diagnostics<CR>" {desc = mappings.workspaceDiagnostics.description;})
(mkKeymap "n" keys.documentDiagnostics "<cmd>Trouble toggle diagnostics filter.buf=0<CR>" {desc = mappings.documentDiagnostics.description;})
(mkKeymap "n" keys.lspReferences "<cmd>Trouble toggle lsp_references<CR>" {desc = mappings.lspReferences.description;})
(mkKeymap "n" keys.quickfix "<cmd>Trouble toggle quickfix<CR>" {desc = mappings.quickfix.description;})
(mkKeymap "n" keys.locList "<cmd>Trouble toggle loclist<CR>" {desc = mappings.locList.description;})
(mkKeymap "n" keys.symbols "<cmd>Trouble toggle symbols<CR>" {desc = mappings.symbols.description;})
2023-12-09 19:03:58 +00:00
];
2024-11-17 18:49:27 +00:00
};
2023-12-09 19:03:58 +00:00
};
}
```
::: {.note}
If you have come across a plugin that has an API that doesn't seem to easily
allow custom keybindings, don't be scared to implement a draft PR. We'll help
you get it done.
2023-12-09 19:03:58 +00:00
:::