我有一个按顺序排列的单词和短语缓冲区,我希望这些行按随机顺序排序.如何使用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
与Sean的解决方案类似,选择区域,然后:
C-u M-| shuf
Run Code Online (Sandbox Code Playgroud)
M- | 将所选区域的内容传递给bash命令shuf.shuf洗牌线.前缀Cu取shuf的输出并用它来覆盖所选区域.
或者,这里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)))))))
Run Code Online (Sandbox Code Playgroud)
对于原始:
M-x find-function
RET sort-lines
RET