zsh: allow multiple bindings to history-substring-search (#3929)

It's pretty common to need multiple bindings to
history-substring-search, since different terminals will send different
keys for up/down.

This does not break back-compatibility, and introduces a new test
This commit is contained in:
Bernardo Meurer 2023-04-29 07:53:09 -07:00 committed by GitHub
parent 27d89b49e3
commit 3144311f31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 8 deletions

View File

@ -190,18 +190,18 @@ let
options = {
enable = mkEnableOption "history substring search";
searchUpKey = mkOption {
type = types.str;
default = "^[[A";
type = with types; either (listOf str) str ;
default = [ "^[[A" ];
description = ''
The key code to be used when searching up.
The key codes to be used when searching up.
The default of <literal>^[[A</literal> corresponds to the UP key.
'';
};
searchDownKey = mkOption {
type = types.str;
default = "^[[B";
type = with types; either (listOf str) str ;
default = [ "^[[B" ];
description = ''
The key code to be used when searching down.
The key codes to be used when searching down.
The default of <literal>^[[B</literal> corresponds to the DOWN key.
'';
};
@ -593,8 +593,14 @@ in
# https://github.com/zsh-users/zsh-history-substring-search#usage
''
source ${pkgs.zsh-history-substring-search}/share/zsh-history-substring-search/zsh-history-substring-search.zsh
bindkey '${cfg.historySubstringSearch.searchUpKey}' history-substring-search-up
bindkey '${cfg.historySubstringSearch.searchDownKey}' history-substring-search-down
${lib.concatMapStringsSep "\n"
(upKey: "bindkey '${upKey}' history-substring-search-up")
(lib.toList cfg.historySubstringSearch.searchUpKey)
}
${lib.concatMapStringsSep "\n"
(downKey: "bindkey '${downKey}' history-substring-search-down")
(lib.toList cfg.historySubstringSearch.searchDownKey)
}
'')
]);
}

View File

@ -5,5 +5,6 @@
zsh-history-path-old-default = ./history-path-old-default.nix;
zsh-history-path-old-custom = ./history-path-old-custom.nix;
zsh-history-ignore-pattern = ./history-ignore-pattern.nix;
zsh-history-substring-search = ./history-substring-search.nix;
zsh-prezto = ./prezto.nix;
}

View File

@ -0,0 +1,25 @@
{ config, lib, pkgs, ... }:
with lib;
{
config = {
programs.zsh = {
enable = true;
historySubstringSearch = {
enable = true;
searchDownKey = "^[[B";
searchUpKey = [ "^[[A" "\\eOA" ];
};
};
test.stubs.zsh = { };
# Written with regex to ensure we don't end up missing newlines in the future
nmt.script = ''
assertFileRegex home-files/.zshrc "^bindkey '\^\[\[B' history-substring-search-down$"
assertFileRegex home-files/.zshrc "^bindkey '\^\[\[A' history-substring-search-up$"
assertFileRegex home-files/.zshrc "^bindkey '\\\\eOA' history-substring-search-up$"
'';
};
}