Skip to content

Commit

Permalink
modules/keymaps: refactor + new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanLepage committed Oct 2, 2023
1 parent 382973b commit 574fb73
Show file tree
Hide file tree
Showing 6 changed files with 716 additions and 521 deletions.
76 changes: 41 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,23 @@ If you are using `makeNixvimWithModule`, then options is treated as options for

## Key mappings
It is fully possible to define key mappings from within NixVim. This is done
using the `maps` attribute:
using the `keymaps` attribute:

```nix
{
programs.nixvim = {
maps = {
normalVisualOp.";" = ":";
normal."<leader>m" = {
silent = true;
action = "<cmd>make<CR>";
keymaps = [
{
key = ";";
action = ":";
}
{
mode = "n";
key = "<leader>m";
options.silent = true;
action = "<cmd>!make<CR>";
};
};
];
};
}
```
Expand All @@ -344,34 +349,35 @@ noremap ; :
nnoremap <leader>m <silent> <cmd>make<CR>
```

This table describes all modes for the `maps` option:

| NixVim | NeoVim |
|----------------|--------------------------------------------------|
| normal | Normal mode |
| insert | Insert mode |
| visual | Visual and Select mode |
| select | Select mode |
| terminal | Terminal mode |
| normalVisualOp | Normal, visual, select and operator-pending mode |
| visualOnly | Visual mode only, without select |
| operator | Operator-pending mode |
| insertCommand | Insert and command-line mode |
| lang | Insert, command-line and lang-arg mode |
| command | Command-line mode |

The map options can be set to either a string, containing just the action,
or to a set describing additional options:

| NixVim | Default | VimScript |
|---------|---------|------------------------------------------|
| silent | false | `<silent>` |
| nowait | false | `<silent>` |
| script | false | `<script>` |
| expr | false | `<expr>` |
| unique | false | `<unique>` |
| noremap | true | Use the 'noremap' variant of the mapping |
| action | N/A | Action to execute |
This table describes all modes for the `keymaps` option.
You can provide several mode to a single mapping by using a list of strings.

| Short | Description |
|-------|--------------------------------------------------|
| `"n"` | Normal mode |
| `"i"` | Insert mode |
| `"v"` | Visual and Select mode |
| `"s"` | Select mode |
| `"t"` | Terminal mode |
| `"" ` | Normal, visual, select and operator-pending mode |
| `"x"` | Visual mode only, without select |
| `"o"` | Operator-pending mode |
| `"!"` | Insert and command-line mode |
| `"l"` | Insert, command-line and lang-arg mode |
| `"c"` | Command-line mode |

Each keymap can specify the following settings in the `options` attrs.

| NixVim | Default | VimScript |
|---------|---------|---------------------------------------------------|
| silent | false | `<silent>` |
| nowait | false | `<silent>` |
| script | false | `<script>` |
| expr | false | `<expr>` |
| unique | false | `<unique>` |
| noremap | true | Use the 'noremap' variant of the mapping |
| remap | false | Make the mapping recursive (inverses `noremap`) |
| desc | "" | A description of this keymap |

## Globals
Sometimes you might want to define a global variable, for example to set the
Expand Down
111 changes: 59 additions & 52 deletions example.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,81 @@
# when compared to just installing NeoVim.
enable = true;

maps.normal = {
keymaps = [
# Equivalent to nnoremap ; :
";" = ":";
{
key = ";";
action = ":";
}

# Equivalent to nmap <silent> <buffer> <leader>gg <cmd>Man<CR>
"<leader>gg" = {
silent = true;
remap = false;
{
key = "<leader>gg";
action = "<cmd>Man<CR>";
# Etc...
};

# We can set the leader key:
leader = ",";
options = {
silent = true;
remap = false;
};
}
# Etc...
];

# We can create maps for every mode!
# There is .normal, .insert, .visual, .operator, etc!
# We can set the leader key:
leader = ",";

# We can also set options:
options = {
tabstop = 4;
shiftwidth = 4;
expandtab = false;
# We can create maps for every mode!
# There is .normal, .insert, .visual, .operator, etc!

mouse = "a";
# We can also set options:
options = {
tabstop = 4;
shiftwidth = 4;
expandtab = false;

# etc...
};
mouse = "a";

# Of course, we can still use comfy vimscript:
extraConfigVim = builtins.readFile ./init.vim;
# Or lua!
extraConfigLua = builtins.readFile ./init.lua;
# etc...
};

# One of the big advantages of NixVim is how it provides modules for
# popular vim plugins
# Enabling a plugin this way skips all the boring configuration that
# some plugins tend to require.
plugins = {
lightline = {
enable = true;
# Of course, we can still use comfy vimscript:
extraConfigVim = builtins.readFile ./init.vim;
# Or lua!
extraConfigLua = builtins.readFile ./init.lua;

# This is optional - it will default to your enabled colorscheme
colorscheme = "wombat";
# One of the big advantages of NixVim is how it provides modules for
# popular vim plugins
# Enabling a plugin this way skips all the boring configuration that
# some plugins tend to require.
plugins = {
lightline = {
enable = true;

# This is one of lightline's example configurations
active = {
left = [
["mode" "paste"]
["redaonly" "filename" "modified" "helloworld"]
];
};
# This is optional - it will default to your enabled colorscheme
colorscheme = "wombat";

component = {
helloworld = "Hello, world!";
};
# This is one of lightline's example configurations
active = {
left = [
["mode" "paste"]
["redaonly" "filename" "modified" "helloworld"]
];
};

# Of course, there are a lot more plugins available.
# You can find an up-to-date list here:
# https://nixvim.pta2002.com/plugins
component = {
helloworld = "Hello, world!";
};
};

# There is a separate namespace for colorschemes:
colorschemes.gruvbox.enable = true;

# What about plugins not available as a module?
# Use extraPlugins:
extraPlugins = with pkgs.vimPlugins; [vim-toml];
# Of course, there are a lot more plugins available.
# You can find an up-to-date list here:
# https://nixvim.pta2002.com/plugins
};

# There is a separate namespace for colorschemes:
colorschemes.gruvbox.enable = true;

# What about plugins not available as a module?
# Use extraPlugins:
extraPlugins = with pkgs.vimPlugins; [vim-toml];
};
}
Loading

0 comments on commit 574fb73

Please sign in to comment.