如何在缓冲区中随机排序?

时间:2011-05-30 03:31:34

标签: emacs elisp

我有一个按顺序排列的单词和短语的缓冲区,我希望这些行以随机顺序排序。如何使用emacs内置函数或使用elisp?

例如,给定

bar
elisp
emacs
foo
hello world
the quick brown fox

我想要一些完全随机的结果,如:

foo
the quick brown fox
hello world
elisp
emacs
bar

或......

hello world
elisp
bar
the quick brown fox
foo
emacs

4 个答案:

答案 0 :(得分:9)

在GNU / Linux上使用Bash:

与Sean的解决方案类似,选择区域,然后:

C-u M-| shuf

说明:

M- |将所选区域的内容传递给bash命令shuf。 shuf洗牌线。前缀C-u获取shuf的输出并使用它来覆盖所选区域。

答案 1 :(得分:7)

或者,这里sort-lines符合此要求。

我删除了reverse参数(这里显然不相关),只是提供了一个'比较'函数,将随机结果返回给sort-subr

(defun my-random-sort-lines (beg end)
  "Sort lines in region randomly."
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (let ;; To make `end-of-line' and etc. to ignore fields.
          ((inhibit-field-text-motion t))
        (sort-subr nil 'forward-line 'end-of-line nil nil
                   (lambda (s1 s2) (eq (random 2) 0)))))))

原件:
M-x find-function RET sort-lines RET

答案 2 :(得分:6)

randomize-region.el似乎做你想做的事。

答案 3 :(得分:4)

如果您不介意炮轰Perl,可以选择要随机化的区域,然后键入 Cu M- | perl -MList::Util=shuffle -e 'print shuffle <STDIN>'

我相信很多其他流行的编程语言都提供类似的功能。