• Stars
    star
    2,221
  • Rank 19,930 (Top 0.5 %)
  • Language
    Emacs Lisp
  • Created over 12 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Multiple cursors for emacs.

Build Status MELPA MELPA Stable NonGNU ELPA

multiple-cursors.el

Multiple cursors for Emacs. This is some pretty crazy functionality, so yes, there are kinks. Don't be afraid though, I've been using it since 2011 with great success and much merriment.

Maintenance warning

I use this package every day, and have been doing so for years. It just works. At least, it works for all my use cases. And if it breaks somehow, I fix it.

However, it has become painfully clear to me that I don't have time to fix problems I don't have. It's been years since I could keep pace with the issues and pull requests. Whenever I try, I keep getting feedback that my fix isn't good enough by some standard I don't particularly care about.

So, I have closed the issue tracker and the pull requests. I hope you can happily use this package, just like I do. If it doesn't work for you, then I'm sorry. Thankfully Emacs is infinitely malleable, you can probably fix it yourself.

TLDR: I am still maintaining this package, but I am no longer crowdsourcing a list of issues.

Installation

I highly recommend installing multiple-cursors through package.el.

It's available on MELPA, MELPA Stable and NonGNU ELPA (enabled by default from Emacs 28 onwards):

M-x package-install multiple-cursors

The package depends on the cl-lib package, so if you do not use package.el or have a recent Emacs, you would need to install that too: see GNU ELPA.

Basic usage

Start out with:

