tags:

views:

256

answers:

6

I've been using emacs for all of my text editing needs for the past three years now. When I'm in a single file, working on code or whatnot, I'm fairly efficient. With two files, I can "C-x b RET" between them and I do fine. However, whenever I need to be working on more than two files at a time, I tend to get lost.

Here are some of the problems that I'd like to work on:

  1. I forget what some of my buffers are called, but I don't understand why C-x C-b splits my window into two buffers and exits the mini buffer. Sure I can switch buffers and choose a buffer to visit, but this feels unintuitive, and leaves me with two buffers open.

  2. When I visit a directory rather than a file, I have a convenient list of all of the files and directories. I usually want to do one of two things with this: 1) Open a single file and never see this buffer again OR 2) Open a bunch of files and never see this buffer again. I don't really know how to do this, as moving the point to a file and hitting return doesn't do either of these things.

  3. I know that my buffers aren't like tabs, but I have an inclination to want to scroll through them to find what I want. I don't know of any key-bindings for this, but I'd like it to be M-n / M-p or the like. Then again, this may be a horribly inefficient way to switch buffers.

  4. When I open interactive help of any kind (for example in ESS), I have a habit of switching back to the buffer I was working in and using C-x 1 to get back to a single buffer. When I do this, however, the help buffer hangs around in my buffer list, further confusing me. I know I can switch to that buffer, kill it, switch back, and then go back to a single buffer, but this seems wrong.

