【问题标题】:How to abbreviate path in Emacs?如何在 Emacs 中缩写路径?
【发布时间】:2013-10-02 15:40:54
【问题描述】:

我想在 emacs 中缩写路径。

emacs 的标准行为是: C-x C-f 然后排版文件的完整名称。例如

C-x C-f ~/latex/project/style/test.sty

可以用来快速完成路径。

由于我经常使用三个目录中的两个,我想知道是否可以提供一些“缩写路径”。例如

C-x C-f lstyle/test.sty

在 « ~/latex/project/style/test.sty » 中自动扩展

感谢您的帮助!

【问题讨论】:

  • 对了,描述key的正确方式是C-x C-f
  • 我自己回答。我可以“dired”一个目录,然后将该目录保存在书签中。下次,我直接使用书签进入目录。还是谢谢!

标签: emacs path


【解决方案1】:

尚未提及的事情,但只是另一个机会(在普通 Emacs 中,无需使用 Helm 或 IDO)。你可以使用书签。特别是对于打开文件,您将 Cx rm - 创建一个书签(系统会提示您为书签指定一个名称),然后 Cx rb 稍后会调用该书签(将提示您输入之前为书签指定的名称)。

如果您使用路径的主要用途是打开文件,这将为您节省一些输入,但如果您需要将其作为文本插入,那么它不会替换缩写。

如果你不知道的话,也许还有一件事。在 C-x C-f 之后输入路径后,您可以使用:

  1. Cx CfMpMn 浏览您打开和向下打开的文件的历史记录。

  2. Cx CfMr 通过为其提供正则表达式来搜索历史记录(虽然有一个缺点,您不会看到它将进行的匹配在您输入正则表达式时)。

【讨论】:

    【解决方案2】:

    实现这一目标的最佳方法是使用缩写。它们是简短的词 键入时扩展为任何内容(因此扩展将发生在 只要你点击“/”或空格或一些标点符号)。

    将以下内容添加到 你的~/.emacs 文件或~/.emacs.d/init.el 文件(我更喜欢第二个):

    (setq-default abbrev-mode t)
    (define-abbrev global-abbrev-table "lstyle" "~/latex/project/style")
    

    然后试一试,在任何地方输入“lstyle/”,它就会展开 到“~/latex/project/style”。

    【讨论】:

    • Abbrev-mode 在 minibuffer 中不起作用,所以对我的需要没用。
    • @PaulPichaureau 你确定吗?它对我有用(即使我运行emacs -Q 以忽略我的所有配置)。你记得最后打/吗?
    【解决方案3】:

    使用标准选项directory-abbrev-alist。来自文档字符串:

    Alist of abbreviations for file directories.
    A list of elements of the form (FROM . TO), each meaning to replace
    FROM with TO when it appears in a directory name.  This replacement is
    done when setting up the default directory of a newly visited file.
    
    FROM is matched against directory names anchored at the first
    character, so it should start with a "\\`", or, if directory
    names cannot have embedded newlines, with a "^".
    
    FROM and TO should be equivalent names, which refer to the
    same directory.  Do not use `~' in the TO strings;
    they should be ordinary absolute directory names.
    
    Use this feature when you have directories which you normally refer to
    via absolute symbolic links.  Make TO the name of the link, and FROM
    the name it is linked to.
    

    【讨论】:

    • 是的,它对我有用。但是我应该怎么做才能使它与 ido 一起使用呢?有什么想法吗?
    • @AndreyTykhonov:不知道;我不使用 Ido。也许其他人将能够提供帮助。如果没有,请尝试邮件列表help-gnu-emacs@gnu.org 来解决问题,或尝试M-x report-emacs-bug 记录增强请求。
    【解决方案4】:

    不完全符合您的要求,但您可以使用recentf 模式和ido 模式快速访问您最近打开的文件。将此代码放入您的初始化文件中:

    ;; ido-mode
    (ido-mode t)
    
    ;; recent mode
    (recentf-mode 1)
    
    ;; define a function to use recentf from ido
    (defun recentf-ido-find-file ()
      "Open a recent file using IDO mode"
      (interactive)
      (let ((file (ido-completing-read "Recent file: " recentf-list nil t)))
        (when file
          (find-file file))))
    
    (global-set-key (kbd "C-c f") 'recentf-ido-find-file)
    

    然后您可以按C-c f 快​​速打开您最近的文件。

    【讨论】:

      【解决方案5】:

      这样的事情有几种解决方案。很难提前说你会更喜欢哪一个。

      一种流行的方法是使用 ido(见 herehere),其他的是 helm。

      【讨论】:

        【解决方案6】:

        在寻找同一问题的答案时,我发现了following example

        (定义缩写表'我的缩写表 '(("xy" "/path/to/the/directory"))) (添加钩子 'minibuffer-setup-hook (拉姆达() (缩写模式 1) (setq local-abbrev-table my-abbrev-table))) (defadvice minibuffer-complete (在 my-minibuffer-complete 激活之前) (扩展缩写)) ;;如果您使用部分完成模式 (defadvice PC-do-completion (在 my-PC-do-completion 激活之前) (扩展缩写))

        【讨论】:

          【解决方案7】:

          使用this 答案作为参考:

          (global-set-key (kbd "<f5>") 'lawlist-bookmark)
          
          (defun lawlist-bookmark (choice)
            "Choices for directories and files."
            (interactive "c[d]ropbox | [b]oxsync")
              (cond
               ((eq choice ?d)
                (interactive)
                (counsel-find-file "C:/Users/acer/Dropbox/"))
               ((eq choice ?b)
                (counsel-find-file "C:/Users/acer/Box Sync/"))
               (t (message "Quit"))))
          

          这里使用counsel-find-file 代替find-file 以允许在迷你缓冲区中进行交互式浏览。

          【讨论】:

            猜你喜欢
            • 2010-12-18
            • 2016-01-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-28
            • 1970-01-01
            • 2021-09-02
            • 1970-01-01
            相关资源
            最近更新 更多