diff options
| author | srdusr <trevorgray@srdusr.com> | 2025-09-01 15:40:50 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2025-09-01 15:40:50 +0200 |
| commit | 40fc89461967f0094911e8846117ed3190e42d0b (patch) | |
| tree | ed22b71d8a3036fb7e0e2a48ec6a09f99f9da365 /linux/home/.config/zsh/user/bindings.zsh | |
| parent | ecd73a73200a64b5c62614debd9f9ec08748a72d (diff) | |
| download | dotfiles-40fc89461967f0094911e8846117ed3190e42d0b.tar.gz dotfiles-40fc89461967f0094911e8846117ed3190e42d0b.zip | |
Testing
Diffstat (limited to 'linux/home/.config/zsh/user/bindings.zsh')
| -rw-r--r-- | linux/home/.config/zsh/user/bindings.zsh | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/linux/home/.config/zsh/user/bindings.zsh b/linux/home/.config/zsh/user/bindings.zsh index ce8451b..ecfb7a8 100644 --- a/linux/home/.config/zsh/user/bindings.zsh +++ b/linux/home/.config/zsh/user/bindings.zsh @@ -21,12 +21,25 @@ bindkey "^U" backward-kill-line bindkey "^[j" history-search-forward # or you can bind it to the down key "^[[B" bindkey "^[k" history-search-backward # or you can bind it to Up key "^[[A" +bindkey '^[[D' backward-char # Left arrow +bindkey '^[[C' forward-char # Right arrow +bindkey '^[D' backward-char # Left arrow +bindkey '^[C' forward-char # Right arrow +bindkey '[C' forward-word +bindkey '[D' backward-word +bindkey -M viins '^[[D' backward-char # Left arrow +bindkey -M viins '^[[C' forward-char # Right arrow + +bindkey -M vicmd '^[[D' backward-char # Left arrow +bindkey -M vicmd '^[[C' forward-char # Right arrow + # Define the 'autosuggest-execute' and 'autosuggest-accept' ZLE widgets autoload -Uz autosuggest-execute autosuggest-accept zle -N autosuggest-execute zle -N autosuggest-accept bindkey '^X' autosuggest-execute bindkey '^Y' autosuggest-accept +bindkey '\M-l' accept-and-complete-next-history # Edit line in vim with alt-e autoload edit-command-line; zle -N edit-command-line @@ -37,3 +50,111 @@ bindkey '^[e' edit-command-line # alt + e exit_zsh() { exit } zle -N exit_zsh bindkey '^D' exit_zsh + +# Copy/Paste +# Safe clipboard copy +smart_copy() { + local text="${LBUFFER}${RBUFFER}" + + # Prefer Wayland, fallback to X11, then others + if command -v wl-copy >/dev/null 2>&1 && [[ "$WAYLAND_DISPLAY" || "$XDG_SESSION_TYPE" == "wayland" ]]; then + echo -n "$text" | wl-copy --foreground --type text/plain 2>/dev/null || true + elif command -v xclip >/dev/null 2>&1 && [[ "$DISPLAY" || "$XDG_SESSION_TYPE" == "x11" || "$XDG_SESSION_TYPE" == "x11-xwayland" ]]; then + echo -n "$text" | xclip -selection clipboard 2>/dev/null || true + elif [[ "$(uname -s)" == "Darwin" ]] && command -v pbcopy >/dev/null 2>&1; then + echo -n "$text" | pbcopy 2>/dev/null || true + elif [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then + echo -n "$text" | clip.exe 2>/dev/null || true + else + echo "smart_copy: No supported clipboard utility found." >&2 + fi +} + +# Safe clipboard paste +smart_paste() { + local clip="" + if command -v wl-paste >/dev/null 2>&1 && [[ "$WAYLAND_DISPLAY" || "$XDG_SESSION_TYPE" == "wayland" ]]; then + clip=$(wl-paste --no-newline 2>/dev/null) + elif command -v xclip >/dev/null 2>&1 && [[ "$DISPLAY" || "$XDG_SESSION_TYPE" == "x11" || "$XDG_SESSION_TYPE" == "x11-xwayland" ]]; then + clip=$(xclip -selection clipboard -o 2>/dev/null) + elif [[ "$(uname -s)" == "Darwin" ]] && command -v pbpaste >/dev/null 2>&1; then + clip=$(pbpaste 2>/dev/null) + elif [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then + clip=$(powershell.exe -Command 'Get-Clipboard -Raw' 2>/dev/null | tr -d '\r') + else + echo "smart_paste: No supported clipboard utility found." >&2 + fi + + LBUFFER+="$clip" + zle reset-prompt +} + +# Register widgets +zle -N smart_copy +zle -N smart_paste + +# Bind keys (optional: choose your preferred) +bindkey '^V' smart_paste +bindkey -M viins '^V' smart_paste +bindkey -M vicmd '^V' smart_paste +bindkey -M vicmd 'p' smart_paste + +bindkey '^Y' smart_copy +bindkey -M viins '^Y' smart_copy +bindkey -M vicmd '^Y' smart_copy +bindkey -M vicmd 'y' smart_copy + +# In vi mode, map Alt-H and Alt-L +#bindkey -M viins "^[u" go_up # Alt-H to go up +#bindkey -M viins "^[o" go_into # Alt-L to go into a directory + + +# Newline and clear +function newline_clear() { + printf "\n" + command clear +} + +zle -N newline_clear + +no_tmux_clear() { + zle clear-screen +} +zle -N no_tmux_clear + +# Newline before clear +if [[ -n "$TMUX" ]]; then + # Bind Ctrl-L to send newline and clear screen + bindkey '^L' newline_clear +else + bindkey '^L' no_tmux_clear +fi + +# use ctrl-z to toggle in and out of bg +function toggle_fg_bg() { + if [[ $#BUFFER -eq 0 ]]; then + BUFFER="fg" + zle accept-line + else + BUFFER="" + zle clear-screen + fi +} +zle -N toggle_fg_bg +bindkey '^Z' toggle_fg_bg + + + + +## Custom key bindings to control history behavior +#bindkey -M vicmd '^[[C' vi-forward-char # Right arrow in normal mode - just move cursor +#bindkey -M vicmd '^[[D' vi-backward-char # Left arrow in normal mode - just move cursor +#bindkey -M vicmd '^A' beginning-of-line # Ctrl-A - go to beginning of line +#bindkey -M vicmd '^E' end-of-line # Ctrl-E - go to end of line + +# Disable automatic suggestion accept on right arrow in normal mode + +## Additional vi-mode key bindings to prevent unwanted history completion +## Disable automatic history completion in normal mode +#bindkey -M vicmd '^[[C' vi-forward-char # Right arrow - just move right, don't complete +#bindkey -M vicmd '^[[D' vi-backward-char # Left arrow - just move left |
