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.


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)


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.