about summary refs log tree commit diff stats
path: root/nix-conf/home/includes
diff options
context:
space:
mode:
authorDavid Morgan <djm_uk@protonmail.com>2024-09-03 15:43:55 +0100
committerDavid Morgan <djm_uk@protonmail.com>2024-09-03 15:43:55 +0100
commit6fa3a5537e9cd9784e8cf75a9475896e59b19579 (patch)
treed33eda51384f3dc0dfa1fc5f206ba812206fe6ab /nix-conf/home/includes
parenta4b9f7e0b12332a4a6282bb0111f7c8b8d445c13 (diff)
downloaddotfiles-6fa3a5537e9cd9784e8cf75a9475896e59b19579.tar.gz
Improve vip function and add some others
Diffstat (limited to 'nix-conf/home/includes')
-rw-r--r--nix-conf/home/includes/zsh.nix47
1 files changed, 31 insertions, 16 deletions
diff --git a/nix-conf/home/includes/zsh.nix b/nix-conf/home/includes/zsh.nix
index 8f5301f..c1fb138 100644
--- a/nix-conf/home/includes/zsh.nix
+++ b/nix-conf/home/includes/zsh.nix
@@ -162,22 +162,37 @@ in
       # disable flow control (so that fzf-git.sh's ^g^s can work)
       stty -ixon
 
-       function vip () {
-         case $# in
-           0)
-             $EDITOR `ea p 1`
-             ;;
-           1)
-             $EDITOR `ea p $1`
-             ;;
-           2)
-             $EDITOR `ea p $1`/"$2"
-             ;;
-           *)
-             echo "Too many parameters"
-             return 1
-             ;;
-         esac
+      # These functions are called as follows, after using ea (using vip as an example):
+      # vip  # edits the first result from ea (roughly equivalent to vi `ea p 1`)
+      # vip <n> # edits the nth result from ea (vi `ea p <n>`)
+      # vip <n> foo # if the nth result from ea is a directory, edit foo in that directory (vi `ea p <n>`/foo)
+      function _vip () {
+        CMD=(''${=1}) # zsh only; not portable
+        BASE_PATH=$(ea p ''${2:-1})
+
+        if [ -z "$BASE_PATH" ]; then
+          echo "No file path found for index $2"
+          return 1
+        fi
+
+        if [ $# -gt 2 -a ! -d "$BASE_PATH" ]; then
+          echo "$BASE_PATH is not a directory"
+          return 2
+        fi
+
+        $CMD "''${BASE_PATH}''${3}"
+      }
+
+      function vip () {
+        _vip $EDITOR ''${@}
+      }
+      function bp () {
+        _vip bat ''${@}
+      }
+      function bpp () {
+        # this will be split into an array in _vip
+        CMD="bat -p"
+        _vip $CMD ''${@}
       }
 
       function generate () { gopass generate -s -p $1 $((RANDOM % 14 + 45)) }