Buffers and Windows and Frames, Oh My!
One thing that I really like about emacs is how it manages open files (buffers) and the diversity one has in customizing the workspace. Of course, I didn’t always like it. It took some getting used to, but after not that long of a time frame I got the hang of it and started to enjoy it. Soon after that, I started customizing my emacs workspace to be even more efficient and productive for me. One thing I’ve found is that I really like to see everything that’s going on, if possible. This is why I typically use a tiling window manager (preferably StumpWM or Ratpoison). I also try to avoid using the mouse. This has been the basis for the way I work in emacs and has led to the setup I’m current using. I run lots of processes inside emacs: Mew for email, elim for instant messaging, twitter.el for checking Twitter, ansi-term for a terminal, etc. (I’m sure I’ll go into more detail on these in later posts). Of course, in order to see all these at once, I have two choices: multiple frames or multiple windows.
First things first: what emacs calls a “frame” and a “window” is opposite of what just about every other program calls them. For instance, a web browser “window” can be split into multiple “frames” (top heading, menu on the left, copyright footer, content in the middle, etc). A “window” is usually thought of as being one of the windows open in a window manager, complete with maximize, minimize, and close buttons. Emacs is the reverse. When you first open emacs, the “window” that pops up is actually called a “frame”. There can be multiple frames open that can be switched between like normal “windows”, using Alt-<Tab>. Each of these “frames” can then be split into “windows” of various sizes. Each window can show a different (or the same) buffer: whether its a source code file, a terminal, or a web browser, whatever.
I think that most people work with multiple frames and a few windows in some frames. I work with one maximized frame (at my work, this means 1900x1280) split into many windows, and I mean many. My current setup uses 17 different windows inside one emacs frame. (Screenshot) If I has multiple frames, I would have to Alt-<Tab> a bunch of times or use the mouse to switch between the frames. My way, I can use emacs commands to switch between windows in the same frame. The basic command is C-x o (other-window) which basically moves focus to the next window. With 17 windows, that’s no better than using Alt-<Tab> as I would have to type C-x o many times, or prefix it with a number of times (i.e. C-u 1 0 C-x o to run other-window 10 times). The latter, of course, requires me to know exactly how many windows are in between where I am and where I want to be.
This brings me to a very useful mode I have found and was actually brought up in the comments of my last post: window-number-mode. This is a wonderful minor mode that gives each window in a frame a number (in red in the mode line by default) from the top-left to the bottom-right. You can then use a command (mine is C-x j <number>) to switch directly to the window with number <number>. There is a short version allowing M-<number>, but it has the drawback (for me, anyway) of only taking one digit, meaning it is only useful for 10 or less windows. I suggest window-number-mode to anyone who works with any number of multiple-window frames regularly.
Somehow, though, 17 windows isn’t always enough to show me every buffer I have open! (OK, granted, I don’t always want to see every buffer, and of the 17 windows, only one is used for editing/programming). Therefore, I still have to do some buffer switching. The standard C-x b (switch-to-buffer) works fine for a bit, but I found myself wanting to type less and be more efficient. I found several switch-to-buffer modifiers and replacements, and I’ve come to really enjoy and rely on ido-mode. Ido-mode adds context and matching to switch-to-buffer. Using ido-mode, when I type C-x b, I get (in the echo area) a list of the files in the current directory. I can start typing to filter the list (typing ‘ser’ would match, for instance, User.php, server.txt, etc.). I can use C-s and C-r to cycle through the currently shown files, so if I type ‘ser’ and am left with User.php first and server.txt second, I can hit enter to open the first item or type C-s to “slide over” to the second item, and press enter to open it instead. Ido also has “smart matching” (I can’t remember what its actually called off hand) meaning I can type U.p and basically get matches for U.p (i.e. User.php, Ultimate.perl, etc). One of my favorite features is that, if I want to go back a directory, I only have to type delete once instead of once for each letter in the directory name. Finally, ido-mode works when opening new files, not just switching buffers. Ido offers many other features and commands as well.
I’ve heard some people speak ill of ido-mode. Some people prefer iswitchb, others prefer other solutions. Ido-mode works great for me and I really enjoy it. It has definitely made switching buffers much faster, easier, and more efficient for me.
So, that’s a breakdown of how I handle all of the buffers I usually have open in emacs. I hope that it provides some insight into one of many ways of Getting Things Done in emacs, even if my way is (most likely) not a very common practice.