changeset 758:3a07cc5c825a

feat: add global expanding shortcuts copied from https://stackoverflow.com/questions/28573145/how-can-i-move-the-cursor-after-a-zsh-abbreviation
author zegervdv <zegervdv@me.com>
date Fri, 10 Mar 2023 16:02:39 +0000
parents b9d3378f6c9e
children 5ee8e6af3494
files dot_zshrc
diffstat 1 files changed, 41 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/dot_zshrc	Sat Feb 25 09:36:39 2023 +0100
+++ b/dot_zshrc	Fri Mar 10 16:02:39 2023 +0000
@@ -232,8 +232,47 @@
 alias -s v=nvim
 alias -s vhd=nvim
 alias -s sh=nvim
-# Global aliases
-alias -g X='| clip'
+
+setopt extendedglob
+typeset -Ag abbreviations
+abbreviations=(
+  # "ac"    "ack -C 5__CURSOR__--ignore-dir=.build"
+  "Ig"    "| rg"
+  "Ip"    "| $PAGER"
+  "Ih"    "| head"
+  "Ik"    "| keep"
+  "It"    "| tail"
+  "Is"    "| sort"
+  "Iv"    "| ${VISUAL:-${EDITOR}}"
+  "Iw"    "| wc"
+  "Ix"    "| xargs"
+  "Ic"    "| clip"
+)
+
+magic-abbrev-expand() {
+    local MATCH
+    LBUFFER=${LBUFFER%%(#m)[_a-zA-Z0-9]#}
+    command=${abbreviations[$MATCH]}
+    LBUFFER+=${command:-$MATCH}
+
+    if [[ "${command}" =~ "__CURSOR__" ]]
+    then
+        RBUFFER=${LBUFFER[(ws:__CURSOR__:)2]}
+        LBUFFER=${LBUFFER[(ws:__CURSOR__:)1]}
+    else
+        zle self-insert
+    fi
+}
+
+no-magic-abbrev-expand() {
+  LBUFFER+=' '
+}
+
+zle -N magic-abbrev-expand
+zle -N no-magic-abbrev-expand
+bindkey " " magic-abbrev-expand
+bindkey "^x " no-magic-abbrev-expand
+bindkey -M isearch " " self-insert
 
 # Exports
 export XDG_CONFIG_HOME=$HOME/.config