Emacs builtin mode 功能介绍

Changelog

  • 2020-05-26 添加diredispell模式的介绍

dired

dired是一个用于directory浏览的mode,功能非常丰富。因此这里介绍的东西肯定不 能完全覆盖,会慢慢完善之。

在 dired 中用外部程序打开对应文件

dired-mode-map中,也是可以执行shell命令的。与之相关的命令有

  • dired-do-shell-command, 默认绑定在!
  • dired-smart-shell-command,默认绑定在M-!
  • async-shell-command,默认绑定在M-&

其中,通过配置dired-guess-shell-alist-user可以令dired-do-shell-command有一个 比较好的默认命令。例如,我这是样配置的:

(setq dired-guess-shell-alist-user `((,(rx "."
                                           (or
                                            ;; Videos
                                            "mp4" "avi" "mkv" "flv" "ogv" "mov"
                                            ;; Music
                                            "wav" "mp3" "flac"
                                            ;; Images
                                            "jpg" "jpeg" "png" "gif" "xpm" "svg" "bmp"
                                            ;; Docs
                                            "pdf" "md" "djvu" "ps" "eps")
                                           string-end)
                                      ,(cond ((eq system-type 'gnu/linux) "xdg-open")
                                             ((eq system-type 'darwin) "open")
                                             ((eq system-type 'windows-nt) "start")
                                             (t "")))))

这里考虑了多个平台下的差异。如linux平台下会使用xdg-open来打开对应的这些文件 (通过mimeinfo来配置,见~/.config/mimeapps.list)。但是它有一个缺点,会阻塞当 前的Emacs进程,所以仅适用于临时查看的需求。

dired-smart-shell-commanddired-do-shell-command类似,也会阻塞当前Emacs进 程。

async-shell-command则不会阻塞当前Emacs,唯一的缺点可能是会多弹出个buffer吧。 如果对async-shell-command的结果不是很感兴趣,可能通过shackle等类似的工具把忽 略对应的buffer

如果使用的是Emacs 28的话,并且已经设置了

(setq browse-url-handlers '(("\\`file:" . browse-url-default-browser)))

可以直接在dired里按W (browse-url-of-dired-file), 这会直接用外部程序打开。 当然,它不会阻塞Emacs

隐藏、显示 以.开头的文件

dired显示文件时使用的ls命令参数是由dired-listing-switches来控制的,它的默 认值是-al。如果不想要显示以.开头的文件,那么通过C-u s (sdired-sort-toggle-or-edit)来重新设置dired-listing-switches

如果只是想简单地隐藏当前目录下以.开头的文件,那么可以通过将满足^\\\.正则的行 删除就行(真实文件并没有删除,只是删除它的显示)。注意到dired-do-print命令基本 不怎么使用,于是可以利用advice来覆盖它,实现我们自己的dotfiles-toggle

;; 修改自 https://www.emacswiki.org/emacs/DiredOmitMode
(define-advice dired-do-print (:override (&optional _))
    "Show/hide dotfiles."
    (interactive)
    (if (or (not (boundp 'dired-dotfiles-show-p)) dired-dotfiles-show-p)
        (progn
          (setq-local dired-dotfiles-show-p nil)
          (dired-mark-files-regexp "^\\.")
          (dired-do-kill-lines))
      (revert-buffer)
      (setq-local dired-dotfiles-show-p t)))

这样只要按一下P就可以达到隐藏、显示的切换了。

如果不想自己写elisp,这里也有一个现成的包 https://github.com/mattiasb/dired-hide-dotfiles

ispell

ispell全称是interactive spell检查器,它支持ispell, aspellhunspell, 以下以hunspell为例。

;; 这里使用的是 en_US 字典,需要使用包管理安装对应的字典,类似的名字可能 hunspell-en_US
(setq ispell-dictionary "en_US"
      ispell-program-name "hunspell"
      ispell-personal-dictionary (expand-file-name "hunspell_dict.txt" user-emacs-directory))

这样就可以通过调用ispell-word来看一个单词是否正确了。如果是evil用户,这个函 数已经被绑定至z=上了。 \w/

5 个赞