emacs equivalent of ct

Go To StackoverFlow.com

4

looking for an equivalent cut and paste strategy that would replicate vim's 'cut til'. I'm sure this is googleable if I actually knew what it was called in vim, but heres what i'm looking for:

if i have a block of text like so:

foo bar (baz)

and I was at the beginning of the line and i wanted to cut until the first paren, in visual mode, I'd do:

 ct (

I think there is probably a way to look back and i think you can pass more specific regular expressions. But anyway, looking for some emacs equivalents to doing this kind of text replacement. Thanks.

2012-04-05 18:25
by Jed Schneider


8

Here are three ways:

  1. Just type M-dM-d to delete two words. This will leave the final space, so you'll have to delete it yourself and then add it back if you paste the two words back elsewhere.
  2. M-z is zap-to-char, which deletes text from the cursor up to and including a character you specify. In this case you'd have to do something like M-2M-zSPC to zap up to and including the second space character.
  3. Type C-SPC to set the mark, then go into incremental search with C-s, type a space to jump to the first space, then C-s to search forward for the next space, RET to terminate the search, and finally C-w to kill the text you selected.

Personally I'd generally go with #1.

2012-04-05 18:42
by Sean
In the case of the 3rd you don't have to set the mark with C-SPC at the start, because incremental search does that automatically - Tom 2012-04-06 05:20
@Tom is correct, for the 3rd you do not need to set the mark. Conflicted as to which answer to mark as the answer. Marking Sean's answer as the answer for giving a few extra options; but bumping kindahero's answer because it is also exactly what I need in this case - Jed Schneider 2012-04-20 12:45


5

as ataylor said zap-to-char is the way to go, The following modification to the zap-to-char is what exactly you want

(defun zap-up-to-char (arg char)
  "Like standard zap-to-char, but stops just before the given character."
  (interactive "p\ncZap up to char: ")
  (kill-region (point)
               (progn
                 (search-forward (char-to-string char) nil nil arg)
                 (forward-char (if (>= arg 0) -1 1))
                 (point))))

(define-key global-map [(meta ?z)] 'zap-up-to-char)           ; Rebind M-z to our version

BTW don't forget that it has the ability to go backward with a negative prefix

2012-04-05 18:46
by kindahero
zap-up-to-char is included with recent Emacsen in misc.el - sanityinc 2012-04-06 12:31
@sanityinc didn't know that. more room for init.el the - kindahero 2012-04-06 13:15
I was curious, so I just double-checked; even Emacs versions as old as 22.1.1 have that function in misc.el. It's not autoloaded, though, so (require misc) is needed - sanityinc 2012-04-06 19:21
using 23.4.1 and it is included by default. thank you - Jed Schneider 2012-04-20 12:46


3

That sounds like zap-to-char in emacs, bound to M-z by default. Note that zap-to-char will cut all the characters up to and including the one you've selected.

2012-04-05 18:42
by ataylor
Ads