According to the documentation the function get-buffer checks if there is a buffer with a specific name. More precisely

(get-buffer name)

Return the buffer named name (a string). If there is no live buffer named name, return nil. See the related question on stackoverflow.

Is there a similar way to check if there exists a buffer whose name starting with a specific string?

2 Answers 2


Function try-completion tests whether a given string can be completed against a given set of completions (e.g. strings).

Function buffer-list provides the list of buffers.

Function buffer-name returns the name of a given buffer.

(defun matches-a-buffer-name? (name)
  "Return non-nil if NAME matches the name of an existing buffer."
  (try-completion name (mapcar #'buffer-name (buffer-list))))

try-completion also accepts an optional PREDICATE argument, which you can use to impose a particular kind of testing, e.g., a particular kind of matching.

C-h f try-completion tells you, part:

Return common substring of all completions of STRING in COLLECTION. Test each possible completion specified by COLLECTION to see if it begins with STRING. The possible completions may be strings or symbols. Symbols are converted to strings before testing, see symbol-name.

All that match STRING are compared together; the longest initial sequence common to all these matches is the return value. If there is no match at all, the return value is nil. For a unique match which is exact, the return value is t.

  • Finally I decided to accept this answer because it also works for the versions <25.1. But the solution provided by xuchunyang is also very good.
    – Name
    Dec 4, 2016 at 15:08

You can use the buffer-list function to get a list of all buffers:

(buffer-list &optional FRAME)

Return a list of all existing live buffers.
If the optional arg FRAME is a frame, we return the buffer list in the proper order for that frame: the buffers show in FRAME come first, followed by the rest of the buffers.

So for example, one can use this to get a list of buffers whose name is starting with *Minibuf-, or nil if no such buffer exists:

(seq-filter (lambda (buf) (string-prefix-p " *Minibuf-" (buffer-name buf)))
     => (#<buffer  *Minibuf-1*> #<buffer  *Minibuf-0*>)
  • What is seq-filter?
    – Name
    Nov 26, 2016 at 15:53
  • 1
    @Name Search the function name in Emacs Lisp manual to read info about it: gnu.org/software/emacs/manual/html_node/elisp/…. It's provided by seq.el, which is part of Emacs since 25.1. It's also possible to install it as a package from GNU ELPA.
    – xuchunyang
    Nov 26, 2016 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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