dapias
dapias

Reputation: 2632

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?

Upvotes: 5

Views: 887

Answers (2)

phils
phils

Reputation: 73345

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.

Upvotes: 2

elethan
elethan

Reputation: 17003

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.

Upvotes: 7

Related Questions