我在 elisp 中编写了一些脚本,它合并了 ls -l 和 du (显示真实文件夹大小而不是 ls 中写入的内容)。我把它命名为lsd。这是屏幕截图:
现在我将列出实现。我不是一个好的编码员,所以我会很感激任何有关错误和可以做得更好的事情的信息。
lsd.el
#!/usr/bin/emacs --script
(progn
(setq argz command-line-args-left)
(setq folder "./")
(while argz
;; (message (car argz))
(if (/= ?- (aref (car argz) 0))
(setq folder (car argz)))
(setq argz (cdr argz)))
(if (/= ?/ (aref folder (1- (length folder)))) (setq folder (concat folder "/")))
(switch-to-buffer " *lsd*")
(erase-buffer)
(shell-command (concat "ls -l -h --color=always " " " (apply 'concat (mapcar '(lambda(arg) (concat arg " ")) command-line-args-left))) (current-buffer))
(switch-to-buffer " *du*")
(erase-buffer)
(shell-command (concat "du -h -d 1 " folder) (current-buffer))
(goto-char 1)
(while (search-forward "Permission denied" (point-max) t nil)
(goto-char (point-at-bol))
(let ((beg (point)))
(forward-line)
(delete-region beg (point)))) ; Remove all permission denied lines, thus show only permitted size.
(goto-char 1)
(while (and (search-forward folder (point-max) t nil) (/= (point-max) (1+ (point-at-eol)))) ; we do not need last line(the folder itself), so this line is something complex.
(setq DIR (buffer-substring (point) (point-at-eol)))
(goto-char (point-at-bol))
(setq SIZE (buffer-substring (point) (1- (search-forward " " (point-at-eol) nil nil))))
(goto-char (point-at-eol))
(switch-to-buffer " *lsd*")
(goto-char 1)
(if (search-forward DIR (point-max) t nil)
(progn
(goto-char (point-at-bol))
(search-forward-regexp "[0-9]+" (point-at-eol) nil nil)
(search-forward-regexp " *[0-9]+[^ \n]*[ \n]*" (point-at-eol) nil nil)
;; If ls have options, that makes some numbers before size column - we are doomed. (-s, for example)
(setq SIZE (concat SIZE " "))
(while (< (length SIZE) (length (match-string 0))) (setq SIZE (concat " " SIZE)))
(replace-match SIZE)))
(switch-to-buffer " *du*"))
(switch-to-buffer " *lsd*")
(message "%s" (buffer-substring (point-min) (point-max)))
(defun error(&rest args) args)
(defun message(&rest args) args)) ; Do not show any messages.
LSD (我制作这个脚本来启动emacs,除了脚本之外不加载任何东西。如果可以更容易地完成,请指出这一点)
#/bin/bash
emacs -Q --script /usr/local/bin/lsd.el $@
问题来了:如何在 dired 中使用这个 lsd?
我可以更改 dired 中的某些内容以使用 lsd 而不是 ls 吗?
我可以重命名oldls中的ls,并制作一些ls bash脚本,如果没有--lsd标志,则将所有参数传递给ls,如果--lsd在这里,则将所有参数传递给lsd?
这是个好主意吗?
最佳答案
在 Emacs24 中还有 `insert-directory-program'设置 ls 可执行文件。放置
(setq 插入目录程序“/usr/local/bin/lsd”)
(相应地调整路径)在 .emacs 或 init.el 中,并且 dired
会采用您的 lsd
脚本。
关于linux - 将 "ls"从 emacs dired 更改为其他应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6524521/