Next: , Up: zap-to-char

The Complete zap-to-char Implementation

The zap-to-char function removes the text in the region between the location of the cursor (i.e., of point) up to and including the next occurrence of a specified character. The text that zap-to-char removes is put in the kill ring; and it can be retrieved from the kill ring by typing C-y (yank). If the command is given an argument, it removes text through that number of occurrences. Thus, if the cursor were at the beginning of this sentence and the character were ‘s’, ‘Thus’ would be removed. If the argument were two, ‘Thus, if the curs’ would be removed, up to and including the ‘s’ in ‘cursor’.

If the specified character is not found, zap-to-char will say “Search failed”, tell you the character you typed, and not remove any text.

In order to determine how much text to remove, zap-to-char uses a search function. Searches are used extensively in code that manipulates text, and we will focus attention on them as well as on the deletion command.

Here is the complete text of the version 22 implementation of the function:

     (defun zap-to-char (arg char)
       "Kill up to and including ARG'th occurrence of CHAR.
     Case is ignored if `case-fold-search' is non-nil in the current buffer.
     Goes backward if ARG is negative; error if CHAR not found."
       (interactive "p\ncZap to char: ")
       (if (char-table-p translation-table-for-input)
           (setq char (or (aref translation-table-for-input char) char)))
       (kill-region (point) (progn
                              (search-forward (char-to-string char)
                                              nil nil arg)

The documentation is thorough. You do need to know the jargon meaning of the word `kill'.