1

Please dont suggest expand-region. It regularly expands onto symbols which I don't intend to mark. I am looking for a way instead to simply mark all the text within matching delimiters.

1 Answer 1

2

Apologies if this describes something you've already rejected, but we sometimes overlook the most obvious solutions.

For all of the below examples, point needs to be in front of the opening bracket, quote, or parenthesis. If point is located inside of the pair, the easiest way to get it in place is usually to press "C-M-u", but this depends on what command your major-mode has bound to that key. It also depends on the major-mode's syntax-table (which you can update if necessary).

Marking the surrounded text

For example in emacs-lisp-mode, if you have a line like this:

(message |"The best things in life are free.")

(where "|" is the location of point.)

You can mark the text inside of the quotes like so:

  1. C-M-SPC
  2. C-f
  3. C-x C-x
  4. C-b

Even though this is 4 keystrokes, I find it satisfying in its efficiency and predictability.

If you're using the QWERTY layout, all base keys are on the left-hand side, the result being that you can use one hand to hold control and meta, while the other hand presses the letters.

This becomes a quick movement after you've done it a few times, and the results are more predictable than expand-region.

Alternatives to marking the text

If you already know what you'd like to do after marking the text, you can probably reduce the number of keystrokes by avoiding that step entirely.

Deleting or killing the inner text

To delete or kill/cut the inner text and insert something else inside the pair of punctuation marks, you can just delete everything and replace the pair.

  1. C-M-SPC
  2. C-d to delete, or C-w to kill
  3. "
  • electric-pair-mode automatically places the closing quote after the cursor.
  • If delete-active-region has been set to nil, then "C-d" won't work to delete, so you'll need to use "C-w" to remove the text regardless. (The default value for that variable is t.)

Another option is to use zap-to-char or zap-up-to-char. (The latter has no default keybinding, but "C-z" is a popular one to use, since it matches the M-z binding of the former.)

You could do:

  1. C-f
  2. M-z
  3. .

If there are multiple periods in the text, you can repeat the last two keypresses until you've killed everything (or use "M-k" until only the last period remains, then use "M-z ."). The text will be joined together on the kill-ring.

Or, you can use zap-up-to-char, if you've bound it to "C-z".

  1. C-f
  2. C-z
  3. "

Replacing the paired punctuation

If you want to replace a surrounding pair with a different type of pair (e.g. parentheses to brackets) the delete-pair command may be useful. This is another command that has no default keybinding, but "C-c d" is a good place for it.

Given the same example text and point location above, you could replace the parentheses with square brackets in the following manner.

  1. C-M-u
  2. C-M-SPC
  3. [
  4. C-c d

(with "C-c d" bound to delete-pair)

2
  • thanks a lot for this informative answer. I try to see what works for me. However, C-M-SPC for me just insert a blank character oO
    – CD86
    Commented Jan 16, 2023 at 8:28
  • 1
    Well that's strange, because the default binding of C-M-SPC is mark-sexp since version 22 at the latest (but probably before that). You can see what command it's currently bound to by entering C-h k C-M-SPC. Commented Jan 17, 2023 at 20:17

Your Answer

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

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