Search code examples
fileemacsorganization

How can I open quickly a file in emacs?


I have a very complex and dense structure of folders and files. I would like to open a file which is very far away from my home quickly in emacs.

C-x C-f is not quick enough because I have to write the path which could be very long (example : /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)

So, is it another way, quicker, (than C-x C-f) to open such file and, if not, is there a way to create shortcuts for the most used files ?

P-S : I don't want to move files (long paths means to me clean and organized computer).


Solution

  • OPTION # 1 -- one function per assigned key:

    (global-set-key (kbd "<f6>") (lambda () (interactive)
      (find-file "/Users/HOME/.0.data/todo.org")
      (message "Opened:  %s" (buffer-name))))
    

    OPTION # 2 -- function with options:

    (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"))))
    

    OPTION # 3 -- right click context menu (popup):

    (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))
    

    OPTION # 4 -- menubar drop-down menu:

    (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")]
        )  ))