regex-edit

This is a little example about how to write your own ZLE widgets. This one use the read-from-minibuffer module to ask the user the regex to apply.

# Define a new ZLE widget to apply a regexp
# to all but command (asyd@asyd.net)

autoload -U read-from-minibuffer

# Define a new function which apply a regexp
# to all words but the first one (the command)
regex-edit () {
   # REPLY will be filled by read-from-minibuffer
   local REPLY words
   words=(${=BUFFER})
   read-from-minibuffer "Regexp:"
   if [ -n $REPLY ]; then
      # Yes I know, it's a bit ugly, but how apply a regex
      # in pure zsh ?
      BUFFER="$words[1] $(echo $words[2,${#words}] | sed ${REPLY})"
   fi
}

# Create a new ZLE widget
zle -N regex-edit

# And finally bind it
bindkey "^[e" regex-edit

see also another example

 
zle/new_widget.txt · Last modified: 2010/01/05 09:20 (external edit)