1

I very often encounter the problem that I have to cut out a part of an expression and need to copy it somerhere else. For example the following python line:

courtage_text = data["expose"]["brokerage"]["courtage"]["text"].lower()

What I want to do is cut out

data["expose"]["brokerage"]["courtage"]["text"]

What I do very often is to move the point to the beginning of what I want to cut out and then do M-f a couple times to move where I want and then C-w

Is there a faster way of selecting what I want? I think this is a common problem and relevant to other languages as well.

4
  • 1
    You may be interested in trying the packes avy and expand-region.
    – Basil
    Oct 2, 2017 at 21:19
  • 2
    FWIW, moving by balanced expressions (e.g. C-M-f / C-M-b) is slightly more accurate than moving by words (e.g. M-f / M-b) in such cases.
    – Basil
    Oct 2, 2017 at 21:32
  • Another way: with point at the beginning of 'data', hit C-M-SPC (mark-sexp) repeatedly until it has marked what you want, then C-w to kill it. But in this case I think M-z . is the fastest, as @tripleee has commented below.
    – glucas
    Oct 3, 2017 at 19:55
  • What I found best up to now is the solution provided by @Basil. expand-region works very well and is very fast.
    – daeda
    Oct 3, 2017 at 20:58

3 Answers 3

1

If using evil you can move point to the d and type dt.

Another way I often do stuff like this in evil is move point to the d and then go in to visual state with v then move point to where I want (usually using ace-jump) and then type x

Some advantages of the 2nd way are:

  • You don't have to bother thinking of just the right character to put after the dt of the first method. You could just jump to the approximate location and move your visual selection.

  • It works for skipping multiple instances of that character (like if you had multiple periods in between the one you wanted to delete until).

  • It works across multiple lines.

The advantage of the 1st way is that it's quicker, as long as you don't have to jump too far or think too much about exactly which character you want to stop at.

2
  • 2
    For users unfamiliar with evil and perhaps unwilling to subject themselves to it, dt. is M-z . in regular Emacs, plain and simple, except it zaps to just before the dot.
    – tripleee
    Oct 3, 2017 at 7:25
  • By default M-z runs zap-to-char which kills up to and including the character you give it. There is also zap-up-to-char from misc.el (it comes with Emacs, but isn't loaded by default) which kills until right before the character you give it. Personally I find the latter useful more often.
    – Omar
    Oct 4, 2017 at 2:15
0

Here's the simplest way to do it that I found. With the point to the beginning of what you want to cut. You are going to use the zap-to-char command with an argument. Use C-u 4 to give it an argument of 4 and then use M-z to call zap-to-char. Enter ] <RET>. That should work.

0

I have shortcuts for moving between whitespaces:

(bind-key "M-o"
  (lambda ()
    "Move to next whitespace."
    (interactive "^")
    (skip-chars-forward "^\n\t ") (skip-chars-forward "\n\t ")
    ))
(bind-key "M-y"
   (lambda ()
    "Move to previous whitespace."
    (interactive "^")
    (skip-chars-backward "\n\t ") (skip-chars-backward "^\n\t ")
    ))

In your example, there is a whitespace before data[..., so I can first move to . (e.g., by going to the end of line and move backward few words) and then jump to data with the above shortcut.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.