keymap.zsh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # ===== History Navigation =====
  2. # Setup fuzzy history search with up/down keys
  3. # Up arrow/k - search history forward
  4. autoload -U up-line-or-beginning-search
  5. zle -N up-line-or-beginning-search
  6. # Down arrow/j - search history backward
  7. autoload -U down-line-or-beginning-search
  8. zle -N down-line-or-beginning-search
  9. # Bind keys in both vicmd and viins modes
  10. # vicmd = vi command mode, viins = vi insert mode
  11. bindkey -M vicmd "k" up-line-or-beginning-search
  12. bindkey -M vicmd "^k" up-line-or-beginning-search
  13. bindkey -M viins "^k" up-line-or-beginning-search
  14. bindkey -M viins "^[[A" up-line-or-beginning-search # Up arrow key
  15. bindkey -M vicmd "j" down-line-or-beginning-search
  16. bindkey -M vicmd "^j" down-line-or-beginning-search
  17. bindkey -M viins "^j" down-line-or-beginning-search
  18. bindkey -M viins "^[[B" down-line-or-beginning-search # Down arrow key
  19. # ===== System Clipboard Integration =====
  20. # Enhanced yank that copies to system clipboard
  21. function vi-yank-clipboard() {
  22. # First perform the normal vi-yank operation
  23. zle vi-yank
  24. # Only copy if there's content in the cut buffer
  25. if [[ -n "$CUTBUFFER" ]]; then
  26. # Detect the environment and use appropriate clipboard command
  27. if [[ "$XDG_SESSION_TYPE" == "wayland" ]] && command -v wl-copy >/dev/null; then
  28. echo -n "$CUTBUFFER" | wl-copy
  29. elif command -v xclip >/dev/null; then
  30. echo -n "$CUTBUFFER" | xclip -selection clipboard
  31. elif command -v pbcopy >/dev/null; then
  32. # For macOS
  33. echo -n "$CUTBUFFER" | pbcopy
  34. fi
  35. fi
  36. }
  37. # Register the function and bind it
  38. zle -N vi-yank-clipboard
  39. bindkey -M vicmd "y" vi-yank-clipboard
  40. # ===== Command Line Editing =====
  41. # Edit command in external editor
  42. # Load the function and register it
  43. autoload -U edit-command-line
  44. zle -N edit-command-line
  45. # Bind 'v' in command mode to open editor
  46. bindkey -M vicmd "v" edit-command-line
  47. # ===== Additional Useful Bindings =====
  48. # Ctrl+Space to accept autosuggestion
  49. bindkey '^ ' autosuggest-accept
  50. # Ctrl+e to edit command in editor from any mode
  51. bindkey '^e' edit-command-line
  52. # Ensure backspace and delete work as expected
  53. bindkey '^?' backward-delete-char
  54. bindkey "^[[3~" delete-char
  55. # Home/End keys
  56. bindkey "^[[H" beginning-of-line
  57. bindkey "^[[F" end-of-line
  58. bindkey "^[[1~" beginning-of-line
  59. bindkey "^[[4~" end-of-line
  60. # Function to open NixOS config directory
  61. function edit-nixos-config() {
  62. # Store current directory
  63. local original_dir="$PWD"
  64. # Build the command that will cd, run nvim, then cd back
  65. BUFFER="cd ~/.config/nixos && nvim . && cd '$original_dir'"
  66. zle accept-line
  67. }
  68. zle -N edit-nixos-config
  69. # Bind Ctrl+X then C in both insert and command modes
  70. bindkey -M viins '^xc' edit-nixos-config
  71. bindkey -M vicmd '^xc' edit-nixos-config