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

ZLE Widget: Insert last word (except &)



Hi there,

  I found it annoying that the widget insert-last-word inserts `&' if the
previous command was a background job. I'd rather like the last word
*before* `&' to be inserted. So I wrote a custom widget:

smart-insert-last-word() {
  emulate -L zsh
  setopt extendedglob

  if [[ -n "$NUMERIC" ]]
  then
    zle insert-last-word -n $NUMERIC
  else
    lastcmd=`fc -n -l -1`
    lastcmd=${lastcmd%%[[:space:]]#}

    if [[ $lastcmd[$#lastcmd] == '&' && $lastcmd[$#lastcmd-1] != '\' ]]
    then
      zle insert-last-word -n 2
    else
      zle insert-last-word
    fi
  fi    
}

zle -N smart-insert-last-word

bindkey "^[." smart-insert-last-word
bindkey "^[_" smart-insert-last-word

If any numeric argument is given, the original function is called with
that argument. Otherwise, the previous command ist determined. If it
ends with `&', insert-last-word is invoked with an argument of 2,
which inserts the last but one word.

Example:

% myeditor *.txt &
% cp _                # press [Alt+.], _ is the cursor
% cp *.txt_           # result

Hope you find it useful.

Christoph

-- 
   o      o     o    o     o    <o     <o>    o>    o
  .|.    \|.   \|/   //    X     \      |    <|    <|>
   /\     >\   /<    >\   /<     >\    /<     >\    /<
Mr. Asciihead learns the Macarena
Christoph Lange, langec@xxxxxx, http://www.cul.de/home/yaph/, ICQ #51191833



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