Skip to content

Instantly share code, notes, and snippets.

@zhannes
Forked from bamanzi/gist:2250340
Created July 4, 2012 17:54
Show Gist options
  • Save zhannes/3048588 to your computer and use it in GitHub Desktop.
Save zhannes/3048588 to your computer and use it in GitHub Desktop.
[emacs] Use frame to group buffers (similar to perspective feature)
;; from https://gist.github.com/2250340
(defun frame-bufs-switch-buffer ()
"Switch buffer, within buffers associated with current frame (`frame-bufs-buffer-list')"
(interactive)
(if (and (fboundp 'frame-bufs-mode)
frame-bufs-mode)
(let* ( (buffers (mapcar 'buffer-name (frame-bufs-buffer-list (selected-frame))))
(buffers-rotated (append (cdr buffers) (cons (car buffers) nil)))
(target (ido-completing-read "Buffer: " buffers-rotated)) )
(switch-to-buffer target))
(call-interactively 'ido-switch-buffer)))
(defun frame-bufs-mode-hook ()
;; adapted from from https://gist.github.com/2250340
;; After loading a file into buffer, it seems to requires two calls to 'C-x b'
;; for the new buffer to be included in the list.
;; Calling (frame-bufs-reset-frame) seems to fix so that the first call
;; to 'C-x b' includes the newly added buffer in the list.
(add-hook 'buffer-list-update-hook
(lambda ()
(frame-bufs-reset-frame)))
(add-hook 'frame-bufs-mode-on-hook
(lambda ()
(global-set-key (kbd "C-x b") 'frame-bufs-switch-buffer)
(global-set-key (kbd "C-x B") 'ido-switch-buffer)))
(add-hook 'frame-bufs-mode-off-hook
(lambda ()
(global-set-key (kbd "C-x b") 'ido-switch-buffer)))
(require 'frame-bufs)
(frame-bufs-mode t))
@zhannes
Copy link
Author

zhannes commented Jul 4, 2012

These fn's (in combination with self-discipline in which files to open) allow me to compartmentalize my workflow into several topics/channels. You might want separate Emacs windows* for different projects/apps. You might also want to separate files by topic. For example, separate windows for:

  • server side code
  • client-side javascript
  • view templates and HTML

Pressing 'C-x b' is the Emacs command to show currently open buffers**. Whereas the default behavior of emacs is to list all open buffers across multiple frames, I do want that default. When I list open buffers, I only want to see what is open in the current window/frame. Presumably, each window/frame would only have related files on that topic.

When I'm editing client-side JS and I want to switch to another JS file, I don't want to see or think about Ruby or other un-related files, or other distractions. Or, if I'm in a Ruby project but also have a Wordpress project open, I don't want to see Wordpress files when I look for Ruby files.

* Emacs uses the term 'window' to refer to the currently visible 'buffer'.
** Emacs uses the term 'buffers'. You might have the file 'app.js' open in a buffer. The name of the buffer will correspond to the file name. But, buffers are not solely used for files. For example, the results of a grep, help screen, etc would be in a buffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment