Tags: ido

RSS - Atom - Subscribe via email

Emacs Basics: Call commands by name with M-x (with tips for better completion using ido or helm)

Posted: - Modified: | emacs, emacs-basics, podcast

Emacs has way too many keyboard shortcuts to memorize. Fortunately, you can call commands by name by typing M-x and the name of the command. M- stands for the Meta key. If your keyboard does not have a Meta key (and most don't, these days), use Alt or Option. For example, on a PC keyboard, you can type Alt-x. Alternatively, you can replace Meta with ESC. M-x then becomes ESC x.

If you know the name of the command to execute, you can type it after M-x, and then press RET (the Return key, which is the same as the Enter key). For example, M-x find-file opens a file. M-x save-buffer saves the current file. You can use TAB to complete words. Use <up> and <down> to go through your command history.

What if you don't know the name of the command to execute? You can use M-x apropos-command to search for the command using keywords. If you know the keyboard shortcut or you can find the command on a menu, you can also use M-x describe-key and then do the keyboard shortcut or select it from the menu.

If a command you execute has a keyboard shortcut, it will flash briefly at the bottom of your screen. For example:

You can run the command `find-file' with C-x C-f

Using TAB for completion can be a little slow. Here are two ways to make that and a whole lot of other things faster: ido and helm. To explore these approaches, you will need to add the MELPA package repository to your configuration. To set that up, add the following to the beginning of your ~/.emacs.d/init.el file.

(package-initialize)
(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)

Then use M-x eval-buffer to load the changes into your current Emacs, and use M-x package-refresh-contents to reload the list of packages.

Helm mode

This is what completion with Helm looks like:

2014-03-17 13_06_54-c__sacha_personal_organizer.org.png

Figure 2: Helm

Use M-x package-install to install the helm package. Then you can try it out with M-x helm-mode . After you start Helm mode, try M-x again. You can type in multiple words to search for a command, and you can use <up> and <down> to go through completions. Use M-p and M-n to go through your command history.

If you like it, here's some code that you can add to your ~/.emacs.d/init.el file to load it automatically next time, and to tweak it for more convenience.

(require 'helm-config) 
(helm-mode 1)

Use M-x eval-buffer to load your changes.

If you change your mind and want to disable helm-mode, you can toggle it off with M-x helm-mode .

If you like how that works, you may want to (global-set-key (kbd "M-x") 'helm-M-x). If you do, you'll be able to see keybindings when you call commands with M-x. Note that if you want to use a prefix argument (ex: C-u), you will need to do that after calling M-x instead of before.

Ido, ido-hacks, smex, ido-vertical-mode, and flx-ido

Ido is like Helm, but it takes a different approach. Here's what this combination will get you:

2014-03-17 12_40_40-MELPA.png

Figure 1: ido, smex, ido-vertical-mode, and flx-ido

If you want to give this a try, remove or comment out (helm-mode 1) from your ~/.emacs.d/init.el (if you added it), and disable helm-mode if you still have it active from the previous section.

To set Ido up, use M-x package-install to install ido, smex, ido-vertical-mode, ido-hacks, and flx-ido.

After the packages are installed, add the following code to your ~/.emacs.d/init.el .

(ido-mode 1)
(require 'ido-hacks nil t)
(if (commandp 'ido-vertical-mode) 
    (progn
      (ido-vertical-mode 1)
      (setq ido-vertical-define-keys 'C-n-C-p-up-down-left-right)))
(if (commandp 'smex)
    (global-set-key (kbd "M-x") 'smex))
(if (commandp 'flx-ido-mode)
    (flx-ido-mode 1))

Use M-x eval-buffer to load your changes, then try M-x again. You should now have much better completion. You'll be able to call commands by typing in part of their names. Use <up> and <down> to go through the completion options, and use <left> and <right> to go through your history.

Try it for a week. If you like it, keep it. If you don't like it, try the Helm approach.

Other tips

When you learn keyboard shortcuts, try to remember the names of the commands as well. You can do that with C-h k (describe-key). For example, M-x calls the command execute-extended-command. That way, even if you forget the keyboard shortcut, you can call the command by name.

If you forget the name of the command and you don't know the keyboard shortcut for it, you can look for it in the menus or in the help file. You can open the help file with C-h i (info). You can also use M-x apropos-command to search through the commands that you can call with M-x.

Make your own cheat sheet with frequently-used keyboard shortcuts and commands to help you learn more about Emacs. Good luck!

Emacs Basics: M-x

Emacs Basics: M-x

You can download the MP3 from archive.org.

Emacs: Working with multiple source trees

| emacs

As a developer, I often find myself working with multiple trees of source code. Sometimes, I’m comparing the trunk and the branches. Sometimes, I’m copying ideas from one place to another. Sometimes, I’m working on one project and an urgent defect comes in for a different project. With good development environments such as Eclipse, I’d still need to click on the project or directory in order to change my context, and then use a keyboard shortcut to find the resource in that tree.

With an awesome development environment like my customized Emacs, however, I can easily juggle multiple source trees. Here’s the macro I wrote to make setting this up much easier:

(defmacro sacha/file-cache-setup-tree (prefix shortcut directories)
  "Set up the file-cache tree for PREFIX using the keyboard SHORTCUT.
DIRECTORIES should be a list of directory names."
  `(let ((file-cache-alist nil)
	 (directories ,directories))
     (file-cache-clear-cache)
     (while directories
       (file-cache-add-directory-using-find (car directories))
       (setq directories (cdr directories)))
     (setq ,(intern (concat "sacha/file-cache-" prefix "-alist")) file-cache-alist)
     (defun ,(intern (concat "sacha/file-cache-ido-find-" prefix)) ()
       (interactive)
       (let ((file-cache-alist ,(intern (concat "sacha/file-cache-" prefix "-alist"))))
	 (call-interactively 'file-cache-ido-find-file)))
     (global-set-key (kbd ,shortcut)
		     (quote ,(intern (concat "sacha/file-cache-ido-find-" prefix))))))

With that, I can use the following to map C-c p to finding files in my personal directories:

(sacha/file-cache-setup-tree
 "personal"
 "C-c p"
 '("/var/www/html/drupal"
   "~/elisp"
   "~/personal"))

I’ve also set up keyboard shortcuts for the other trees I’m working on.

You’ll need file-cache, ido, and probably something like this:

(require 'filecache)
(require 'ido)
(defun file-cache-ido-find-file (file)
  "Using ido, interactively open file from file cache'.
First select a file, matched using ido-switch-buffer against the contents
in `file-cache-alist'. If the file exist in more than one
directory, select directory. Lastly the file is opened."
  (interactive (list (file-cache-ido-read "File: "
                                          (mapcar
                                           (lambda (x)
                                             (car x))
                                           file-cache-alist))))
  (let* ((record (assoc file file-cache-alist)))
    (find-file
     (expand-file-name
      file
      (if (= (length record) 2)
          (car (cdr record))
        (file-cache-ido-read
         (format "Find %s in dir: " file) (cdr record)))))))

(defun file-cache-ido-read (prompt choices)
  (let ((ido-make-buffer-list-hook
	 (lambda ()
	   (setq ido-temp-list choices))))
    (ido-read-buffer prompt)))
(add-to-list 'file-cache-filter-regexps "docs/html")
(add-to-list 'file-cache-filter-regexps "\\.svn-base$")
(add-to-list 'file-cache-filter-regexps "\\.dump$")