ZSH: allows command swap key binding to support multiple groups

The groups are now separated by : character and each element in the
group is separated by a space

I have added a group for swapping ping and mtr
This commit is contained in:
Jonathan Hodgson 2020-10-14 12:29:16 +01:00
parent d68a0628b2
commit ae532f2ca0

View file

@ -119,13 +119,20 @@ bindkey '\em' make_current_word_directory
# This came about because I often find myself starting off with cd, tabbing and
# realising I would have been better off starting the command with vim
swap_command(){
local commands=("cd" "vim" "ls")
# Each group should be seperated by a colon with each item in a group
# seperated by a space
local groups="cd vim ls:ping mtr"
local tokens=(${(z)LBUFFER})
local cmd="${tokens[1]}"
local newindex=0
# If there is no command, return
[ "$cmd" = "" ] && return 0
# Find the first group group with the current command
local group="$(echo -en "$groups" | tr ':' '\n' | grep "$cmd" | head -n 1)"
# Turn the chosen group into an array, splitting on a space
local commands=(${(@s/ /)group})
# Find out where the current command is in the list
local currentindex=${commands[(ie)${cmd}]}
if [ "$currentindex" -gt "${#commands}" ]; then