Zapping to strings and regexps

Posted: June 22, 2007 in Emacs

Here’re two functions which are similar to zap-to-char, but zap to a given string or regular expression.

(defun th-zap-to-string (arg str)
  "Same as `zap-to-char' except that it zaps to the given string
instead of a char."
  (interactive "p\\nsZap to string: ")
  (kill-region (point) (progn
                         (search-forward str nil nil arg)

(defun th-zap-to-regexp (arg regexp)
  "Same as `zap-to-char' except that it zaps to the given regexp
instead of a char."
  (interactive "p\\nsZap to regexp: ")
  (kill-region (point) (progn
                         (re-search-forward regexp nil nil arg)

Since I like zapping to strings and regexps more than zapping to chars, I bound them globally to M-z and M-Z, zap-to-char can be accessed with C-M-z.

(global-set-key (kbd "M-z")   'th-zap-to-string)
(global-set-key (kbd "M-Z")   'th-zap-to-regexp)
(global-set-key (kbd "C-M-z") 'zap-to-char)
  1. Chris Ball says:

    Cool. For what it’s worth, I do:

    C-s zapstring RET C-w

    By virtue of C-s setting the mark at the place you search from, and the point ending up at zapstring, the region is set correctly for a cut-region (C-w) to work, and you get interactive highlight search thrown in. You can use any other search function in place of C-s, obviously.

    I like your blog! I also use xmonad and emacs.

    – Chris.

  2. Tassilo Horn says:

    Hi Chris,

    indeed, C-s zapstring RET C-w is even better than th-zap-to-string, and there’s C-u C-s zapregex RET C-w, too!

    Really strange, I even don’t use scrollbars and navigate mostly with Isearch, but I never had the idea to use it for killing.

    Thanks a lot!

  3. Erik Iverson says:

    FYI, the trick described in the comments only seems to work if transient-mark-mode is disabled, at least on Emacs 22.1.1…

  4. santiago mejia says:

    In case it is useful to anyone. The functions, as they stand, do not work on my system. For them to work I had to change:

    (interactive “p\\nsZap to string: “)


    (interactive “p\nsZap to string: “)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s