7

I've used XCode and Netbeans, and I've noticed that they have a feature to automatically complete quotation marks or parentheses (and I assume that other IDEs often do this also). I don't know what the name of this feature is, but is there any way to do this in Emacs?

For example, when I type

printf("

I would like it to automatically input

printf("")

placing the cursor in between the quotation marks.

Thank you.

Wooble
  • 87,717
  • 12
  • 108
  • 131
beardc
  • 20,283
  • 17
  • 76
  • 94
  • I used such a macro before, but I use so many different editors today (emacs, vs, eclipse, pn) that I couldn't rely on such a feature. Instead I internalized this behavior and can barely type a left-side character without reflexively inserting the corresponding balanced character. Non-programmers that see me doing this think I'm weird. – Zano Oct 22 '10 at 08:47

6 Answers6

8

The basic variant would be AutoPairs. The same effect but a little more sophisticated can also be achieved with YASnippet.

Jacob Oscarson
  • 6,363
  • 1
  • 36
  • 46
  • It seemed like AutoPairs and Paredit are the two popular suggestions, but it looks like AutoPairs is made to integrate well with YASnippet, which is nice for further code-completion. – beardc Oct 23 '10 at 13:15
3

If you type M-(, that will insert both a ( and a ), and leave point in between; if you then type M-), that will move point across the closing ). I use this all the time.

There is also a mode called "paredit" (available from http://mumble.net/~campbell/emacs/paredit.el) which does this sort of thing for quotes as well, and probably other stuff.

offby1
  • 6,767
  • 30
  • 45
  • In my configuraton, `M-)` is bound to `move-past-close-and-reindent`, i.e. it opens up a new line. Is there a possibility to just move past the enclosing parenthese without reindenting? – phimuemue Oct 22 '10 at 06:10
  • `M-- C-M-u` might do what you want. – offby1 Oct 24 '10 at 19:52
3

Paredit-mode inserts matching closing elements by default, so the while typing you'll see something like printf() then printf("") and the cursor would be positioned inside quotes.

eGlyph
  • 1,117
  • 6
  • 11
2

I'm using code from http://cmarcelo.wordpress.com/2008/04/26/a-little-emacs-experiment/ to do "electric pairs". As I descibe in my blog other modes have problems with Python's triple quoted strings. (A Python peculiarity)

Matt Harrison
  • 1,225
  • 11
  • 12
2

My 5 cents here as well.

(setq skeleton-pair t)
(defvar skeletons-alist
  '((?\( . ?\))
    (?\" . ?\")
    (?[  . ?])
    (?{  . ?})
    (?$  . ?$)))

(global-set-key (kbd "(") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "[") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "\"") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "\'") 'skeleton-pair-insert-maybe)

Next advice will enable the backspace to deletes the pairs: a(|)b -> ab

(defadvice delete-backward-char (before delete-empty-pair activate)
  (if (eq (cdr (assq (char-before) skeletons-alist)) (char-after))
      (and (char-after) (delete-char 1))))

Next advice will make backward-kill-word (for me is M-backspace) to delete matching par even if it separated by other text; very handy.

(defadvice backward-kill-word (around delete-pair activate)
  (if (eq (char-syntax (char-before)) ?\()
      (progn
 (backward-char 1)
 (save-excursion
   (forward-sexp 1)
   (delete-char -1))
 (forward-char 1)
 (append-next-kill)
 (kill-backward-chars 1))
    ad-do-it))

I am trying to move now to paredit, though.

VitoshKa
  • 8,387
  • 3
  • 35
  • 59
  • Since Autopairs doesn't seem to automatically delete pairs, this backwards delete pairs advice looks like a great supplement. – beardc Oct 23 '10 at 13:18
0

The autopair minor mode does exactly what you ask for.

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117