In Textmate I can wrap enclosing characters ('(', '[', '"', etc.) around text by selecting it and hitting the opening character. For example, if I select word and hit (, it will become (word). What does Emacs call this feature and how do I enable it?
For parens you can do M-(. For brackets/braces/quotes you could do:
(global-set-key (kbd "M-[") 'insert-pair)
(global-set-key (kbd "M-{") 'insert-pair)
(global-set-key (kbd "M-\"") 'insert-pair)
Note that if you don't have a region highlighted, it will just insert the pair of whatevers and put the cursor in between them. Also handy for deleting matching whatevers is
(global-set-key (kbd "M-)") 'delete-pair)
EDIT:
Good point in the comments about overriding backward-paragraph. You could bind it to C-{, which might interfere with something in a major mode. insert-pair takes the last key and does a lookup to see what pair to insert, so if you don't want to bind it to something-{ you could bind to this function instead:
(defun my-insert-braces ()
(interactive)
(if (region-active-p)
(insert-pair 1 ?{ ?})
(insert "{}")
(backward-char)))
I use http://www.emacswiki.org/emacs/ParEdit. M-( does exactly this.
Since Emacs 24.1(released 2012-06).
Put this in your emacs init: (electric-pair-mode 1).
Now If you select a word and hit (, it will become (word). Same for ", [, { etc.
Autopair is the best one of these tools
You can have a look at wrap-region.
I'd take a look also at skeleton-mode
http://ggorjan.blogspot.com/2007/05/skeleton-pair-mode-in-emacs.html
It's very flexible expecially for parentheses.
There is textmate-mode.
From Emacswiki:
See textmate-mode for an attempt of having the TextMate behaviour for parenthesis and quotes (auto-closing, overwriting, smart delete).
http://code.google.com/p/emacs-textmate/
There's now Corral as well. Its "do what I mean" behavior makes this process a lot faster than manually selecting the text and hitting the key.
(disclaimer: I'm the author)
If you use smartparens just select the text and then type the pair. Smartparens wiki: Wrapping
Related
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.
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.
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
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.
(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)))
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.
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).
I want to select the whole word connected with dot.
Is there any function in Emacs doing this.
Example:
123.456.[7]89
Cursor is on "7".
Apply "???function".
Region "123.456.789" is selected.
[123.456.789]
Is there "???function" in Emacs?
I use expand-region:
(require 'expand-region)
(global-set-key (kbd "C-=") 'er/expand-region)
so I press C-= once and it selects 789; I press it a second time and it selects 123.456.789. It works nice with strings, lines, statements from different languages.
Home: https://github.com/magnars/expand-region.el
Install it with ELPA (M-x list-packages).
ps: http://wikemacs.org/index.php/Elpa
Here's a solution:
(defun mark-whole-word ()
(interactive)
(let ((table (syntax-table)))
(modify-syntax-entry ?. "w" table)
(with-syntax-table table
(backward-word)
(set-mark (point))
(forward-word))))
The key here is modifying the syntax.
So if you replace ?. with ?- above, you can mark similarly
123-456-789.
#abo-abo's answer is good.
Just as another data point, you can also use command thing-region (from library thing-cmds.el -- Thing-At-Point Commands) for this. It prompts you for a type of THING to select. Just accept the default THING type, sexp, and you get what you requested in this case.
Cannot get to bind Enter to newline-and-indent in Emacs !!! Very annoying.
I already tried everything on the following thread by changing 'mode' to ruby and still nothing:
How do I make Emacs auto-indent my C code?
I know that the problem is the RETURN key, since if I bind to something else, works fine.
I tried [enter], (kbd "enter"), (read-kbd-macro "enter"), (kbd "RET")
Follow-up 1.
This is what I get from C-hkRET
RET runs the command newline, which is an interactive compiled Lisp
function.
It is bound to RET.
(newline &optional ARG)
Insert a newline, and move to left margin of the new line if it's blank.
If use-hard-newlines' is non-nil, the newline is marked with the
text-propertyhard'.
With ARG, insert that many newlines.
Call auto-fill-function' if the current column number is greater
than the value offill-column' and ARG is nil.
I dont know what to make of it or how to figure out if it's a global
or local binding that gets in the way. trying to remap C-j
also doesnt work.
As a previous comment says, use C-h k (describe-key) to see what the key is bound to at the point when it's not doing what you want. The (kbd "foo") syntax will be correct for whichever foo describe-key refers to it as.
Chances are that you are simply not defining that key in the appropriate keymap.
Note that major and minor mode keymaps take precedence over the global keymap, so you shouldn't necessarily be surprised if a global binding is overridden.
edit:
Myself, I have a hook function for common behaviours for all the programming modes I use, and it includes the sort of remapping you're after. The relevant part looks like this:
(defun my-coding-config ()
(local-set-key (kbd "RET") (key-binding (kbd "M-j")))
(local-set-key (kbd "<S-return>") 'newline)
)
(mapc
(lambda (language-mode-hook)
(add-hook language-mode-hook 'my-coding-config))
'(cperl-mode-hook
css-mode-hook
emacs-lisp-mode-hook
;; etc...
))
See Daimrod's answer for the explanation of why I'm re-binding RET to the current binding of M-j -- although I'm using comment-indent-new-line (or similar) instead of newline-and-indent (or similar), which does what I want in both comments and non-comments.
In Emacs 24, programming modes seem to derive from prog-mode, so you could probably (un-tested) reduce that list to prog-mode-hook plus any exceptions for third-party modes which don't yet do that.
As said earlier, use C-hkC-j because
C-j is the standard key to do newline-and-indent.
If you open a new file, activate ruby-mode and try the previous
command you will see why it doesn't work. Because ruby-mode doesn't
have newline-and-indent but rather
reindent-then-newline-and-indent. Yes that's stupid but you can either ask
to the maintener to change it, or accept it.
However I suggest you to use C-j to do it because
ruby-mode is not the only mode to do so, like paredit-mode which
uses paredit-newline.
I'm giving ErgoEmacs mode a try to see if I can use Emacs more comfortably. Some of its keybindings are fairly intuitive, but in many cases I don't want to outright replace the defaults.
For example, in the context of ErgoEmacs' navigation shortcut structure, M-h makes sense as a replacement for C-a--but I want to be able to use both, not just M-h. I tried simply duplicating the commands:
;; Move to beginning/ending of line
(defconst ergoemacs-move-beginning-of-line-key (kbd "C-a")) ; original
(defconst ergoemacs-move-end-of-line-key (kbd "C-e")) ; original
(defconst ergoemacs-move-beginning-of-line-key (kbd "M-h")) ; ergoemacs
(defconst ergoemacs-move-end-of-line-key (kbd "M-H")) ; ergoemacs
But Emacs simply overwrites the first keybinding with the second. What's the best way to address this?
To re-post reply from ergo-emacs mailing list:
Xah Lee said:
that's very easy.
in the
ergoemacs-mode.el file, there's this
line (load "ergoemacs-unbind") just
comment it out. That should be all
you need to do. However, note that
ErgoEmacs keybinding defines those
common shortcuts such as Open, Close,
New, Save... with keys Ctrl+o,
Ctrl+w, Ctrl+n, Ctrl+s etc. About 7 of
them or so. So, i think some of these
will hit on emacs traditional
bindings with Ctrl. if you are new to
ErgoEmacs and trying to explore it,
you might just try starting with few
keys. this page might have some
useful info:
http://code.google.com/p/ergoemacs/wiki/adoption
thanks for checking out ErgoEmacs!
Xah ∑ http://xahlee.org/
As it turns out, ErgoEmacs uses two files to define the keybinding. One is the main ergoemacs-mode.el file, and the other is the specific keyboard layout you select (e.g. ergoemacs-layout-us.el). The latter document creates a constant, which the former uses to create the keybinding. So while I thought I was duplicating the keybinding, I was actually changing the constant which was subsequently used for that purpose.
Solution:
In ergomacs-mode.el:
;; Move to beginning/ending of line
(define-key ergoemacs-keymap ergoemacs-move-beginning-of-line-key 'move-beginning-of-line)
(define-key ergoemacs-keymap ergoemacs-move-end-of-line-key 'move-end-of-line)
(define-key ergoemacs-keymap ergoemacs-move-beginning-of-line-key2 'move-beginning-of-line) ; new
(define-key ergoemacs-keymap ergoemacs-move-end-of-line-key2 'move-end-of-line) ; new
In ergoemacs-layout-us.el:
;; Move to beginning/ending of line
(defconst ergoemacs-move-beginning-of-line-key (kbd "M-h"))
(defconst ergoemacs-move-end-of-line-key (kbd "M-H"))
(defconst ergoemacs-move-beginning-of-line-key2 (kbd "C-a")) ; new
(defconst ergoemacs-move-end-of-line-key2 (kbd "C-e")) ; new
Huh? Is having one and only one way for every function some golden principle of ErgoEmacs? Because normal keybinding works exactly the opposite way: you name one key at a time and specify what it should do. If a mode defines a global variable to mean "the key that end-of-line is bound to", then of course there can be only one value, but with the normal binding commands you can bind the same function to as many combinations as you like. In fact, every keybinding I have ever seen used looked either like this
(global-set-key [(meta space)] 'just-one-space)
or like this
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
(define-key c-mode-map [(control c) b] 'c-insert-block))
if it's only for a specific mode.
How does one make M-( the default behavior for typing an opening "(" character? I want Emacs to automatically insert the closing ")" after the cursor when I type a "(" character regardless of whether it's part of an M-key combination. Additionaly, I want to extend this behavior to quotes, subquotes, brackets and braces. Typing M-( is a pain, and there don't appear to be any comparable forms for those other characters.
I don't write much elisp myself, but this is something I cribbed off somebody. The code goes into your .emacs.
(setq skeleton-pair t)
(setq skeleton-pair-on-word t) ; apply skeleton trick even in front of a word.
(global-set-key "[" 'skeleton-pair-insert-maybe)
(global-set-key "{" 'skeleton-pair-insert-maybe)
(global-set-key "(" 'skeleton-pair-insert-maybe)
(global-set-key "\"" 'skeleton-pair-insert-maybe)
Check out paredit.el which keeps parens/braces/quotes balanced as you wish, and also does offers many other features to assist with s-exp manipulation. If you're going to be writing Lisp code (as your name implies) you will probably want to use this library eventually.
"(" is bound to self-insert-command while M-'(' is insert-parenthesis. You can reverse that simply by using global-set-key or define-key to bind "(" to insert-parenthesis.