打开文件加强版

发布: 2022-07-09   更新: 2022-07-09   标签: file

在 Emacs 中, find-file(C-x C-f) 是用来打开文件的最基本方式,但有时当前 buffer 内有需要打开文件的信息,比如有如下文本:

/tmp/test.log

这时可以用 find-file-at-point 直接打开该文件。此外在编辑 elisp 配置时,有时候需要打开一个变量所代表的文件,我之前一直采用在 minibuffer 中执行命令的方式来打开,比如:

1
(find-file custom-file)

有些低效,能不能把 find-file-at-point 增强下,支持这种变量形式呢?当然是可以的:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
(defun my/find-file-at-point ()
  "Enhanced version of `find-file-at-point'.
First attempt to open file specified by `symbol-at-point', and fallback to normal one."
  (interactive)
  (condition-case nil
      (thread-last (thing-at-point 'symbol t)
                   (intern)
                   (symbol-value)
                   (find-file-noselect)
                   (switch-to-buffer))
    (t (call-interactively 'find-file-at-point))))

最后,把上面这个命令绑定到常用的快捷键,比如 C-x C-f ,这样以后就可以方便的使用了。



收听方式

反馈