Pages

Sunday, May 24, 2026

zap-to-char M-z

Sometimes I discover an Emacs feature after an accidental keystroke. If I can remember the keystroke, I'll invoke the keystroke help (C-h k) to learn about it. Or sometimes the prompt in the mode line will suggest the function's name.

Today, the keystroke was M-z, which is bound to the function [[zap-to-char][zap-to-char]].

It seems to be ideal if, like me, you doze briefly while typing a senteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

Oops, there I go again!

To fix that I can just press M-- M-z t to delete all the trailing "e" characters.

Note that M-- is invoked by holding down the Alt key while pressing hyphen. It defines a negative prefix argument, which instructs zap-to-char to "go backward," deleting all characters from point to the specified character ("t"). I'm not sure how or why you'd want the default "forward" behavior.

What accidental keystroke have you discovered recently?

2 comments:

  1. I often use zap to char or zap up to char when replacing the contents of a string or a parenthetical note, or when rewording part of a phrase or a sentence.

    ReplyDelete
    Replies
    1. I can see that it would be useful for parenthetical or quoted phrases. It would be "targeted" since the "char" would be somewhat unique. Thanks for that suggestion!

      Delete

Thank you for taking time to comment. Please note that comments may be deleted if they:

* Contain link(s) to a domain outside USA or Canada or to a commercial enterprise.
* Include non-English words or characters.
* Are irrelevant to the subject matter of the post.

For posts older than 14 days, comments are moderated and require approval.