Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a current buffer exists in Emacs?

Tags:

emacs

elisp

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

like image 962
oneself Avatar asked Feb 25 '09 16:02

oneself


People also ask

What is Emacs buffer?

Buffers in Emacs editing are objects that have distinct names and hold text that can be edited. Buffers appear to Lisp programs as a special data type. You can think of the contents of a buffer as a string that you can extend; insertions and deletions may occur in any part of the buffer.


2 Answers

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. 
like image 111
Gareth Rees Avatar answered Oct 12 '22 02:10

Gareth Rees


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.

like image 35
Ole Avatar answered Oct 12 '22 02:10

Ole