I'd like to add some extras that I sometimes use with multiple-cursors
.
I've found the following to be useful:
iy-go-to-char
Sometimes, you find yourself needing to move to the next #
or the next ,
or some specific delimiter. This is especially handy when you need to move across different numbers of words for each cursor.
It provides the functions iy-go-to-char
and iy-go-to-char-backward
, which you can bind and use nicely.
expand-region
This is really useful for similar reason to iy
- when you've got slightly different lines under each cursor, and you maybe want to select the quoted string under the point or similar.
custom stuff
These are things that are just elisp in my config, but I find useful. Probably not written by me.
Prompt for input
I got this one from Magnars in response to a request for putting a different string under each cursor. The code is simple, but binding it to something and using for multiple cursors is a handy way to insert slightly different stuff in each place.
(defun prompt-for-insert ()
(interactive)
(insert (read-string "Insert: ")))
Increment number at point
This either came from SO, or emacswiki. Not sure which.
It's pretty useful on its own, and also when used in combination with mc/insert-numbers
(defun my-increment-number-decimal (&optional arg)
"Increment the number forward from point by 'arg'."
(interactive "p*")
(save-excursion
(save-match-data
(let (inc-by field-width answer)
(setq inc-by (if arg arg 1))
(skip-chars-backward "0123456789")
(when (re-search-forward "[0-9]+" nil t)
(setq field-width (- (match-end 0) (match-beginning 0)))
(setq answer (+ (string-to-number (match-string 0) 10) inc-by))
(when (< answer 0)
(setq answer (+ (expt 10 field-width) answer)))
(replace-match (format (concat "%0" (int-to-string field-width) "d")
answer)))))))
eval-and-replace
This is super powerful, and enables some really useful bits 'n' pieces. It evaluates whatever sexp is behind the cursor, and inserts the result of evaluating it.
Combine it with multiple-cursors-numbers
to e.g. insert powers of two, or a series that goes 0 1 2 0 1 2 0 1 2
across each line or something. (I've done both of these usefully)
Or use it with s.el
to write out those tedious java config with default things. You write out each CONFIG_VALUE_VARIABLE
, then transform that into CONFIG_VALUE_VARIABLE = "configValueVariable"
by running s-camel-case
.
(defun eval-and-replace ()
(interactive)
(let ((value (eval (preceding-sexp))))
(kill-sexp -1)
(insert (format "%s" value))))
multiple-cursors
very quickly just by reading the README file. My recommendation is to just learnmc/mark-next-like-this
. Try it out, and get familiar with what it does. From there refer back to the README whenever you have a question that starts with "I wonder if multiple-cursors can do ..." All of that said, I do think that a more basic introduction than the Emacs Rocks! video would be helpful for beginners.