feat(tmux): add fish completions
This commit is contained in:
parent
3e8ff2e064
commit
b8be6a3a4e
6 changed files with 77 additions and 22 deletions
130
modules/programs/tmux/default.nix
Normal file
130
modules/programs/tmux/default.nix
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
{ config, lib, pkgs, ... }:
|
||||
|
||||
with lib;
|
||||
let
|
||||
cfg = config.my.programs.tmux;
|
||||
|
||||
fzf1 = pkgs.writeShellApplication {
|
||||
name = "fzf1";
|
||||
runtimeInputs = with pkgs; [ coreutils fzf ];
|
||||
text = ''
|
||||
#!/usr/bin/env bash
|
||||
|
||||
options=$(fzf --filter "''$*" < /dev/stdin)
|
||||
|
||||
if [[ -z $options ]]; then
|
||||
exit 1
|
||||
elif [[ $(wc -l <<< "$options") -eq 1 ]]; then
|
||||
selected="$options"
|
||||
else
|
||||
selected=$(echo "$options" | fzf --query="$*")
|
||||
fi
|
||||
|
||||
if [[ -z $selected ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "$selected"
|
||||
'';
|
||||
};
|
||||
|
||||
tmux-switch = pkgs.writeShellApplication {
|
||||
name = "tmux-switch";
|
||||
runtimeInputs = with pkgs; [ tmux ];
|
||||
text = ''
|
||||
#!/usr/bin/env bash
|
||||
if [[ -z ''${TMUX+x} ]]; then
|
||||
tmux attach -t "$1"
|
||||
else
|
||||
tmux switch-client -t "$1"
|
||||
fi
|
||||
'';
|
||||
};
|
||||
|
||||
tmux-sessionizer = pkgs.writeFishApplication {
|
||||
name = "ts";
|
||||
runtimeInputs = with pkgs; [ tmux findutils coreutils procps fd fzf1 tmux-switch ];
|
||||
text = readFile ./tmux-sessionizer/script.fish;
|
||||
completions = readFile ./tmux-sessionizer/completions.fish;
|
||||
};
|
||||
|
||||
tmux-attach = pkgs.writeFishApplication {
|
||||
name = "ta";
|
||||
runtimeInputs = with pkgs; [ tmux fzf1 tmux-switch ];
|
||||
text = readFile ./tmux-attach/script.fish;
|
||||
completions = readFile ./tmux-attach/completions.fish;
|
||||
};
|
||||
|
||||
in
|
||||
{
|
||||
options.my.programs.tmux = {
|
||||
enable = mkEnableOption "tmux";
|
||||
autoAttach = mkEnableOption "autoAttach";
|
||||
keybinds = mkOption {
|
||||
type = with types; attrsOf (attrsOf string);
|
||||
default = { };
|
||||
description = "Keybinds for tmux";
|
||||
example = literalExample ''
|
||||
{
|
||||
prefix = {
|
||||
"-" = "split-window -v";
|
||||
"|" = "split-window -h";
|
||||
};
|
||||
copy-mode-vi = {
|
||||
"v" = "send -X begin-selection";
|
||||
};
|
||||
}
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
my.shell.abbreviations.t = "tmux";
|
||||
|
||||
home-manager.users.moritz.home.packages = [
|
||||
tmux-sessionizer
|
||||
tmux-attach
|
||||
];
|
||||
home-manager.users.moritz.programs = {
|
||||
tmux = {
|
||||
enable = true;
|
||||
clock24 = true;
|
||||
customPaneNavigationAndResize = true;
|
||||
keyMode = "vi";
|
||||
mouse = true;
|
||||
prefix = "C-Space";
|
||||
sensibleOnTop = false;
|
||||
plugins = with pkgs.tmuxPlugins; [
|
||||
sensible
|
||||
tmux-fzf
|
||||
vim-tmux-navigator
|
||||
yank
|
||||
];
|
||||
extraConfig =
|
||||
let
|
||||
mkKeybind = table: mapAttrsToList (keybind: value: "bind-key -T ${table} '${keybind}' ${value}");
|
||||
keybinds = flatten (mapAttrsToList mkKeybind cfg.keybinds);
|
||||
in
|
||||
''
|
||||
# Keybinds
|
||||
${concatStringsSep "\n" keybinds}
|
||||
'';
|
||||
};
|
||||
fzf.tmux.enableShellIntegration = true;
|
||||
fish.interactiveShellInit =
|
||||
let
|
||||
insideVariables = [ "$TMUX" "$INSIDE_EMACS" "$EMACS" "$VIM" "$VSCODE_RESOLVING_ENVIRONMENT" ];
|
||||
insideVariableMissing = concatStringsSep " && " (map (x: "test -z ${x}") insideVariables);
|
||||
in
|
||||
mkIf cfg.autoAttach
|
||||
''
|
||||
if ! fish_is_root_user && test "$TERM_PROGRAM" != 'vscode' && ${insideVariableMissing}
|
||||
if test -z $tmux_autostarted
|
||||
set -x tmux_autostarted true
|
||||
ts
|
||||
end
|
||||
end
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
1
modules/programs/tmux/tmux-attach/completions.fish
Normal file
1
modules/programs/tmux/tmux-attach/completions.fish
Normal file
|
|
@ -0,0 +1 @@
|
|||
complete -c ta -f -a '(tmux list-sessions -F "#{session_name}" 2>/dev/null)'
|
||||
5
modules/programs/tmux/tmux-attach/script.fish
Normal file
5
modules/programs/tmux/tmux-attach/script.fish
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
set selected (tmux list-sessions -F '#{session_name}' 2>/dev/null | fzf1 $argv)
|
||||
if not test -n "$selected"
|
||||
exit 1
|
||||
end
|
||||
tmux-switch "$selected"
|
||||
1
modules/programs/tmux/tmux-sessionizer/completions.fish
Normal file
1
modules/programs/tmux/tmux-sessionizer/completions.fish
Normal file
|
|
@ -0,0 +1 @@
|
|||
complete -c ts -f -a '(fd -HIg '.git' ~/ --min-depth 1 --max-depth 5 --type d --prune --exec realpath "{}/.." | xargs -I{} basename {} | string replace "." "")'
|
||||
13
modules/programs/tmux/tmux-sessionizer/script.fish
Normal file
13
modules/programs/tmux/tmux-sessionizer/script.fish
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
set selected (fd -HIg '.git' ~/ --min-depth 1 --max-depth 5 --type d --prune --exec dirname {} | fzf1 $argv)
|
||||
|
||||
set selected_name (basename $selected 2>/dev/null | string replace "." "_")
|
||||
|
||||
if not test -n "$selected_name"
|
||||
exit 1
|
||||
end
|
||||
|
||||
if ! tmux has-session -t $selected_name 2> /dev/null
|
||||
tmux new-session -ds $selected_name -c $selected
|
||||
end
|
||||
|
||||
tmux-switch $selected_name
|
||||
Loading…
Add table
Add a link
Reference in a new issue