首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在emacs中快速打开文件?

如何在emacs中快速打开文件?
EN

Stack Overflow用户
提问于 2013-10-09 21:45:38
回答 4查看 3.9K关注 0票数 4

我有一个非常复杂和密集的文件夹和文件结构。我想打开一个文件,这是非常远离我的家很快在emacs。

C-x C-f不够快,因为我必须编写很长的路径(例如:/very/long/and/boring/path/which/make/me/use/tab/for/autocompletion/too/much/and/ruins/my/hands/with/repetitive/strain/injuries/my_file_finally.tex)

那么,是否有另一种方式,比C-x C-f更快地打开这样的文件?如果没有,是否有一种方法可以为最常用的文件创建快捷方式?

我不想移动文件(长路径对我来说意味着干净和有组织的计算机)。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-10-09 23:11:15

选项# 1 --每个分配的键有一个函数:

代码语言:javascript
运行
复制
(global-set-key (kbd "<f6>") (lambda () (interactive)
  (find-file "/Users/HOME/.0.data/todo.org")
  (message "Opened:  %s" (buffer-name))))

选项2 --带有选项的函数:

代码语言:javascript
运行
复制
(global-set-key (kbd "<f5>") 'lawlist-bookmark)

(defun lawlist-bookmark (choice)
  "Choices for directories and files."
  (interactive "c[D]ired | [v]ocab.org | [g]td.org | [d]iary.org | [n]otes.org")
    (cond
           ((eq choice ?D)
           (dired "/very/long/and/boring/path/which/make/me/use/tab/for/..."))
           ((eq choice ?v)
           (find-file "/Users/HOME/.0.data/vocab.org")
            (message "Opened:  %s" (buffer-name)))
          ((eq choice ?g)
           (find-file "/Users/HOME/.0.data/gtd.org")
            (message "Opened:  %s" (buffer-name)))
          ((eq choice ?d)
           (find-file "/Users/HOME/.0.data/diary.org")
            (message "Opened:  %s" (buffer-name)))
          ((eq choice ?n)
           (find-file "/Users/HOME/.0.data/notes.org")
            (message "Opened:  %s" (buffer-name)))
          (t (message "Quit"))))

选项# 3 --右击上下文菜单(弹出):

代码语言:javascript
运行
复制
(global-set-key [mouse-3] 'lawlist-popup-context-menu)

(defvar lawlist-context-menu-map
  (let ((map (make-sparse-keymap "Context Menu")))
    (define-key map [find-file-todo] (cons "ToDo" (lambda () (interactive)
      (find-file "/Users/HOME/.0.data/todo.org"))))
    (define-key map [find-file-gtd] (cons "GTD" (lambda () (interactive)
      (find-file "/Users/HOME/.0.data/gtd.org"))))
    (define-key map [find-file-notes] (cons "Notes" (lambda () (interactive)
      (find-file "/Users/HOME/.0.data/notes.org"))))
    (define-key map [find-file-vocab] (cons "Vocab" (lambda () (interactive)
      (find-file "/Users/HOME/.0.data/vocab.org"))))
    (define-key map [find-file-diary] (cons "Diary" (lambda () (interactive)
      (find-file "/Users/HOME/.0.data/diary.org"))))
    (define-key map [seperator-three] '(menu-item "--"))
    (define-key map [help-for-help] (cons "Help" 'help-for-help))
    (define-key map [seperator-two] '(menu-item "--"))
    (define-key map [my-menu] (cons "LAWLIST" (make-sparse-keymap "My Menu")))
    (define-key map [my-menu 01] (cons "Next Line" 'next-line))
    (define-key map [my-menu 02] (cons "Previous Line" 'previous-line))
    (define-key map [seperator-one] '(menu-item "--"))
  map) "Keymap for the LAWLIST context menu.")

(defun lawlist-popup-context-menu  (event &optional prefix)
  "Popup a context menu."
  (interactive "@e \nP")
    (define-key lawlist-context-menu-map [lawlist-major-mode-menu]
      `(menu-item ,(symbol-name major-mode)
        ,(mouse-menu-major-mode-map) :visible t))
    (popup-menu lawlist-context-menu-map event prefix))

选项# 4 --菜单下拉菜单:

代码语言:javascript
运行
复制
(require 'easymenu)

(easy-menu-define lawlist-menu global-map "My own menu"
  '("lawlist"
    ["Next Line" next-line t]
    ["Previous Line" previous-line t]
    ["Dired Mode" dired t]
    ["--" my-function t]
    ("Favorites"
    ["Diary" (find-file "/Users/HOME/.0.data/diary.org")]
    ["--" diary]
    ["GTD" (find-file "/Users/HOME/.0.data/gtd.org")]
    ["--" gtd]
    ["Vocab" (find-file "/Users/HOME/.0.data/vocab.org")]
    ["--" vocab]
    ["Notes" (find-file "/Users/HOME/.0.data/notes.org")]
    ["--" notes]
    ["ToDo" (find-file "/Users/HOME/.0.data/todo.org")]
    )  ))
票数 6
EN

Stack Overflow用户

发布于 2013-10-09 23:24:13

除了上述答案外,掌舵可能是解决问题的好方法。虽然在支持模式匹配和创建/管理/保存复杂文件集方面,它比冰柱做得更少,但helm做的既好又快(从击键角度和软件/延迟角度来看)。只要M-x package-install <ret> helm <ret>你就准备好了。

首先,helm可以跟踪recenf,这是您最近使用过的文件列表。只需M-x helm-recentf并键入一个字符串/regex,该字符串/regex将与您想要的文件名匹配,它将与您最近实时打开的文件名匹配。滚动并输入以选择其中一个。

但是等等!还有更多:(如果这听起来有点像广告),如果您在Linux上(我假设您在路径中使用斜杠,并且看起来是emacs+linux > mac+linux),您可以使用GNU,一种查找系统中任何位置的文件的工具,只要位置守护进程以前见过它,如果文件已经存在了很长时间(请参阅下面的文件)。几乎是实时的。定位过程快速,舵机实时传输定位结果。它的命令是M-x helm-locate

现在,如果您想在git/mercurial/bazaar/等项目中找到文件,怎么办?您可以使用弹丸,一个“项目交互库”。使用舵机-投射器(你需要抛射弹和舵射弹的安装方式,就像你自己一样)。它将将文件/目录名称和其他任何内容流到helm中。再一次。您只需要键入一个匹配任何位置的子字符串,helm就会为您找到它。M-x helm-projectile

他说,“舵手有很多能力”。有更多,更多的需要掌舵。尝试helm-moccur,helm-ack (外部包),helm-imenu,helm-浏览-代码,helm-tracker-搜索,helm-d缩写等等。

最后:为什么要选一个呢?helm真正做得很好的一件事就是创建您自己的命令。下面是一个搜索上述所有内容的函数。

代码语言:javascript
运行
复制
;; you'll need to require helm-config and helm-projectile somewhere above
(defun my-helm-omni (&rest arg) 
  ;; just in case someone decides to pass an argument, helm-omni won't fail.
  (interactive)
  (helm-other-buffer
    (append '(helm-c-source-buffers-list ;; list of all open buffers
               helm-c-source-recentf)    ;; all recent files

      ;; projectile errors out if you're not in a project 
      (if (projectile-project-p) ;; so look before you leap
        '(helm-source-projectile-files-list
           helm-source-projectile-recentf-list
           helm-source-projectile-buffers-list)
        '())

      '(
         helm-c-source-files-in-current-dir ;; files in current directory
         helm-c-source-locate               ;; file anywhere
         helm-c-source-bookmarks            ;; bookmarks too
         helm-c-source-buffer-not-found     ;; ask to create a buffer otherwise
         ))
    "*helm-omni*"))

我把这个绑在C-c o上了

票数 8
EN

Stack Overflow用户

发布于 2013-10-09 22:15:41

  1. 参见这个答案中的几乎相同的问题。特别是,考虑在Bookmark+中使用(a) Dired书签和其他与项目相关的书签,以及(b)在冰柱中使用与项目相关的辅助工具。
  2. 您可以为长目录名创建缩写--参见directory-abbrev-alist
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19283368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档