Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Multiple Cursors keybinds

The Multiple Cursors package has been given much praise throughout the Emacsphere. It has a smaller use-case than keyboard macros, but it is usually quicker and just plain looks awesome. To make full use of its commands, I combine two of the concepts I've explained here before, rebinding M-number and intuitive keymaps.

I won’t go into what this package is, as Magnar already has a whole video on that. Instead, I’ll just explain how I use it.

Firstly, the following keys make the most sense to me.

(require 'multiple-cursors-core)
;; This is globally useful, so it goes under `C-x', and `m'
;; for "multiple-cursors" is easy to remember.
(define-key ctl-x-map "\C-m" #'mc/mark-all-dwim)
;; Usually, both `C-x C-m' and `C-x RET' invoke the
;; `mule-keymap', but that's a waste of keys. Here we put it
;; _just_ under `C-x RET'.
(define-key ctl-x-map (kbd "<return>") mule-keymap)

;; Remember `er/expand-region' is bound to M-2!
(global-set-key (kbd "M-3") #'mc/mark-next-like-this)
(global-set-key (kbd "M-4") #'mc/mark-previous-like-this)

These three commands do most of the hard work. I'll never forget about them, so their keys don't need to be clever, they just need to be quick. Having mc/mark-next-like-this right next to er/expand-region is really the best place, although now-a-days I use mc/mark-all-dwim almost exclusively.

Something that took me a long time to figure out is that you can unmark stuff you just marked. Previously, whenever I marked-next-like-this once too many I’d just abort and start again. It makes sense to bind this to the same keys as above with the Shift modifier.

;; These vary between keyboards. They're supposed to be
;; Shifted versions of the two above.
(global-set-key (kbd "M-£") #'mc/unmark-next-like-this)
(global-set-key (kbd "M-$") #'mc/unmark-previous-like-this)

On the other hand, this package has a myriad of commands which are extremely useful on a less-than-daily basis, and this is where we invoke the power of intuitive key-maps.

(define-prefix-command 'endless/mc-map)
;; C-x m is usually `compose-mail'. Bind it to something
;; else if you use this command.
(define-key ctl-x-map "m" 'endless/mc-map)

;;; Really really nice!
(define-key endless/mc-map "i" #'mc/insert-numbers)
(define-key endless/mc-map "h" #'mc-hide-unmatched-lines-mode)
(define-key endless/mc-map "a" #'mc/mark-all-like-this)

;;; Occasionally useful
(define-key endless/mc-map "d"
  #'mc/mark-all-symbols-like-this-in-defun)
(define-key endless/mc-map "r" #'mc/reverse-regions)
(define-key endless/mc-map "s" #'mc/sort-regions)
(define-key endless/mc-map "l" #'mc/edit-lines)
(define-key endless/mc-map "\C-a"
  #'mc/edit-beginnings-of-lines)
(define-key endless/mc-map "\C-e"
  #'mc/edit-ends-of-lines)

Note how easy these keys are to remember. I use mc/insert-numbers barely once a week, but I never forget it's bound to C-x m i (and when I do use it, it’s a godsend). Other commands that I rarely use but save me a lot of trouble when I do are sort/reverse-regions and mc-hide-unmatched-lines-mode.

comments powered by Disqus