multiple-cursors做代码对齐

multiple-cursors做代码对齐

multiple-cursors做代码对齐

假设我们有这样一个文件:

::msgcat::mcset cn "steel" "钢"
::msgcat::mcset cn "al" "铝"
::msgcat::mcset cn "general" "焊缝参数"
::msgcat::mcset cn "FAT" "疲劳等级 *"
::msgcat::mcset cn "Weld type" "焊缝类型"
::msgcat::mcset cn "Weld width" "焊缝宽度"

我们想要让它对齐成如下:

::msgcat::mcset cn "steel"      "钢"
::msgcat::mcset cn "al"         "铝"
::msgcat::mcset cn "general"    "焊缝参数"
::msgcat::mcset cn "FAT"        "疲劳等级 *"
::msgcat::mcset cn "Weld type"  "焊缝类型"
::msgcat::mcset cn "Weld width" "焊缝宽度"

当然,我们可以使用 align-regexp ,然后,使用正则表达式进行对齐,但是,那个还是比较麻烦的。

其实,multiple-cursors中有一个命令 mc/vertical-align-with-space ,可以用来进行对齐操作。

这样,我们只需要先创建一个multiple-cursors:

http://images2015.cnblogs.com/blog/717724/201702/717724-20170209231242072-1074762385.jpg

然后,跳到最后面:

http://images2015.cnblogs.com/blog/717724/201702/717724-20170209231242744-1818541276.jpg

再用 sp-backward-sexp ,跳到要对齐的位置:

http://images2015.cnblogs.com/blog/717724/201702/717724-20170209231243416-344474362.jpg

再执行 mc/vertical-align-with-space ,就可以对齐了:

http://images2015.cnblogs.com/blog/717724/201702/717724-20170209231244088-1988850424.jpg

这样,基本上可以应对很多比较麻烦的对齐。当然,简单的,用空格或者是其他一个符合对齐的工作,可以使用另外一个函数来完成,更加方便:

(defun my-align-regexp-repeated (start stop regexp)
  "Like align-regexp, but repeated for multiple columns. See
http://www.emacswiki.org/emacs/AlignCommands"
  (interactive "r\nsAlign regexp: ")
  (let ((regexp (if (string= regexp "") " " regexp))
        (spacing 1)
        (old-buffer-size (buffer-size)))
    ;; If our align regexp is just spaces, then we don't need any
    ;; extra spacing.
    (when (string-match regexp " ")
      (setq spacing 0))
    (align-regexp start stop
                  ;; add space at beginning of regexp
                  (concat "\\([[:space:]]*\\)" regexp)
                  1 spacing t)
    ;; modify stop because align-regexp will add/remove characters
    (align-regexp start (+ stop (- (buffer-size) old-buffer-size))
                  ;; add space at end of regexp
                  (concat regexp "\\([[:space:]]*\\)")
                  1 spacing t)
    (indent-region start (+ stop (- (buffer-size) old-buffer-size)))))

Date: 2017-02-09 10:19

Created: 2017-02-09 周四 23:12

Emacs 26.0.50.4 (Org mode 9.0.4)

Validate

posted @ 2017-02-09 23:13  yangwen0228  阅读(753)  评论(0编辑  收藏  举报