Multiple cursors

Multiple cursors is a very nice package that lets you create several cursors that all do the same thing as you type (see the example below). You can add it to emacs using the steps described here Once you have installed it, it is useful to set up a keybinding (a keyboard short-cut) for it. You can do this by adding the following to your emacs config file to set C-c m c as the binding for multiple cursors.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; multiple cursors                                                       ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(global-set-key (kbd "C-c m c") 'mc/edit-lines)

Once you have done this and restarted emacs, you can use multiple cursors.

To use it, highlight the lines on which you wish to have cursors and use C-c m c. Now you can edit away and press enter when you are done to exit multiple cursors. In the following example, I create cursors on 4 lines and then use them to edit the four lines simultaneously.

emacs-mc1.gif

For a more complex example, I use multiple cursors to reformat a latex style table into plain text. In this clip I do the following

  1. First make the column headings that I want
  2. highlight the lines of the table and start multiple-cursors
  3. edit the line, making use of M-f and M-b to move the cursors forward and back by word, which is useful when the text is not aligned
  4. exit multiple cursors and highlight the table again
  5. use C-u M-x align to align the columns of the table (more tips on aligning text to follow)

emacs-mc2.gif

Of course, there are lots of other ways to accomplish tasks like the above (e.g. using macros or regular expressions in query-replace, or using iedit), many of which we will look at in the future, but I like multiple cursors as a nice visual way of making these sorts of edits.

Note that sometimes when you type a command with multiple cursors running it will ask you if you want to apply this command to all cursors – you can answer yes or no to this and it will remember your choice.

For more examples, see this video.

  • rofh

    Hi. Thanks for the tip, I find multiple cursors easier to work with than rectangulars in simple cases.

    Just wanted to add my 2 cents:

    You write “Once you have done this and restarted emacs, you can use multiple cursors”. Don’t you think it’s an anti-pattern, even (especially?) for beginner emacs users? We have super useful C-x C-e to eval the line, or M-:.

    IMO, there are few reasons to restart emacs at all after reconfiguration, as one can just re-eval .emacs

    cheers

    • Ben Maughan

      Thanks. I have to admit, I don’t really understand when you *need* to restart emacs. After minor changes to to my config file, I just evaluate them, but after installing/upgrading packages I tend to restart. In the past I have found some packages need a restart and some do not, though I don’t know why. Any clarification of this would be very appreciated.

      With desktop-save-mode I find restarting to be quite painless in any case.

  • emarsk

    * lets you create

    • Ben Maughan

      D’oh. Thank’s.

      • emarsk

        Your welco’me 🙂

  • The contents of mc/cmds-to-run-for-all get backed up in ~/.emacs.d. Be sure to add it to source control.

  • Pingback: Emacs Lisp: Prográmame esta | El Tao Del Hao()