Setting arbitrary cursor positions with multiple cursors in Emacs? - 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.

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.

How to insert new line (after cursor point) with indentation?

I works in programming mode such as javascript-mode and usually need to push some lines down for formatting purpose.
I find C-o convenient to insert a newline after cursor point. However the line pushed down loses its indentation.
I find RET convenient to insert newline. And the line pushed down is nicely indented. However, the inserted new-line comes after the current cursor point. (EDIT: ) I find it is convenient to keep the cursor position by inserting the newline after it, because sometimes I still need to modify the current line.
Probably something like this:
(defun open-line-and-indent ()
"Like `newline-and-indent', but do not move the point."
(interactive)
(save-excursion
(newline-and-indent)))
(global-set-key (kbd "C-o") #'open-line-and-indent)
;; (define-key javascript-mode-map (kbd "C-o") #'open-line-and-indent)

How to insert space after cursor?

I am a new user of emacs.
I found some useful options that allows me to insert new line before or after cursor: C-j (before cursor), C-o (after cursor).
I found this very convenient in doing formatting text across lines.
Now, are there methods to insert space after cursor for in-line formatting?
Currently I have to insert space before cursor using Space then C-b multiple times just to return to the original position when doing formatting within one line.
I don't think there is such a function, but it is easy to write:
(defun my-insert-space-after-point ()
(interactive)
(save-excursion (insert " ")))
(global-set-key (kbd "C-.") 'my-insert-space-after-point)
This binds the function to C-.; adjust to preference.
Another way to do this is to record a macro, save it, and bind it to a key. The steps to do that are described in this answer.

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

Assign multiple Emacs keybindings to a single command?

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.