How to select text between quotes, brackets... in Emacs? - 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.

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).

Related

How to set a keybinding to create and jump to the next line in emacs?

I have the following code that attempts to create a new line and then jump to it. The idea is that move-end-of-line will jump to the end of the current line, and ["C-m"] would act as return/enter. Yet executing this command gives the error: "wrong number of arguments". How do I fix this?
(global-set-key (kbd "C-.") 'new-line)
(defun new-line ()
(interactive)
(move-end-of-line)
["C-m"]
)
I think you need to read the Emacs & elisp manuals: these questions are pretty easy to answer. Here's one way to do it.
(defun insert-line-after-line (&optional n)
(interactive "p")
(end-of-line 1) ;end of current line
(open-line n) ;open n new lines
(forward-line 1)) ;go to start of first of them
But seriously: Emacs has very extensive self-documentation, it is easy to find out how to do these things.
An option is to record a macro and use that.
M-x kmacro-start-macro
C-e
C-m
M-x kmacro-end-macro
If you don't care about the macro persisting, just run it:
C-x e
But if you want it to persist you would save it:
M-x name-last-kbd-macro new-line
M-x insert-kbd-macro new-line
and paste the output into your initialisation file (with your shortcut definition):
(global-set-key (kbd "C-.") 'new-line)
(fset 'new-line
[end return])
["C-m"] is like the way you specify a key for doing a key binding, but this is not the same as how you programmatically tell Emacs to insert a character into a document. You could use (insert-char ?\^M) (see ref here), but that would result in a literal ^M character (try it; another way to do the same thing interactively is Ctrl-Q followed by Ctrl-M). (insert "\n") seems to be what you're looking for.
Also, the reason you're getting the message "wrong number of arguments" is because (move-end-of-line) requires an argument when called out of interactive context. (move-end-of-line 1) works.
That said, possibly an easier way to achieve the result you're looking for is with the following setting:
(setq next-line-add-newlines t)
This will allow you to just do C-n at the end of the file and not worry about the distinction between moving and inserting.

Setting arbitrary cursor positions with multiple cursors in Emacs?

I want to modify different parts of my file with the same string. So I installed multiple-cursors in emacs. But unfortunately, I cannot do the (simple?) thing of marking different parts of the text and start to edit.
Each command I have checked seems not useful to do what I want. For instance consider I need to edit the beginning of line 10 and a character in the middle of line 34. How do I do that?
It sounds like you are looking for the command mc/add-cursor-on-click, which I personally have bound to C-S-<mouse-1>. With this setup, I can hold Ctrl and Shift and click the beginning of line 10, and then click the middle of line 34, and I will have a cursor in both positions.
You can bind this command the way I have like this in your init:
(global-set-key (kbd "C-S-<mouse-1>") 'mc/add-cursor-on-click)
You can replace the kbd with another combination too if you want.
The following is derived from https://github.com/magnars/multiple-cursors.el/issues/44 and seems like a reasonable keyboard-driven solution.
(require 'multiple-cursors)
(defun mc/toggle-cursor-at-point ()
"Add or remove a cursor at point."
(interactive)
(if multiple-cursors-mode
(message "Cannot toggle cursor at point while `multiple-cursors-mode' is active.")
(let ((existing (mc/fake-cursor-at-point)))
(if existing
(mc/remove-fake-cursor existing)
(mc/create-fake-cursor-at-point)))))
(add-to-list 'mc/cmds-to-run-once 'mc/toggle-cursor-at-point)
(add-to-list 'mc/cmds-to-run-once 'multiple-cursors-mode)
(global-set-key (kbd "C-S-SPC") 'mc/toggle-cursor-at-point)
(global-set-key (kbd "<C-S-return>") 'multiple-cursors-mode)
Firstly use mc/toggle-cursor-at-point to mark each position where you want a cursor, and then invoke multiple-cursors-mode to activate them.

Key binding for § symbol in Emacs

I'm working in Ubuntu, but since the standard way of inserting unicode characters (Ctrl+Shift+U and, after that, the unicode code) doesn't work inside emacs, I've, in my .emacs, some keystrokes for different unicode symbols which I use frequently, for example:
(global-set-key (kbd "C-c b") "☛")
and every symbol works fine, except the symbol §, which is replaced by a simple dash ("-") when I use the corresponding keystroke:
(global-set-key (kbd "C-c y") "§")
The question is, what does it make this symbol different for other symbols and how can I solve my problem?
global-set-key usually expects a function, so this should work:
(global-set-key (kbd "C-c y") (lambda () (interactive) (insert "§")))
But you're better off using the excellent insert-char function:
(global-set-key (kbd "<f2> u") 'insert-char)
It understands hex Unicode as well as text description (with completion and all).
Just press TAB to see the completions.
You can insert unicode chars in emacs by doing C-x8RET<unicode-hex>RET. So to insert § do C-x8RET00A7RET.
You can bind insert-char (the command run by C-x8RET) to a simpler key if you wish, or define a custom function and bind it to a key as follows
(global-set-key (kbd "C-c y") (lambda () (interactive) (insert "§")))
A general solution to the need to insert a number of Unicode chars quickly is to define a command for each, dedicated to inserting it, and bind that command to a simple key sequence.
That is, in effect, what #abo-abo and #Iqbal have offered you, in the form of:
(lambda () (interactive) (insert "§")))
If you have multiple such chars that you want to create such commands for, then
library ucs-cmds.el can help -- in two ways:
It provides a macro, ucsc-make-commands, that lets you, in one fell swoop, define commands for whole ranges of Unicode chars. For example:
(ucsc-make-commands "^cjk") defines an insert-char command for each Chinese, Japanese, and Korean Unicode character.
(ucsc-make-commands "^greek [a-z]+ letter") does the same for each Greek letter.
(ucsc-make-commands "arabic") does the same for each Arabic character.
It provides a replacement command (ucsc-insert) for C-x 8 RET, which acts exactly the same as the vanilla command (insert-char), except that if you give it a negative prefix argument then it not only inserts the char you choose but it also creates an insertion command for it (i.e., the kind of command that the macro creates in bulk).
The names of the commands created by macro ucsc-make-commands and command ucsc-insert are exactly the same as the Unicode names of the chars themselves, except that they are lowercase and have hyphens (-) in place of space chars.

How to select string that is connected with dot on Emacs

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.

Wrapping selecting text in enclosing characters in Emacs

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