Starter Kit Ido

This is part of the Emacs Starter Kit.

Starter Kit Ido

ido-mode is like magic pixie dust!

Basic configuration

(ido-mode t)
(setq ido-everywhere            t
      ido-enable-prefix         nil
      ido-enable-flex-matching  t
      ido-auto-merge-work-directories-length nil
      ;;ido-use-filename-at-point t
      ido-max-prospects         10
      ido-create-new-buffer     'always
      ;; ido-use-virtual-buffers   t
      ;; ido-handle-duplicate-virtual-buffers 2
      ido-default-buffer-method 'selected-window
      ido-default-file-method   'selected-window)

Keybindings

Since ido buffer/file list is shown vertically, use <up/down> keys for navigating through ido item

(defun ido-my-keys ()
  (define-key ido-completion-map (kbd "<up>")   'ido-prev-match)
  (define-key ido-completion-map (kbd "<down>") 'ido-next-match))

(add-hook 'ido-setup-hook 'ido-my-keys)

Extensions ordering and ignored ones

(setq ido-file-extensions-order     '(".cc" ".h" ".tex" ".sh" ".org"
                                      ".el" ".tex" ".png"))
(setq completion-ignored-extensions '(".o" ".elc" "~" ".bin" ".bak"
                                      ".obj" ".map" ".a" ".so"
                                      ".mod" ".aux" ".out" ".pyg"))
(setq ido-ignore-extensions t)

Keep annoying buffers out of search

(setq ido-ignore-buffers (list (rx (or (and bos  " ")
                                       (and bos
                                            (or "*Completions*"
                                                "*Shell Command Output*"
                                                "*vc-diff*")
                                            eos)))))

Allow spaces when using ido-find-file

(add-hook 'ido-make-file-list-hook
          (lambda ()
            (define-key ido-file-dir-completion-map (kbd "SPC") 'self-insert-command)))

Show ido results vertically, rather than horizontally

(setq ido-decorations (quote ("\n-> " "" "\n " "\n ..." "[" "]" "
  [No match]" " [Matched]" " [Not readable]" " [Too big]" "
  [Confirm]")))
(defun ido-disable-line-truncation () (set (make-local-variable 'truncate-lines) nil))
(add-hook 'ido-minibuffer-setup-hook 'ido-disable-line-truncation)

Using recentf within ido

(defun recentf-ido-find-file ()
    "Find a recent file using Ido."
    (interactive)
    (let* ((file-assoc-list
            (mapcar (lambda (x)
                      (cons (file-name-nondirectory x)
                            x))
                    recentf-list))
           (filename-list
            (remove-duplicates (mapcar #'car file-assoc-list)
                               :test #'string=))
           (filename (ido-completing-read "Choose recent file: "
                                          filename-list
                                          nil
                                          t)))
      (when filename
        (find-file (cdr (assoc filename
                               file-assoc-list))))))

Setting ido everywhere

(ido-ubiquitous-mode)
;;(ido-at-point-mode)

Setting flx-ido-mode

Fuzzy matching for Emacs … a la Sublime Text. See https://github.com/lewang/flx

(flx-ido-mode)

Increase emacs garbage collection size

(setq gc-cons-threshold 20000000)

Advice ido to open file as root

From Emacs Redux.

(defadvice ido-find-file (after find-file-sudo activate)
  "Find file as root if necessary."
  (unless (and buffer-file-name
               (file-writable-p buffer-file-name))
    (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))

Misc.

(define-key ido-file-completion-map (kbd "C-w") 'ido-delete-backward-updir)

Deprecated

;; Add project file to ido search using F6 shortcut
(defun my-ido-project-files ()
  "Use ido to select a file from the project."
  (interactive)
  (let (my-project-root project-files tbl)
    (unless project-details (project-root-fetch))
    (setq my-project-root (cdr project-details))
    ;; get project files
    (setq project-files
          (split-string
           (shell-command-to-string
            (concat "find "
                    my-project-root
                    " \\( -name \"*.svn\" -o -name \"*.git\" \\) -prune -o -type f -print | grep -E -v \"\.(pyc)$\""
                    )) "\n"))
    ;; populate hash table (display repr => path)
    (setq tbl (make-hash-table :test 'equal))
    (let (ido-list)
      (mapc (lambda (path)
              ;; format path for display in ido list
              (setq key (replace-regexp-in-string "\\(.*?\\)\\([^/]+?\\)$" "\\2|\\1" path))
              ;; strip project root
              (setq key (replace-regexp-in-string my-project-root "" key))
              ;; remove trailing | or /
              (setq key (replace-regexp-in-string "\\(|\\|/\\)$" "" key))
              (puthash key path tbl)
              (push key ido-list)
              )
            project-files
            )
      (find-file (gethash (ido-completing-read "project-files: " ido-list) tbl)))))
;; bind to a key for quick access
(define-key global-map [f6] 'my-ido-project-files)

(require 'project-root)
(setq project-roots
      '(("your project name"
         :root-contains-files ("LICENSE.GPL3.txt")
         )))
File under version control - commit 135971e - 2014-11-29