From 2e38dbc611f75ea3b98bd92e799593c05eaa5c26 Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Sun, 28 Nov 2021 20:58:16 +0000 Subject: [PATCH] Adds auto-expand to some of my aliases Heavily inspired by this: https://dev.to/frost/fish-style-abbreviations-in-zsh-40aa When space is pushed, zsh will try to expand any aliases that I have put in the array in this file. It is currently quite basic, only looking at the first word on the command line. --- shells/zsh/includes/auto-expand.zsh | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 shells/zsh/includes/auto-expand.zsh diff --git a/shells/zsh/includes/auto-expand.zsh b/shells/zsh/includes/auto-expand.zsh new file mode 100644 index 00000000..dff295c1 --- /dev/null +++ b/shells/zsh/includes/auto-expand.zsh @@ -0,0 +1,53 @@ +#!/usr/bin/env zsh + +# An array of aliases that should be auto-expanded +typeset -a ealiases +ealiases=( + "mkdir" + "qmv" + "grep" + "cal" + "df" + "docker" + "docker-compose" + "v" + "vim" + "status" + "st" + "checkout" + "ch" + "push" + "pull" + "bb" + "merge" + "mg" + "switch" + "sw" +) + + + + + +# expand any aliases in the current line buffer +function expand-ealias() { + if [[ $LBUFFER =~ "\<(${(j:|:)ealiases})\$" ]]; then + zle _expand_alias + zle expand-word + fi + zle magic-space +} +zle -N expand-ealias + +# Bind the space key to the expand-alias function above, so that space will expand any expandable aliases +bindkey ' ' expand-ealias +bindkey '^ ' magic-space # control-space to bypass completion +bindkey -M isearch " " magic-space # normal space during searches + +# A function for expanding any aliases before accepting the line as is and executing the entered command +expand-alias-and-accept-line() { + expand-ealias + zle .backward-delete-char + zle .accept-line +} +zle -N accept-line expand-alias-and-accept-line