qed
qed

Reputation: 23154

How to select text between quotes, brackets... in Emacs?

In vim, you can do this by vi", vi[, vi( ...

For example, if you have a line like this:

x = "difference between vim and emacs"

and the cursor is anywhere between those quotes and you hit vi", then the string will be visually selected.

Upvotes: 9

Views: 6417

Answers (6)

yPhil
yPhil

Reputation: 8387

(defun select-text-in-delimiters ()
  "Select text between the nearest left and right delimiters."
  (interactive)
  (let (start end)
    (skip-chars-backward "^<>([{\"'")
    (setq start (point))
    (skip-chars-forward "^<>)]}\"'")
    (setq end (point))
    (set-mark start)))

Upvotes: 2

Drew
Drew

Reputation: 30708

Use libraries thingatpt+.el and thing-cmds.el.

You will find there commands such as thing-region, which selects a thing at point as the region. Since string-contents and list-contents are defined as things (in thingatpt+.el), these select the string/list contents as the region:

(thing-region "string-contents") ; Select the contents of the string at point.
(thing-region "list-contents")   ; Select the contents of the list at point.

Or interactively:

M-x thing-region RET string-contents RET

Other, related commands include:

  • C-M-U (aka C-M-S-u) -- mark-enclosing-list: Select enclosing list. Repeat to expand list levels. Works with any balanced-parenthesis expressions (e.g., vectors): whatever the current syntax table defines as having balanced-delimiter syntax.

  • mark-thing -- Select successive things of a given kind (repeating).

  • next-visible-thing -- Move to the next visible thing of a given kind (repeating).

Upvotes: 0

Dan
Dan

Reputation: 5369

Late to the party, but you can also use Evil mode, which does a bang-up job of Vim emulation, including the motion commands you mentioned.

Upvotes: 5

Andreas R&#246;hler
Andreas R&#246;hler

Reputation: 4804

On top of the toolkit

https://launchpad.net/s-x-emacs-werkstatt/+download

the following keys/commands are delivered:

(global-set-key [(super \))] 'ar-parentized-atpt)
(global-set-key [(super \])] 'ar-bracketed-atpt)
(global-set-key [(super \})] 'ar-braced-atpt)
(global-set-key [(super \")] 'ar-doublequoted-atpt)
(global-set-key [(super \')] 'ar-singlequoted-atpt)

That way with a couple of more chars known as delimiters will constitute commands.

ar-delimited-atpt will return the string around point found by nearest delimiter.

A group of more powerful commands allows re-using keys like that

(global-set-key [(control c)(\")] 'ar-doublequote-or-copy-atpt)
(global-set-key [(control c)(\')] 'ar-singlequote-or-copy-atpt)
(global-set-key [(control c)(<)] 'ar-lesser-angle-or-copy-atpt)
(global-set-key [(control c)(>)] 'ar-greater-angle-or-copy-atpt)

Here a doctring given as example:

ar-doublequote-or-copy-atpt is an interactive Lisp function in
`thing-at-point-utils.el'.

It is bound to C-c ".

(ar-doublequote-or-copy-atpt &optional NO-DELIMITERS)

If region is highlighted, provide THING at point with doublequote(s),
  otherwise copy doublequote(ed) at point.
  With C-u, copy doublequote(ed) without delimiters. 
  With negative argument kill doublequote(ed) at point. 

Upvotes: 1

Kyle Meyer
Kyle Meyer

Reputation: 1586

The package expand-region is convenient for this. Calling er/expand-region with the point inside the quotes will mark the nearest word, and then calling it again will mark all the words inside the quotes. (Calling it a third time will expand the region to include the quotes.)

I have it bound to C-;.

(global-set-key (kbd "C-;") 'er/expand-region)

With this binding, pressng C-; C-; will highlight the text between the quotes.

Upvotes: 18

Rahul
Rahul

Reputation: 77926

From Emacs Documentation

Nearly all modes support “(,)” as parentheses, and most also support square brackets “[,]” and curly brackets “{,}”. However, you can make any pair of characters a parenthesis-pair, by using the following command:

(modify-syntax-entry ?^ "($")
(modify-syntax-entry ?$ ")^")

Also, take a look at this post How to mark the text between the parentheses in Emacs?. key combination given per this post

Try the key sequence C-M-u C-M-SPC (i.e., while holding the Control and Meta keys, press u and Space in sequence), which executes the commands backward-up-sexp and mark-sexp

Upvotes: 9

Related Questions