Multiple-cursors 真的好爽!

重新设定了一下热键,变为SPC d开头,感觉好爽:

    (defunxxx-multiple-cursors ()
      (use-package multiple-cursors
        :init
        (progn
          (spacemacs/set-leader-keys "dl" 'mc/edit-lines)
          (spacemacs/set-leader-keys "db" 'mc/edit-beginnings-of-lines)
          (spacemacs/set-leader-keys "de" 'mc/edit-ends-of-lines)

      (spacemacs/set-leader-keys "da" 'mc/mark-all-like-this)
      (spacemacs/set-leader-keys "dA" 'mc/mark-all-dwim)

      (spacemacs/set-leader-keys "dj" 'mc/mark-next-like-this)
      (spacemacs/set-leader-keys "dJ" 'mc/unmark-next-like-this)
      (spacemacs/set-leader-keys "dk" 'mc/mark-previous-like-this)
      (spacemacs/set-leader-keys "dK" 'mc/unmark-previous-like-this)

      (spacemacs/set-leader-keys "di" 'mc/insert-numbers)
      (spacemacs/set-leader-keys "dh" 'mc-hide-unmatched-lines-mode)
      (spacemacs/set-leader-keys "dd" 'mc/mark-all-symbols-like-this-in-defun)
      (spacemacs/set-leader-keys "dr" 'mc/reverse-regions)
      (spacemacs/set-leader-keys "ds" 'mc/sort-regions)

      (global-unset-key (kbd "M-<down-mouse-1>"))
      (global-set-key (kbd "M-<mouse-1>") 'mc/add-cursor-on-click))
    :config
    (setq mc/cmds-to-run-for-all
          '(
            electric-newline-and-maybe-indent
            hungry-delete-backward
            spacemacs/backward-kill-word-or-region
            spacemacs/smart-move-beginning-of-line
            lispy-move-beginning-of-line
            lispy-move-end-of-line
            evil-exit-visual-state
            evil-backward-char
            evil-delete-char
            evil-escape-emacs-state
            evil-escape-insert-state
            evil-exit-emacs-state
            evil-previous-visual-line
            evil-next-visual-line
            evil-forward-char
            evil-insert
            evil-next-line
            evil-normal-state
            evil-previous-line
            evil-append
            evil-append-line
            forward-sentence
            kill-sentence
            org-self-insert-command
            sp-backward-delete-char
            sp-delete-char
            sp-remove-active-pair-overlay))))
7 个赞
(defun multiple-cursors/init-multiple-cursors ()
  (use-package multiple-cursors
    :config
    ;; multiple-cursors
    (global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
    (global-set-key (kbd "C->") 'mc/mark-next-like-this)
    (global-set-key (kbd "C-+") 'mc/mark-next-like-this)
    (global-set-key (kbd "C-c C-<") 'mc/mark-all-like-this)
    ;; From active region to multiple cursors:
    (global-set-key (kbd "C-c c r") 'set-rectangular-region-anchor)
    (global-set-key (kbd "C-c c c") 'mc/edit-lines)
    (global-set-key (kbd "C-c c e") 'mc/edit-ends-of-lines)
    (global-set-key (kbd "C-c c a") 'mc/edit-beginnings-of-lines)
    ))
1 个赞
    (define-key evil-visual-state-map (kbd "mn") 'mc/mark-next-like-this)
    (define-key evil-visual-state-map (kbd "ma") 'mc/mark-all-like-this)

(defun liu233w/init-ace-mc ()
  (use-package ace-mc
    :defer t
    :init
    (define-key evil-visual-state-map (kbd "mm") 'ace-mc-add-multiple-cursors)
    (define-key evil-visual-state-map (kbd "ms") 'ace-mc-add-single-cursor)))


我是在evil的visual state才生效的快捷键,反正一般是在标记之后才会用这个 :grinning:

还有ace-mc,可以很方便的选择要产生光标的位置

1 个赞

一般用于什么场景下?

Emacs Rocks! Episode 08: mark-multiple

这里有介绍,虽然不是mc的,但是功能差不多。

2 个赞

Multiple-cursors 在标记了多个光标之后,没有没办法同时选中多个光标的内容(例如yw),并yank到kill ring当中。

multiple cursors和spacemacs配合的不大好,在issue里面有讨论的。

Multiple Cursors Integration Discussion

最近改用evil-mc了。spacemacs自带了这个,只要打开就可以用,但是对于某些操作(比如hungry-delete之类的)没法在多光标下执行,需要设置变量evil-mc-custom-known-commands。这是我的配置,额外增加了一个micro-state,这样在make光标的时候就不用一直按着ctrl了。只不过ace-mc没法用在evil-mc上,有些不爽。

(defun liu233w/post-init-evil-mc ()
  (add-hook 'prog-mode-hook 'evil-mc-mode)
  (add-hook 'text-mode-hook 'evil-mc-mode)

  ;; 设置在evil-mc之下可以执行的命令,主要是删除操作
  (setq evil-mc-custom-known-commands
        '((paredit-backward-delete . ((:default . evil-mc-execute-default-call-with-count)))
         (hungry-delete-backward . ((:default . evil-mc-execute-default-call-with-count)))
         (org-delete-backward-char . ((:default . evil-mc-execute-default-call-with-count)))
         ))

  (spacemacs|define-micro-state liu233w/evil-mc-add-cursor
    :doc
    "`n' make-and-go-to-next-match `t' skip-and-go-to-next-match"
    :use-minibuffer t
    :on-enter (evil-mc-make-and-goto-next-match)
    :bindings
    ("n" evil-mc-make-and-goto-next-match)
    ("t" evil-mc-skip-and-goto-next-match))

  (define-key evil-visual-state-map (kbd "mn")
    'spacemacs/liu233w/evil-mc-add-cursor-micro-state)
  (define-key evil-visual-state-map (kbd "ma")
    'evil-mc-make-all-cursors)
  )

不错,抄你的配置试试。

多光标复制粘贴没问题(是我理解错了么?)

你用的是multi cursor,还是evil-mc

multiple-cursors + 原生 emacs

原生的emacs就不用说了,我是说Spacemacs+multiple-cursors不行。

我认为并没有什么差别,我同样是 evil-mode,进入到多光标模式下,就不要再拘泥 vi 的操作方式,切换到类似 hybrid-mode 方式,问题就解决了

上面elisp暂时看不大懂,不过貌似没人提到这个?: GitHub - hlissner/evil-multiedit: Multiple cursors for evil-mode, based on iedit 在spacemacs自带的iedit基础上改的,支持sublime经典的渐进式选择的方式,我昨晚看day3的时候找到的,还没试不过从文档来看非常棒。

请问要如何启用和使用evil-mc? 虽然能在Spacemacs,但是在M-x中没有相关的选项。因为不知道使用时是怎样操作,所以也无法确认是不是已经开启了。

看看你想用的evil-mc-match之类的函数有没有加载咯。或者(featurep 'evil-mc)(evil-mc或者evil-mc-mode,看作者心情了)

hungry-delete 是做什么用的?

同样使用 evil-mc-mode 如果同时使用的 evil-collections 的话,按键不起作用了,有人遇到过吗?

xx4 困扰已久=- =,

(use-package multiple-cursors
  :defer t
  :ensure t
  :config
  ;; 有些命令会询问是否应用到所有光标,然后该命令的 是/否 会被缓存到这个文件中
  (setq mc/list-file (expand-file-name "multiple-cursors-list-file" wk-cfg-dir))
  )

(define-key global-map (kbd "C-S-c C-S-c") 'mc/edit-lines)
(define-key global-map (kbd "C->") 'mc/mark-next-like-this)
(define-key global-map (kbd "C-<") 'mc/mark-previous-like-this)
(define-key global-map (kbd "C-c C-<") 'mc/mark-all-like-this)
(global-unset-key (kbd "M-<down-mouse-1>"))
(define-key global-map (kbd "M-<mouse-1>") 'mc/add-cursor-on-click)
(define-key mc/keymap (kbd "<return>") nil)