XSteve's Emacs Power User Tips

I use Emacs as integration environment for most of the tasks I need to do. The other applications I use are:
  • ion3: A Unix window manager that can be used very effective via the keyboard
  • zsh: A very powerful shell
  • Mozilla Firefox: You certainly know that one ;-)
  • Emacs: everything else. See the rest of the page for a bunch of tips

I am also the author of various emacs lisp addon packages.

Finding Files

  • C-x C-f: The standard way, I use the ffap bindings
  • ;;; find file at point
    (require 'ffap)
    ;; rebind C-x C-f and others to the ffap bindings (see variable ffap-bindings)
    (ffap-bindings)
    ;; C-u C-x C-f finds the file at point
    (setq ffap-require-prefix t)
    ;; browse urls at point via w3m
    (setq ffap-url-fetcher 'w3m-browse-url)
    

  • C-u C-x C-f: Find the file at point (via the ffap package), relative filenames work also
  • C-x C-j: dired-jump
  • ;; provide some dired goodies and dired-jump at C-x C-j
    (load "dired-x")
    

    C-x C-j opens a dired buffer with point at the actual file name.
    You can navigate in this buffer with the cursor keys and open a file via RET
  • Bind M-F12 to recentf-open-files
  • ;;recentf
    (require 'recentf)
    (recentf-mode 1)
    (setq recentf-max-saved-items 500)
    (setq recentf-max-menu-items 60)
    (global-set-key [(meta f12)] 'recentf-open-files)
    

    M-F12 opens a buffer that contains the recent opened buffers
    Use C-s to search for a filename and hit RET to open the file.
  • Bind M-F11 to a function that uses ido on the recently opened files
  • (defun xsteve-ido-choose-from-recentf ()
      "Use ido to select a recently opened file from the `recentf-list'"
      (interactive)
      (let ((home (expand-file-name (getenv "HOME"))))
        (find-file
         (ido-completing-read "Recentf open: "
                              (mapcar (lambda (path)
                                        (replace-regexp-in-string home "~" path))
                                      recentf-list)
                              nil t))))
    
    (global-set-key [(meta f11)] 'xsteve-ido-choose-from-recentf)
    

    M-F11 uses the ido completion functionality to select a file from the recentf-list. Note: ido-completing-read needs emacs 22.
  • Use M-x desktop-save to keep the opened files persistent between sessions
  • ;; save a list of open files in ~/.emacs.desktop
    ;; save the desktop file automatically if it already exists
    (setq desktop-save 'if-exists)
    (desktop-save-mode 1)
    
    ;; save a bunch of variables to the desktop file
    ;; for lists specify the len of the maximal saved data also
    (setq desktop-globals-to-save
          (append '((extended-command-history . 30)
                    (file-name-history        . 100)
                    (grep-history             . 30)
                    (compile-history          . 30)
                    (minibuffer-history       . 50)
                    (query-replace-history    . 60)
                    (read-expression-history  . 60)
                    (regexp-history           . 60)
                    (regexp-search-ring       . 20)
                    (search-ring              . 20)
                    (shell-command-history    . 50)
                    tags-file-name
                    register-alist)))
    

    Use M-x desktop-save once to save the desktop.
    When it exists, Emacs updates it on every exit.

Switching Buffers

  • C-x b: The standard binding, I use ido for buffer switching
  • (ido-mode 'buffer)
    (setq ido-enable-flex-matching t)
    

    (ido-mode 'buffer) enables ido for buffer switching
    ido-enable-flex-matching means that if the entered string does not match any buffer name, any buffer name containing the entered characters in the given sequence will match.
  • I use F12 to invoke ibuffer
  • (setq ibuffer-shrink-to-minimum-size t)
    (setq ibuffer-always-show-last-buffer nil)
    (setq ibuffer-sorting-mode 'recency)
    (setq ibuffer-use-header-line t)
    (global-set-key [(f12)] 'ibuffer)
    

    Ibuffer shows a buffer list that allows to perform almost any imaginable operation on the opened buffers.
  • I use F11, Shift-F11 to switch to the last recent used buffer
  • My package bubble-buffer.el allows to navigate to the last recent used buffer via one keystroke (F11).
    When I press F11 again, the buffer before is selected. Shift-F11 selects the buffer that was selected before by the bubble-buffer package.
    The interesting property of the package is, that it does not destroy the order of the buffer list, when a buffer is selected.
    (when (require 'bubble-buffer nil t)
      (global-set-key [f11] 'bubble-buffer-next)
      (global-set-key [(shift f11)] 'bubble-buffer-previous))
    (setq bubble-buffer-omit-regexp "\\(^ .+$\\|\\*Messages\\*\\|*compilation\\*\\|\\*.+output\\*$\\|\\*TeX Help\\*$\\|\\*vc-diff\\*\\|\\*Occur\\*\\|\\*grep\\*\\|\\*cvs-diff\\*\\)")
    

  • Use M-F11: xsteve-ido-choose-from-recentf (see above)
  • xsteve-ido-choose-from-recentf allows me to switch to any recently opened file.
    The nice thing, using that function is, that it does not matter, if I have the buffer already opened, or if the file must be opened now.
    With that function I have a persistent buffer list available.

Shell Integration

  • Switch to the current emacs directory in a running zsh instance
  • (defun xsteve-save-current-directory ()
      "Save the current directory to the file ~/.emacs.d/current-directory"
      (interactive)
      (let ((dir default-directory))
        (with-current-buffer (find-file-noselect "~/.emacs.d/current-directory")
          (delete-region (point-min) (point-max))
          (insert (concat dir "\n"))
          (save-buffer)
          (kill-buffer (current-buffer)))))
    (global-set-key [(super f10)] 'xsteve-save-current-directory)
    

    Hitting s-F10 saves the current directory to a file
    ;; a part of my ~/.zshrc
    do-cd-emacs() {
           LBUFFER="cd $(cat ~/.emacs.d/current-directory)"
           zle accept-line
    }
    zle -N do-cd-emacs
    bindkey '\e[21~' do-cd-emacs #F10
    

    Hitting F10 in a running zsh switches to the recently saved directory from emacs
  • Open a file in the current running emacs from a zsh
  • (server-start)
    

    Just start the emacs server in the .emacs
    ;; a part of my ~/.zshrc
    function ec
    {
        emacsclient --no-wait "$PWD/$1"
    }
    
    function ecw
    {
        emacsclient "$PWD/$1"
    }
    

    The functions ec and ecw allow to open a file or a directory in the currently running emacs.
    The following opens file.txt:
    ec file.txt


Here is the outline of the topics I will cover in the near future:

Search and Replace

Various Navigation Commands

Copying and yanking text

Use Dired for File Operations

Dynamic abbreviations and hippie-expand

Use Emacs Calc as calculator



Last modified: Tue Jul 24 23:10:44 CEST 2007