Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 987
qed Avatar asked Jun 06 '14 20:06

qed


5 Answers

(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)))
like image 125
yPhil Avatar answered Sep 19 '22 04:09

yPhil


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

like image 31
Rahul Avatar answered Oct 24 '22 20:10

Rahul


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.

like image 16
Kyle Meyer Avatar answered Oct 24 '22 18:10

Kyle Meyer


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.

like image 4
Dan Avatar answered Oct 24 '22 20:10

Dan


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. 
like image 1
Andreas Röhler Avatar answered Oct 24 '22 20:10

Andreas Röhler