0

A bit of a silly question here, but you know emacs is a lot of library. Is anyone familiar with extracting buffers by number from the buffers list?

In javascript or php this kind of thing is easy, just put an index next to the array. In emacs however, the lists have sometimes objects and sometimes strings, and getting the strings out of the objects seems sometimes a bit esoteric, given the sprawling and somewhat fragmented nature of the knowledge base.

Of course, this is really easy and I'm missing something obvious, but nonetheless, help me out if you know the answer!

To reiterate, I'd like to dolist the buffers list and extract a matching name if there or return the second, third, or fourth buffer from the list, as the case may be.

1
  • All of these things are explained in the Emacs Lisp manual which comes with your emacs: do C-h i g (elisp) RET. If you don't know how Lisp works, you might want to start with the Introdcution to Emacs Lisp: do C-h i g (eintr) RET. What you want to do can be done with the functions nth, buffer-list and buffer-file-name. Check the manuals for details. – NickD Mar 9 at 6:52
1

The function buffer-list returns a list of buffer objects. This list isn't special; it's just an ordinary list. As such you can access an element of the list with nth, which is documented in chapter 5.3 Accessing Elements of Lists of the Emacs Lisp Manual. (You can also read this manual without leaving Emacs with C-h i.)

Like this:

(nth 5 (buffer-list))

You can access the name of a buffer with the accessor method buffer-name:

(buffer-name (nth 5 (buffer-list)))

Therefore you can get a list of the names of all of the buffers with mapcar:

(mapcar #'buffer-name (buffer-list))

If you want more information about any of these functions, use C-h f. This prompts you for a function name (the default for the prompt will be the name of the function at point, if any), and then tells you everything it knows about it. There will be a description, as well a link to both the manual and the source code.

1
  • Thanks, that's exactly what worked and exactly the source I was reading from while you were writing this (though I searched google). In the beginning, you want to get away with a few customizations, but if you keep at it, you're better off taking emacs 401 :). Also, the list may not be special, but nothing works without it, ;). – F. Certainly. Mar 9 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.