Cheap tricks in Emacs: Buffers

« previous entry | next entry »
Dec. 9th, 2008 | 07:27 pm

One nice feature of Emacs, that is as old as Emacs itself, are buffers. Most Emacs users like them because you can have multiple buffers open at once and work on many things at once. And for the record, I'm actually a big fan of the Emacs buffer menu. It looks primitive compared to a tab bar or some such, but it gets things done. But the Emacs buffer list isn't the embarrassing way I use buffers.

I like buffers because they're inexpensive. "Cheap" as in they don't cost a lot to open. I often have arbitrary text with an ephemeral quality that I want to edit. I suppose the scratch buffer is the appropriate place to edit text with a short shelf-life. But I often run `C-x b b' to open a buffer called "b". If I need another I'll often type `C-x b b b' to open another named "bb". Obviously I name the buffers of varying lengths of "b" since "b" is also the last character that's part of the "switch to buffer" command. Since the buffer "b" doesn't exist for switching to, Emacs creates an empty one for you.

Opening a blank buffer opens in the blink of an eye -- faster than it takes to request it. It isn't associated with a file anywhere. Although, you can later, and you may want to if you the file grows to something worth saving. With a blank buffer, you can call an Emacs mode (or minor mode) that is appropriate for the text you're editing. Sometimes a mode like Mail mode or Shell mode are useful. More than likely I'm copying in text from another program to use Emacs's rectangle commands to transform text, or unconditional regular expression replacement or on occasion using keyboard macros.

Opening buffers with no file on disk associated means when you kill the buffer the data is gone -- unless its in the kill-ring. Emacs won't ask if you're sure and can be a bit dangerous, but as I said, use `C-x C-w' to save. This also has the added benefit of using Emacs's always reliable Auto-save feature.

Do folks have other small ways they use Emacs that definitely isn't described in the manual and doesn't necessarily seem correct, but helps them get things done and they wouldn't want to give up?

Link | Leave a comment | Share

Comments {4}

Temporary buffers are cool

date: Dec. 11th, 2008 06:45 am (UTC)

I'm a great fan of ERC. I use it for all my IRC sessions, because I like having the full power of Emacs when I'm typing in a chat window.

Whenever I have to prepare a long-ish amount of text to paste in an IRC window, I fire up a temporary buffer, paste the text in the new buffer, edit, reformat, add borders, quoting characters like "> " and finish all my editing in the temporary buffer. Then I pull the final version of the text in the kill ring, I switch to the destination ERC buffer and yank.

Back when I was an irssi user, I had to open a new screen window, fire up vim or Emacs, and prepare my paste text there. ERC and temporary buffers make this a lot easier. I can't really imagine going back.

Reply | Thread

Aaron S. Hawley

Re: Temporary buffers are cool

from: aaronhawley
date: Dec. 11th, 2008 12:00 pm (UTC)

yeah, i do my quoting and formatting for other email clients in Emacs. You'd think I'd just make a switch to one of the 3+ email clients available in Emacs!

Reply | Parent | Thread


(no subject)

from: ccmmcc
date: Dec. 12th, 2008 01:17 am (UTC)

I open temporary buffers for the same purposes all the time, although mine usually have names like ``fikgh''.

I also routinely use temporary buffers for massaging parts of a larger document (although I recently realized that I could do most of that work by narrowing-to-region first).

And I often pull chunks of LaTeX code out of a document into their own basic document so I can try different ways of formatting things without having to recompile the whole doc.

Finally, I sometimes construct a series of shell commands by doing some amount of manipulation of data on the command line and then open the results in Emacs for some additional tuning. Once the commands are right, I save the buffer, then execute. (Now that I think about it, I bet I could execute the script from within Emacs too....)

Reply | Thread

Aaron S. Hawley


from: aaronhawley
date: Dec. 12th, 2008 01:55 am (UTC)

Yeah, I used to open temporary buffers with "asdf", too, but then settled on "b".

Indeed, narrow-to-region /et al/ is usually more appropriate, but I don't need it as often as using new buffers for text. I usally have text that is from or to another program outside Emacs -- like a Firefox.

Yeah you can execute a shell script with C-c C-x, or you can execute a region of shell script with M-| bash RET

Reply | Parent | Thread