3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

multiple-cursorの連番挿入機能の拡張

Last updated at Posted at 2013-06-30

multiple-cursor.el(ver1.2.1)で連番を挿入できる関数mc/insert-numbersは、連番の開始番号しか選択できません。
cua-modeのように問い合わせで増加数や0埋めを指定したかったので走り書きしました。

設定をコピペして反映させ、multiple-cursor使用時に
M-x my/mc/insert-numbersを呼び出せば以下のプロンプトが表示されます。

  • Start from (default 0): 開始番号
  • Increment by (default 1): 増加数
  • Padding (default %01d): 0埋め、空白埋めなどのフォーマット(%03d, %5d)

.emacs.d/init.el
    ;; insert specific serial number
    (defvar my/mc/insert-numbers-hist nil)
    (defvar my/mc/insert-numbers-inc 1)
    (defvar my/mc/insert-numbers-pad "%01d")

    (defun my/mc/insert-numbers (start inc pad)
      "Insert increasing numbers for each cursor specifically."
      (interactive
       (list (read-number "Start from: " 0)
             (read-number "Increment by: " 1)
             (read-string "Padding (%01d): " nil my/mc/insert-numbers-hist "%01d")))
      (setq mc--insert-numbers-number start)
      (setq my/mc/insert-numbers-inc inc)
      (setq my/mc/insert-numbers-pad pad)
      (mc/for-each-cursor-ordered
       (mc/execute-command-for-fake-cursor
        'my/mc--insert-number-and-increase
        cursor)))
    
    (defun my/mc--insert-number-and-increase ()
      (interactive)
      (insert (format my/mc/insert-numbers-pad mc--insert-numbers-number))
      (setq mc--insert-numbers-number (+ mc--insert-numbers-number my/mc/insert-numbers-inc)))
3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?