The way I've dealt with this so far involves using a tiling window manager and a few emacs windows in different work-spaces, rather than actually learn the best way to manage a number of files in emacs. I don't necessarily want to change emacs to better fit my needs (although I am open to that if it fits in with what I'm about to say), instead, I'd like to grok the thought process behind handling files/buffers the way that emacs does, and how I can be more efficient with it.

Any answer that would help me understand the correct way, or a more efficient way to manage my buffers or files would be greatly appreciated.

+9  A: 

I think you will really enjoy Ido for dealing with multiple buffers who's names you can't exactly remember. When you type C-x b it shows a list of open buffers in most used order. As you type some of the characters in a buffer name the list is filtered. The characters you type don't have to be at the begging of the name or contiguous. Using C-f, C-b or left/right arrow keys cycles through the buffer choices.

Also see Smex for Ido like functionality for M-x

Closing windows is done with C-x 0. Intentionally splitting the window is done with C-x 2 for horizontal, C-x 3 for vertical. I love this feature, since it allows me to have test and production code visible at the same time. C-x o takes me to the other window.

Bryan Ash
And of course `C-x 1`, which closes any *other* windows in the frame, maximising the current one. There's also `C-x 4 0` which is bound to `kill-buffer-and-window`, which may be an apt solution to one of the original problems.
phils
While there are other great answers, I'm marking this one as accepted, since Ido solves my most frustrating problem of forgetting the names of my buffers quite elegantly.
Wilduck
I also just realized that I can kill buffers from the minibuffer with Ido. C-b then choose the one I want to kill and C-k. Wonderful.
Wilduck
+6  A: 
  1. Bind C-x C-b to ibuffer. This is a better buffer listing facility with many advanced features, and its default behaviour is to replace the current buffer with the buffer listing, and then bury the listing when you select a buffer (leaving you with the newly-selected buffer in place of the original one).

    You can simply use C-x b to enter your selection in the mini-buffer, of course; however the tab-completion (which is needed to make this a viable option, IMO) does open a new window temporarily, at which point I think you might as well familiarise yourself with something with more features.

  2. Use a instead of RET when selecting from dired. This kills the dired buffer instead of leaving it behind. C-h m in any buffer will show you the help for its major mode (followed by help for the minor modes), and you can read about all the available dired key bindings there.

  3. http://www.emacswiki.org/cgi-bin/wiki/TabBarMode ? (edit: I prefer Rémi's answer for this one, but TabBarMode would give you the visual tab element if you were particularly keen on that.)

  4. q is bound to a 'quit' function in a great many major modes. Generally it buries the buffer rather than killing it, but I certainly find that fine.

To elaborate a little on #1, ibuffer has lots of nice features, and M-x customize-group ibuffer RET will give you some idea of how you can customise it to your liking.

Furthermore, you can filter the buffer list by many criteria (again, use C-h m to see its help page), and then generate a 'group' definition from the current filters, and save your custom filters and groups for future usage.

For example:

  • / f ^/var/www/ RET: filter buffer list to show only filenames starting with /var/www/.
  • / s Web filters RET: name and save active filter set to your init file.
  • / g Web development RET: create a named group from the active filters.
  • / S My groups RET: name and save group definitions to your init file.
  • / r Web filters RET: invoke the "Web filters" filters.
  • / R My groups RET: invoke the "My groups" groups.
  • RET on a group name to collapse or expand it.
  • C-k and C-y to kill and yank groups, to re-arrange them.
  • C-h m for more information...

This way you can have a single Emacs instance running, and create filters and groups for different types of task, and easily switch between them.

phils
Re-reading the question, I should probably emphasise the filtering and grouping abilities of ibuffer. I've updated my answer accordingly.
phils
Thanks for your answer! I had no idea that you could use a in dired. I'm a little confused at your answer for my fourth question though. Just 'q'? In an ESS help buffer this either moves me to the other buffer when my windows are split, or splits my window. Maybe I'm doing it wrong, but that doesn't seem that useful...
Wilduck
Sorry, I'm not familiar with ESS. I had assumed you were referring to interactive help using the Emacs *\*Help\** buffer/mode, in which 'q' is bound this way (as indeed it is in dired, buffer-menu, ibuffer, bs-show, and at least a good proportion (probably the majority) of built-in major modes which bind alpha-numeric characters to commands. (If ESS help fits this description, perhaps you could suggest it to them as an improvement?) As mentioned in another comment, `C-x 4 0` runs `kill-buffer-and-window` which makes for a decent generic solution.
phils
Thanks, that clarified things perfectly.
Wilduck
+1  A: 

I think the number one most useful extension for flipping through buffers is Anything. It lets you start typing part of a buffer (or file!) name and it will figure out what you want. I've rebound C-x b to anything-for-buffers. It makes life so much better.

docgnome
I'll have a look at this. But I normally know where my files are on my computer. Just not in emacs.
Wilduck
+3  A: 

I use C-x right (or C-x C-right) and C-x left (or C-x C-right) to go to the next and previous buffer. I don't mind anymore off the few buffer that lay around in Emacs but you could use k in the buffer list to kill the buffer you don't use anymore.

Rémi
For a variant on this (as well as on the default `list-buffers`), there's also bs. `bs-show` (or `bs-show-sorted`) is basically a slicker version of list-buffers, without the fancy features of something like ibuffer. It also provides `bs-cycle-next` and `bs-cycle-previous` which you can use to switch buffers in the same fashion as the commands that Rémi mentions. These also indicate the next few buffers in the sequence within the minibuffer, which adds some extra value. Check the documentation for the 'cycle' functions, as you can configure the *kinds* of buffers that it will cycle through.
phils
bs doesn't provide its own key-bindings, but if you preferred its features to the other alternatives, I would simply re-bind `C-x C-left`, `C-x C-right`, and `C-x C-b` to the bs functions, as desired. See also `M-x customize-group bs RET`.
phils
This is great. I'm sure I could have googled this, but for some reason I was caught up on M-n and M-p. C-x right works just as well. Thank you.
Wilduck
+2  A: 

You can also try Iswitchb mode which provides auto-completion for buffer names when you switch buffers via C-x b.

To activate:

M-x iswitchb-mode

Or add to your .emacs file:

(iswitchb-mode)

It is similar to Ido mode for buffer switching but a bit more lightweight.

Also, if you want a more customizable listing of your buffers then use M-x bs-show as an alternative to C-x C-b. In that buffer type ? to get a list of actions you can perform.

Ray Vega
+2  A: 

As always, there are many ways to help you with this; it depends a bit on personal preference what works best, here are some links with explanations:

  • ibuffer; which is an updated buffer menu (C-x C-b)
  • ido, which let's you have more powerful autocompletion to switch through buffers. It's a kind-of 'better iswitchb'.

These two are enough for me; but you may also be interested in the tabbar-mode, which gives you rudimentary tabs (like firefox has them).

djcb
Thanks for the emacs-fu link.
Wilduck