51

I would like to write a function which takes action if a give buffer name already exists. For example:

(if (buffer-exists "my-buffer-name")
    ; do something
 )

Does elisp have a function that will check the for the existence of a buffer similar to how my made up "buffer-exists" function does?

Thanks

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
oneself
  • 38,641
  • 34
  • 96
  • 120

4 Answers4

68

From the documentation:

(get-buffer name)

Return the buffer named name (a string).
If there is no live buffer named name, return nil.
name may also be a buffer; if so, the value is that buffer.

(get-buffer-create name)

Return the buffer named name, or create such a buffer and return it.
A new buffer is created if there is no live buffer named name.
If name starts with a space, the new buffer does not keep undo information.
If name is a buffer instead of a string, then it is the value returned.
The value is never nil.
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
  • 1
    Shouldn't get-buffer be used as an argument to bufferp to verify it is indeed a buffer? (if (bufferp (get-buffer "my-buffer-name") ;do something ) – PuercoPop Sep 20 '12 at 17:29
  • 4
    No need to use `bufferp`. Since `get-buffer` returns either `nil` or a buffer, you can just test it directly: `(let ((b (get-buffer "foo"))) (if b ...))` – Gareth Rees Sep 20 '12 at 20:16
8

This is what I did:

(when (get-buffer "*scratch*")
  (kill-buffer "*scratch*"))

This checks for the buffer scratch. If there's such a thing, kill it. If not, do nothing at all.

Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
Ole
  • 487
  • 7
  • 20
6

not sure about version this predicate appeared, but now Emacs has buffer-live-p:

buffer-live-p is a built-in function in `buffer.c'.

(buffer-live-p OBJECT)

Return non-nil if OBJECT is a buffer which has not been killed.
Value is nil if OBJECT is not a buffer or if it has been killed.
zarkone
  • 1,335
  • 10
  • 16
  • This does not answer the question. You have to input a buffer-object as `OBJECT` and not a buffer name. If you pass a name of a living buffer as `OBJECT` you get the return value `nil`. Maybe, it is worth a comment though. – Tobias Aug 11 '15 at 10:07
  • this make sence. May be I should create separate question for this? i just searched for `buffer-live-p` functionality and found only this approach.. – zarkone Aug 11 '15 at 10:49
  • 2
    This helped me out considerably since `get-buffer` will return `#` rather than `nil` if the buffer once existed! – Alexander Griffith Dec 10 '16 at 20:20
  • @AlexanderGriffith I could not reproduce this. After **(kill-buffer "*scratch*")** and then **(get-buffer "*scratch*")** returns nil. – Talespin_Kit May 24 '21 at 08:33
  • @Talespin_Kit If you run it all in one script `get-buffer` will return `#`. For example, try running `(let ((buffer (get-buffer-create "scratch"))) (kill-buffer buffer) (cons (buffer-live-p buffer) (get-buffer buffer)))` – Alexander Griffith Jun 08 '21 at 15:47
5

If you'd like to define your hypothetical function as above, this works:

(defun buffer-exists (bufname)   (not (eq nil (get-buffer bufname))))

I use this to automatically close the *scratch* buffer on startup, so I don't have to cycle through it in my list of buffers, as follows:

(defun buffer-exists (bufname) (not (eq nil (get-buffer bufname))))
(if (buffer-exists "*scratch*")  (kill-buffer "*scratch*"))
Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
user249106
  • 51
  • 1
  • 2
  • 5
    Note that `(eq nil )` is the same as `(null )` or `(not )`, so `(not (eq nil (get-buffer bufname)))` is `(not (not (get-buffer bufname)))`, so you can drop the double negation and just use `(get-buffer bufname)`. At which point you can redefine `buffer-exists` as an alias for `get-buffer`. – Stefan Nov 21 '12 at 17:50
  • @Stefan (nitpick) The given `buffer-exists` is not exactly the same as `get-buffer` because it will return either `t` or `nil`, but never the actual buffer. – Ken Wayne VanderLinde Mar 17 '17 at 05:54