Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

backward-kill-shell-word widget

[ I mentioned this to IRC some weeks ago and Valodim told me about
select-word-style so I didn't post this to the list; but now, I'll post it
anyway in case there's a subtle difference that I'm overlooking.
Perhaps the interactive_comments test is such a difference? ]

This widget is like ^W but for "shell words" rather than
whitespace-separated words, so

    % echo foo "bar baz" <CURSOR>^W
    % echo foo <CURSOR>

If you use zsh 5.1.1 or older, comment the 'zle -f' line.  (Without that
line, using this widget followed by another kill followed by a yank
won't include the text this widget killed in the yank.)


backward-kill-shell-word() {
  local MATCH; integer MBEGIN MEND
  integer start end_of_word end_of_cut=$CURSOR

  # Walk backwards to an end-of-word
  [[ $LBUFFER =~ '[[:space:]]*$' ]] || : # sets $MATCH
  (( end_of_word = CURSOR - $#MATCH ))

  # Find the start of the shell word ending at $BUFFER[end_of_word]
  () {
    local l="$PREBUFFER$LBUFFER[1,end_of_word]"
    local -a a
    if [[ -o interactive_comments ]]; then
      a=( ${(zZ+c+)l} )
      a=( ${(z)l} )
    (( start = end_of_word - ${#a[-1]} + 1 ))

  # Standard kill-widget behaviour
  zle -f 'kill'
  if [[ $LASTWIDGET == *'kill'* ]]; then
    zle copy-region-as-kill -- "${BUFFER[start,end_of_cut]}"

  # Delete the last shell word from $LBUFFER
zle -N backward-kill-shell-word
bindkey '^T' backward-kill-shell-word

Messages sorted by: Reverse Date, Date, Thread, Author