Email or username:

Password:

Forgot your password?
Ténno Seremélʹ

Is there a version of zap-up-to-char that counts from the end of a line? Or maybe more simply zaps up to the last occurence, since that's what I sometimes need.

#lang_en #emacs #questions

12 comments
jeeger

@tennoseremel You could use `avy-zap-up-to-char-dvim` which allows you to select the character position interactively.

Ténno Seremélʹ

@jeeger Haven't tried using avy yet, TBH 🤔

jeeger

@tennoseremel It's yet another extra package, I know ☺. But the `goto-char` functionality is great.

Ramin Honary

@tennoseremel easy! It even says right in the documentation for zap-up-to-char:

>

> Kill up to, but not including ARGth occurrence of CHAR.

> When run interactively, the argument INTERACTIVE is non-nil.

> 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.

> Ignores CHAR at point.

> If called interactively, do a case sensitive search if CHAR

> is an upper-case character.

To set a negative argument: C-u - M-z ("Control-U minus Alt-Z")

Just as a reminder, to lookup the documentation on functions and commands: C-h f zap-up-to-char<RET>

Also, just FYI, you can set the mark highlighting and run interactive search C-s or C-r and the search will not cancel the highlight, so you can highlight up to a search string.

@tennoseremel easy! It even says right in the documentation for zap-up-to-char:

>

> Kill up to, but not including ARGth occurrence of CHAR.

> When run interactively, the argument INTERACTIVE is non-nil.

> 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.

rayslava

@tennoseremel If I got you right, it will be something like that:

(defun zap-back-up-to-char (char)
"Zap back to a character CHAR from the end of the line"
(interactive "cZap back to char: ")
(let ((end (save-excursion (move-end-of-line 1) (point)))
(start (save-excursion
(move-end-of-line 1)
(search-backward (char-to-string char) nil t))))
(if start
(progn
(delete-region (1+ start) end)
(goto-char (1+ start)))
(error "Character not found"))))

(define-key global-map (kbd "C-M-z") #'zap-back-up-to-char)

@tennoseremel If I got you right, it will be something like that:

(defun zap-back-up-to-char (char)
"Zap back to a character CHAR from the end of the line"
(interactive "cZap back to char: ")
(let ((end (save-excursion (move-end-of-line 1) (point)))
(start (save-excursion
(move-end-of-line 1)
(search-backward (char-to-string char) nil t))))
(if start
(progn
(delete-region (1+ start) end)
(goto-char (1+ start)))
(error "Character not found"))))

rayslava

@tennoseremel or this one :blobcatthink:

(defun zap-up-to-last-char (char)
"Zap up to the last occurrence of CHAR in the line"
(interactive "cZap up to char: ")
(let ((start (point)))
(let ((end (save-excursion
(move-end-of-line 1)
(search-backward (char-to-string char) start t))))
(if end
(delete-region start end)
(error "Character not found")))))

Ténno Seremélʹ

@rayslava Thanks! I'll try it later, when I have enough mental power to read what it does :blobcatgiggle:

rayslava

@tennoseremel yep.
Also, a one-line-shorter version that saves the zapped part into kill-ring:

(defun zap-up-to-last-char (char)
"Zap up to the last occurrence of `char' in the line"
(interactive "cZap up to char: ")
(let* ((start (point))
(end (save-excursion
(move-end-of-line 1)
(search-backward (char-to-string char) start t))))
(if end
(kill-region start end)
(error "Character not found"))))

Ténno Seremélʹ

@rayslava Yay :)

(defun zap-up-to-last-char (char)
"Zap up to the last occurrence of CHAR in the line."
(interactive "cZap up to the last occurence of char: ")
(let* ((case-fold-search (if (char-uppercase-p char) nil case-fold-search))
(start (point))
(end (save-excursion
(move-end-of-line 1)
(search-backward (char-to-string char) start t))))
(if end
(kill-region start end)
(error "Character not found"))))

rayslava

@tennoseremel now you just need to promote it to upstream :blobcathappy:

Ténno Seremélʹ

@rayslava Looking at the source of zap-up-to-char it needs to be a bit longer, so it could be used non-interactively… :)

Go Up