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

Re: delete-word does not delete the entire word...

DervishD wrote:
> > Incidentally, a wrapper function to delete the word under the cursor
> > is a little tricky to write.
>     Writing zle widgets is tricky.

If you have 4.1, much of the work is done for you; see the description
of match-words-by-style in the zshcontrib manual.  Here's a function
using it (which consequently won't work in 4.0) which can go into the
distribution when the glitches have been ironed out.

A side effect is that you can decide using styles what constitutes a
word, as described in the same section of the manual.

emulate -L zsh
setopt extendedglob

local curcontext=:zle:delete-whole-word
local -a matched_words
# Start and end of range of characters to remove.
integer pos1 pos2


if [[ -n "${matched_words[3]}" ]]; then
    # There's whitespace before the cursor, so the word we are deleting
    # starts at the cursor position.
    # No whitespace before us, so delete any wordcharacters there.

if [[ -n "${matched_words[4]}" ]]; then
    # There's whitespace at the cursor position, so only delete
    # up to the cursor position.
    # No whitespace at the cursor position, so delete the
    # current character and any following wordcharacters.
    (( pos2 = CURSOR + ${#matched_words[5]} + 1 ))

# Move the cursor and delete the block in one go for the
# purpose of yanking.
(( CURSOR = pos1 ))
Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxx>
Work: pws@xxxxxxx
Web: http://www.pwstephenson.fsnet.co.uk

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