(require 'multiple-cursors)

Then you have to set up your keybindings - multiple-cursors doesn't presume to know how you'd like them laid out. Here are some examples:

When you have an active region that spans multiple lines, the following will add a cursor to each line:

(global-set-key (kbd "C-S-c C-S-c") 'mc/edit-lines)

When you want to add multiple cursors not based on continuous lines, but based on keywords in the buffer, use:

(global-set-key (kbd "C->") 'mc/mark-next-like-this)
(global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-c C-<") 'mc/mark-all-like-this)

First mark the word, then add more cursors.

To get out of multiple-cursors-mode, press <return> or C-g. The latter will first disable multiple regions before disabling multiple cursors. If you want to insert a newline in multiple-cursors-mode, use C-j.

Video

You can watch an intro to multiple-cursors at Emacs Rocks.

Command overview

Mark one more occurrence

  • mc/mark-next-like-this: Adds a cursor and region at the next part of the buffer forwards that matches the current region.
  • mc/mark-next-like-this-word: Adds a cursor and region at the next part of the buffer forwards that matches the current region, if no region is selected it selects the word at the point.
  • mc/mark-next-like-this-symbol: Adds a cursor and region at the next part of the buffer forwards that matches the current region, if no region is selected it selects the symbol at the point.
  • mc/mark-next-word-like-this: Like mc/mark-next-like-this but only for whole words.
  • mc/mark-next-symbol-like-this: Like mc/mark-next-like-this but only for whole symbols.
  • mc/mark-previous-like-this: Adds a cursor and region at the next part of the buffer backwards that matches the current region.
  • mc/mark-previous-like-this-word: Adds a cursor and region at the next part of the buffer backwards that matches the current region, if no region is selected it selects the word at the point.
  • mc/mark-previous-like-this-symbol: Adds a cursor and region at the next part of the buffer backwards that matches the current region, if no region is selected it selects the symbol at the point.
  • mc/mark-previous-word-like-this: Like mc/mark-previous-like-this but only for whole words.
  • mc/mark-previous-symbol-like-this: Like mc/mark-previous-like-this but only for whole symbols.
  • mc/mark-more-like-this-extended: Use arrow keys to quickly mark/skip next/previous occurrences.
  • mc/add-cursor-on-click: Bind to a mouse event to add cursors by clicking. See tips-section.
  • mc/mark-pop: Set a cursor at the current point and move to the next (different) position on the mark stack. This allows for fine grained control over the placement of cursors.

Juggle around with the current cursors

  • mc/unmark-next-like-this: Remove the cursor furthest down in the buffer.
  • mc/unmark-previous-like-this: Remove the cursor furthest up in the buffer.
  • mc/skip-to-next-like-this: Remove the cursor furthest down, marking the next occurrence down.
  • mc/skip-to-previous-like-this: Remove the cursor furthest up, marking the next occurrence up.

Mark many occurrences

  • mc/edit-lines: Adds one cursor to each line in the current region.
  • mc/edit-beginnings-of-lines: Adds a cursor at the start of each line in the current region.
  • mc/edit-ends-of-lines: Adds a cursor at the end of each line in the current region.
  • mc/mark-all-like-this: Marks all parts of the buffer that matches the current region.
  • mc/mark-all-words-like-this: Like mc/mark-all-like-this but only for whole words.
  • mc/mark-all-symbols-like-this: Like mc/mark-all-like-this but only for whole symbols.
  • mc/mark-all-in-region: Prompts for a string to match in the region, adding cursors to all of them.
  • mc/mark-all-like-this-in-defun: Marks all parts of the current defun that matches the current region.
  • mc/mark-all-words-like-this-in-defun: Like mc/mark-all-like-this-in-defun but only for whole words.
  • mc/mark-all-symbols-like-this-in-defun: Like mc/mark-all-like-this-in-defun but only for whole symbols.
  • mc/mark-all-dwim: Tries to be smart about marking everything you want. Can be pressed multiple times.

Special

  • set-rectangular-region-anchor: Think of this one as set-mark except you're marking a rectangular region.
  • mc/mark-sgml-tag-pair: Mark the current opening and closing tag.
  • mc/insert-numbers: Insert increasing numbers for each cursor, top to bottom.
  • mc/insert-letters: Insert increasing letters for each cursor, top to bottom.
  • mc/sort-regions: Sort the marked regions alphabetically.
  • mc/reverse-regions: Reverse the order of the marked regions.
  • mc/vertical-align: Aligns all cursors vertically with a given CHARACTER to the one with the highest column number (the rightest). (Might not behave as intended if more than one cursors are on the same line.)
  • mc/vertical-align-with-space: Aligns all cursors with whitespace like mc/vertical-align does.

Tips and tricks

  • To get out of multiple-cursors-mode, press <return> or C-g. The latter will first disable multiple regions before disabling multiple cursors. If you want to insert a newline in multiple-cursors-mode, use C-j.

  • (define-key mc/keymap (kbd "<return>") nil) will make <return> insert a newline; multiple-cursors-mode can still be disabled with C-g.

  • Sometimes you end up with cursors outside of your view. You can scroll the screen to center on each cursor with C-v and M-v or you can press C-' to hide all lines without a cursor, press C-' again to unhide.

  • Try pressing mc/mark-next-like-this with no region selected. It will just add a cursor on the next line.

  • Try pressing mc/mark-next-like-this-word or mc/mark-next-like-this-symbol with no region selected. It will mark the word or symbol and add a cursor at the next occurrence

  • Try pressing mc/mark-all-like-this-dwim on a tagname in html-mode.

  • Notice that the number of cursors active can be seen in the modeline.

  • If you get out of multiple-cursors-mode and yank - it will yank only from the kill-ring of main cursor. To yank from the kill-rings of every cursor use yank-rectangle, normally found at C-x r y.

  • You can use mc/reverse-regions with nothing selected and just one cursor. It will then flip the sexp at point and the one below it.

  • When you use mc/edit-lines, you can give it a positive or negative prefix to change how it behaves on too short lines.

  • If you would like to keep the global bindings clean, and get custom keybindings when the region is active, you can try region-bindings-mode.

  • There is a special hook that is run when the mode is diabled (which is equivalent to the number of cursors going back to 1): multiple-cursors-mode-disabled-hook

BTW, I highly recommend adding mc/mark-next-like-this to a key binding that's right next to the key for er/expand-region.

Binding mouse events

To override a mouse event, you will likely have to also unbind the down-mouse part of the event. Like this:

(global-unset-key (kbd "M-<down-mouse-1>"))
(global-set-key (kbd "M-<mouse-1>") 'mc/add-cursor-on-click)

Or you can do like me and find an unused, but less convenient, binding:

(global-set-key (kbd "C-S-<mouse-1>") 'mc/add-cursor-on-click)

Unknown commands

Multiple-cursors uses two lists of commands to know what to do: the run-once list and the run-for-all list. It comes with a set of defaults, but it would be beyond silly to try and include all the known Emacs commands.

So that's why multiple-cursors occasionally asks what to do about a command. It will then remember your choice by saving it in ~/.emacs.d/.mc-lists.el. You can change the location with:

(setq mc/list-file "/my/preferred/file")

NB! Make sure to do so before requiring multiple-cursors.

It is possible to set multiple-cursors to "run-for-all" for every command except for those that are listed in mc/cmds-to-run-once. To enable this set mc/always-run-for-all to non-nil. Add commands to be run once to mc/cmds-to-run-once in ".mc-lists.el".

Known limitations

  • isearch-forward and isearch-backward aren't supported with multiple cursors. If you want this functionality, you can use phi-search.
  • All key bindings that refer to lambdas are always run for all cursors. If you need to limit it, you will have to give it a name.
  • Redo might screw with your cursors. Undo works very well.

Contribute

Yes, please do. There's a suite of tests, so remember to add tests for your specific feature, or I might break it later.

You'll find the repo at:

https://github.com/magnars/multiple-cursors.el

To fetch the test dependencies, install cask if you haven't already, then:

$ cd /path/to/multiple-cursors
$ cask

Run the tests with:

$ ./run-tests.sh

Contributors

  • Takafumi Arakaki has contributed several small improvements
  • Marco Baringer contributed looping to mc/cycle and adding cursors without region for mark-more.
  • Ivan Andrus added showing number of cursors in mode-line, and different options for how to handle short lines in mc/edit-lines.
  • Fuco added the first version of mc/mark-all-like-this-dwim
  • Zach Kost-Smith added mc/mark-pop
  • Maciej Katafiasz added mc/mark-all-dwim
  • Aleksey Fedotov added mc-hide-unmatched-lines-mode
  • Jules Tamagnan added mc/mark-next-like-this-word and mc/mark-next-like-this-symbol
  • Ingo Lohmar extended mc/add-cursor-on-click to toggle cursors.
  • Andrea Orru added mc/mark-previous-like-this-word/-symbol

Thanks!

License

Copyright (C) 2012-2016 Magnar Sveen

Author: Magnar Sveen [email protected] Keywords: editing cursors

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

More Repositories

1

dash.el

A modern list library for Emacs
Emacs Lisp
1,632
star
2

expand-region.el

Emacs extension to increase selected region by semantic units.
Emacs Lisp
1,334
star
3

s.el

The long lost Emacs string manipulation library.
Emacs Lisp
1,023
star
4

prone

Better exception reporting middleware for Ring.
Clojure
511
star
5

optimus

A Ring middleware for frontend performance optimization.
Clojure
364
star
6

stasis

Some Clojure functions for creating static websites.
Clojure
345
star
7

parens-of-the-dead

A series of zombie-themed games written with Clojure and ClojureScript.
Clojure
152
star
8

mark-multiple.el

An extension to emacs that sorta lets you mark several regions at once
Emacs Lisp
114
star
9

string-edit.el

Avoid escape nightmares by editing strings in a separate buffer
Emacs Lisp
100
star
10

tagedit

A collection of paredit-like functions for editing in html-mode.
Emacs Lisp
96
star
11

multifiles.el

Work in progress: View and edit parts of multiple files in one buffer
Emacs Lisp
74
star
12

fold-this.el

Fold the active region in Emacs
Emacs Lisp
71
star
13

change-inner.el

Emacs version of vim's ci and co commands
Emacs Lisp
60
star
14

angular-snippets.el

Yasnippets for AngularJS
Emacs Lisp
45
star
15

autolint

Autolint watches your files for jslint-errors
JavaScript
43
star
16

kaocha-runner.el

An emacs package for running Kaocha tests via CIDER.
Emacs Lisp
32
star
17

smart-forward.el

semantic navigation based on expand-region.el
Emacs Lisp
31
star
18

hardcore-mode.el

Disable arrow keys + optionally backspace and return
Emacs Lisp
28
star
19

java-time-literals

A Clojure library that defines literals for java.time classes.
Clojure
27
star
20

confair

Confair is a configuration library for Clojure.
Clojure
26
star
21

parens-of-the-dead-s2

The code for Parens of the Dead season 2
Clojure
21
star
22

yesql-ghosts

Display ghostly yesql defqueries inline, in Emacs
Emacs Lisp
18
star
23

catenate

A deprecated Ring middleware to serve concatenated static files with cache buster URLs in production.
Clojure
16
star
24

emacsd-reboot

Reboot of .emacs.d
Emacs Lisp
16
star
25

emacsrocks.com

The emacsrocks.com site implemented in Clojure with Stasis and Optimus
Clojure
15
star
26

realize

Realizing clojure data structures, no more laziness
Clojure
15
star
27

test-with-files

A Clojure library to easily write tests with files.
Clojure
15
star
28

datomic-type-extensions

A Clojure library that wraps Datomic API functions to add type extensions.
Clojure
14
star
29

datoms-differ

Find the diff between two txes in datoms.
Clojure
14
star
30

datomic-snippets

Yasnippets for Datomic.
Emacs Lisp
14
star
31

mapdown

A lightweight markup format to turn strings into maps in Clojure.
Clojure
13
star
32

cljs-styles

Vendor prefixes for React inline styles with ClojureScript.
Clojure
13
star
33

html5-walker

Search and replace in HTML5 strings.
Clojure
12
star
34

zombie-clj

An zombie themed web game made with Clojure and ClojureScript
Clojure
12
star
35

what-the-emacsd

The code for http://whattheemacsd.com
HTML
11
star
36

www.parens-of-the-dead.com

The code that generates parens-of-the-dead.com
Clojure
11
star
37

kaocha-noyoda

Don't talk like yoda. This kaocha plugin lets you write `(is (= actual expected))`.
Clojure
11
star
38

Zombie-TDD

Bli med når jeg lager et nettspill fra scratch med javascript på både frontend og backend. Det blir zombier, mafia og testdrevet utvikling.
JavaScript
10
star
39

annoying-arrows-mode.el

Emacs gets annoyed when you navigate around your document one char at a time.
Emacs Lisp
10
star
40

helpful-loader

A Clojure library to load resources with helpful error messages.
Clojure
9
star
41

buster-mode

A minor mode for emacs to speed up development when writing tests with Buster.JS
Emacs Lisp
8
star
42

stubadub

A small stubbing library for Clojure and ClojureScript
Clojure
8
star
43

bang.el

Used to be a modern list library for Emacs (renamed to dash)
Emacs Lisp
8
star
44

optimus-angular

Angular.JS optimizations for Optimus
Clojure
8
star
45

buster.tmbundle

TextMate bundle for Buster.js
6
star
46

java-time-dte

Datomic type extensions for java.time classes
Clojure
6
star
47

optimus-img-transform

An Optimus image transformation middleware.
Clojure
6
star
48

simplezen.el

A simple subset of zencoding-mode for Emacs
Emacs Lisp
5
star
49

zombieclj-s02

Koden til ZombieCLJ Sesong 2
CSS
5
star
50

server-facade

Code and slides belonging to my JavaZone-talk about wrapping your ajax-calls to get cleaner code and nicer testing.
JavaScript
4
star
51

naive-xml-reader

A naive Clojure library that turns XML into maps.
Clojure
4
star
52

blockout

Recreating one of my first games for the web to learn Canvas.
JavaScript
4
star
53

norsk-extreme-startup

Oversatt til norsk og tilpassett FINN
Ruby
3
star
54

crappy-jsp-mode

Seriously, this is not a good jsp-mode, it just solves some of my problems
Emacs Lisp
3
star
55

optimus-jsx

A React JSX asset loader for Optimus.
JavaScript
3
star
56

optimus-less

A LESS asset loader for Optimus.
Clojure
3
star
57

zombieclj.no

Koden bak www.zombieclj.no
Clojure
2
star
58

zombieclj-s2

Koden for andre sesong av Zombie CLJ.
CSS
2
star
59

mytomatoes.clj

mytomatoes.com rescue operation
Clojure
2
star
60

creator.js

A tiny library for creating create-methods for your objects.
JavaScript
2
star
61

roll20-scripts

Scripts for roll20
JavaScript
2
star
62

buster-snippets.el

Yasnippets for the Buster test framework.
Emacs Lisp
2
star