Emacs的dired模式作为文件管理器
Emacs dired-mode as a file manager

原始链接: https://lynn.sh/guix-emacs-file-manager.html

这份指南介绍了如何配置 Guix Home,使其默认使用 `xdg-mime` 在 Emacs 的 Dired 模式下打开目录。不同于声明式系统,Guix Home 允许进行可记录和可复现的配置。 关键在于 `home-xdg-mime-applications-service-type`,它创建 `.desktop` 文件并将其与 MIME 类型关联。示例代码片段定义了一个名为“Emacs Dired 作为文件管理器”的桌面条目,该条目执行 `emacsclient -c -a emacs %u`(在新窗口中打开 Emacs Dired)。然后,此条目将链接到 `inode/directory` MIME 类型。 要应用更改,只需运行 `guix home reconfigure` 即可。这种方法可以扩展到处理其他 MIME 类型,例如图像和文本文件,如果您使用单独的文件管理器,这将特别有用。使用 Emacs 可以避免需要更多 MIME 类型。 指南还提到了对 Guix 用户有用的资源,并推荐查看 `abcdw` 的 `rde` 框架。

This Hacker News thread discusses using Emacs' dired-mode as a file manager. A key benefit highlighted is the ability to perform bulk operations on files, like copying, moving, or deleting, using concise keystrokes and scripting. Users appreciate that dired's functionality is scriptable and recordable, allowing for automation of repetitive tasks. One commenter notes that while it might seem against the Unix philosophy of small, focused tools, Emacs' modularity allows for using only the needed functionalities. Others point out Emacs' roots in Lisp machines, contrasting its approach to Unix principles. Some users prefer using the shell for file management, while others value dired for its seamless integration within the Emacs environment, reducing mental context switching and enabling tasks like renaming files while editing them. Ultimately, dired is praised for its programmability and consistency across different systems.

原文

I'm sure some of you reading this are rolling your eyes or reciting the well-known "Emacs is a great OS, but …" I use it a lot, though. I already use dired-mode to manage my writing and programming. It's something that is powerful, and comfortable to use. If you are following along and use something else, it is also quite easy to adjust my code here as well.

Ok, with that out of the way let's look at what we actually want to do: xdg-mime needs to connect "Opening a directory" with "opening emacs in dired-mode." First, we need the MIME type for directories. A quick search shows that it is called inode/directory.

I have no idea why it is called inode/directory, and I couldn't find an explanation. I've learned not to ask questions about these names.

Opening emacs in dired-mode is the easy part: if you pass emacs a directory instead of a file, it automatically opens in the appropriate mode. Great! Now we need to tie these two concepts together.

If we were using a non-declarative system, such as Debian, I would suggest doing something like creating a .desktop file, running xdg-mime default emacs-dired.desktop inode/directory and that would be it. You could theoretically do this on Guix as well. There is a big downside with this strategy, though: the changes you make are not documented anywhere on your system. If you wanted to duplicate your setup on a new laptop, for example, you would have to either note down this change you made or run into it again, hopefully remember your solution, and then reproduce it. Maybe this works for others, but I know myself. I will not remember, I will run into it again, and then I will get frustrated again. That's why we have guix-home configuration.

home-xdg-mime-applications-service-type is a very helpful service in the guix-home configuration tool. It allows you to create xdg-desktop-entry for each .desktop file you would normally need to create, link those to the MIME type of your choice, and that's it. It is also all written down explicitly, so there is no confusion on what steps were taken. Let's look at the most basic example to solve our issue:

(home-environment
 (services
  (list
   (service home-xdg-mime-applications-service-type
            (home-xdg-mime-applications-configuration
             (default '((inode/directory . emacs-dired.desktop)))
             (desktop-entries
              (list (xdg-desktop-entry
                     (file "emacs-dired")
                     (name "Emacs Dired as a file manager")
                     (type 'application)
                     (config
                      '((exec . "emacsclient -c -a emacs %u")
                        (mimetype . "inode/directory")))))))))))

If this is nonsense to you, I apologize. I'm definitely not the person who should be teaching you lisp. You should definitely learn it, though! The configuration language for Guix is called Guile Scheme, read up on it.

I use emacsclient here because I have emacs always running as a daemon. -c tells it to create a new frame, and -a emacs tells it to fall-back to a regular emacs session if there is some reason my daemon has stopped running. And that's it! It works perfectly after a quick guix home reconfigure. To expand, just add a new xdg-desktop-entry that runs whatever script you wish, and then tie it to a MIME type in the default list.

I considered adding things for image types, text types, etc. This would be useful to do if you used a regular file manager. With emacs though, I am already in the program I need for these types. Opening them from dired-mode already puts them into emacs, so I decided against the necessity for this. If you are using something other than emacs for this setup, I recommend adding the following MIME types:

Mime Type Application Type
image/jpeg image viewer
image/png image viewer
image/svg image viewer
image/webp image viewer
text/plain text editor
text/html text editor
text/x-makefile text editor
application/pdf pdf viewer
x-scheme-handler/http browser
x-scheme-handler/https browser

There are more, but if you are using a minimal system and want full control this will give you a good starting point. Thanks for reading!

If you are learning Guix and need help figuring something out, I highly recommend checking out rde by abcdw. It is a framework for Guix to help create reproducable systems, and also a wealth of knowledge when it comes to configuring.

联系我们 contact @ memedata.com