12

I use ido-mode, but one thing I hate about it is that after C-x C-f I cannot use up/down keys to cycle through recently opened files, as it used to be for example with iswitchb. How can I set ido to work this way?

16

Looking at the customization options for ido-mode (M-x customize-group RET ido RET), I don't see any options for enabling the cycling behavior you describe.

You can, however, add the following to your init-file:

(require 'recentf)

(defun ido-recentf-open ()
  "Use `ido-completing-read' to find a recent file."
  (interactive)
  (if (find-file (ido-completing-read "Find recent file: " recentf-list))
      (message "Opening file...")
    (message "Aborting")))

(global-set-key (kbd "C-x C-r") 'ido-recentf-open)

With this in place you can press C-x C-r to get Ido completion for selecting recently opened files.

By default, the 20 most recent files will be kept in the history. I suggest you crank that up to something like 150 by setting recentf-max-saved-items:

(setq recentf-max-saved-items 150)

Bonus: Accessing recent files, on-steroids edition

I don't see it mentioned in a lot of places, but Ido Virtual Buffers make it super easy to access recent files. Enable them like so:

(setq ido-use-virtual-buffers t)

The behavior you get is this:

If non-nil, refer to past ("virtual") buffers as well as existing ones.

Essentially it works as follows: Say you are visiting a file and the buffer gets cleaned up by midnight.el. Later, you want to switch to that buffer, but find it's no longer open. With virtual buffers enabled, the buffer name stays in the buffer list (using the ido-virtual face, and always at the end), and if you select it, it opens the file back up again. This allows you to think less about whether recently opened files are still open or not. Most of the time you can quit Emacs, restart, and then switch to a file buffer that was previously open as if it still were.

  • So far I've upvoted, I will accept later when I will test it and understand how it works. (my understanding of lisp is very poor). – WeSenseASoulInSearchOfAnswers Nov 4 '14 at 9:46
  • @WeSenseASoulInSearchOfAnswers Sure, take your time. Let me know if you have any specific questions about the code I posted. – itsjeyd Nov 4 '14 at 9:55
  • I accepted but frankly speaking I don't like it because with C-x C-r it is not possible to open new file. I'm also surprised it is not possible to customize it in simple way. The history of C-x C-f is such an obvious feature that I cannot understand why is not implemented in ido yet. For example M-x up/down cycles through recent commands, M-% up/down cycles through recent query replace, even outside emacs - all shells as bash or zsh use up/down to recall previous commands. For me C-x C-f up/down should cycle through recently opened files. Full stop. – WeSenseASoulInSearchOfAnswers Nov 4 '14 at 13:09
  • @WeSenseASoulInSearchOfAnswers I agree with you, and I have no idea why this isn't available or customizable out of the box. For me personally, it's not a big deal because I use the virtual buffers feature I describe in my answer, so switching to a recent file is the same as switching to another buffer; really easy. But I understand that it can be frustrating if a feature you are used to using is not available. – itsjeyd Nov 4 '14 at 13:32
  • 5
    +1 for ido-use-virtual-buffers -- a great feature which should really be on by default IMO. – Jon O. Nov 11 '14 at 1:35
2

Though the bindings have evolved over time, as of today, when you invoke ido-find-file or ido-find-file-read-only, you can use the following bindings available in the default configuration:

  • M-o invokes ido-prev-work-file
  • C-M-o invokes ido-next-work-file

Apart from these not being as ergonomically pleasant as the M-p and M-n bindings I had been used to before trying ido, they're also slow, and the consequent minibuffer chatter is distracting and confusing. ido does something more than just show a recently opened file name; it reports that it's "Searching for 'file name'...", perhaps out of reluctance to offer a name for a file that no longer exists.

The "Searching for" message comes from function ido-make-merged-file-list. Reading the source, I don't see any way to disable whatever magic this function is doing.

You may consider rebinding the ido-prev-work-file and ido-next-work-file pair to something more natural like C-M-p and C-M-n, or swapping the current M-p (ido-prev-work-directory) and M-n (ido-next-work-directory) bindings for these.

Here is the Emacs Lisp code to restore M-p and M-n to cycle through recent files, moving the default directory-focused bindings to C-M-p and C-M-n respectively:

(add-hook 'ido-setup-hook
          (lambda ()
            (let ((kmap ido-file-dir-completion-map))
              (let ((key '(meta ?n)))
                (define-key kmap (vector (cons 'control key))
                  (lookup-key kmap (vector key)))
                (define-key kmap (vector key) 'ido-next-work-file))
              (let ((key '(meta ?p)))
                (define-key kmap (vector (cons 'control key))
                  (lookup-key kmap (vector key)))
                (define-key kmap (vector key) 'ido-prev-work-file)))))
  • 1
    Welcome to Emacs SE! Please try to keep your answers focused on the original poster's question. Surely your comment about the "minibuffer chatter" is interesting and related, but it distracts from finding an actual answer to the question. More importantly: please try to phrase your answer in the form of... well, an answer. What you wrote reads more like a comment, which is not well suited for a Q&A site like this. – paprika Nov 26 '14 at 16:32
  • Well, the first paragraph and its following list answers the original question, which was looking for keys to cycle through recently opened files. Those bindings exist by default, but none of the other answers here mentioned these bindings. I am working on the rebinding code, and will include it in my answer once I'm satisfied with it. – seh Nov 26 '14 at 16:37
1

The answer given by @itsjeyd is pretty complete. Let me just suggest a function:

(defun ido-choose-from-recentf ()
  "Use ido to select a recently visited file from the `recentf-list'"
  (interactive)
  (find-file (ido-completing-read "Open file: " recentf-list nil t)))

;; bind it to "C-c f"
(global-set-key (kbd "C-c f") 'ido-choose-from-recentf)

Courtesy of WikEmacs

1

I found helm-for-files works better than any of the above answers for the purpose of file name auto-completion and having nice search interface. Also, you can configure it to include any search items you like.

  • 1
    @Drew helm comes with ido mode itself. Most of the time when people search this post they might be interested in knowing other alternatives to solve the problem. – user10375 Aug 15 '17 at 13:35
1

what I've resorted to in this case is going back to normal minibuffer behaviour by typing C-f in idomode. Then you'll have normal history browsing keys. If you know it in advance, it can be quick : C-x-f-f :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.