You interact with buffers when using Emacs. A buffer is best described as a container for text or data. It is an Emacs unit that lets you view, edit, and manipulate the data or text. Every open file in your Emacs is associated with a buffer. Even when working with Lisp expressions, they, too, are considered as non-file data and still fall under buffers which are represented as scratch buffers.
With Emacs, you can have the current buffer, the container that you are currently using, and the other containers that are opened for other activities. Besides, you can simultaneously work with several buffers by switching between them when needed. So, how can you close a buffer that you no longer need? Let’s find out!
Three Ways of Closing the Emacs Buffers
There are three common ways of closing the buffers in Emacs. Each method is applicable based on what your goal is. For instance, you may want to close the active or specific buffer. Whatever your goal is, any of the other three methods that are presented in this post will work for you.
Before discussing how to close the Emacs buffers, the first step is to check your current active buffers. For that, use the “C-x c-b” command. The “C’ represents the “Ctrl” keyboard key. So, press “Ctrl + x” and release. Then, press “Ctrl + b” and release to show the available open buffers for your case.
Having seen how you can list the open buffers, let’s see the three methods of closing the Emacs buffers.
Method 1: Killing an Active Buffer
The easiest way to close the active buffer is using the “kill-buffer” command. When executed, the command will prompt you to close the default buffer and show its name to confirm that it’s the correct buffer that you want to close.
Press the “M-x” command and type “kill-buffer”. You will get a window like the one in the following which shows the current default buffer for your case. To close it, press the “Enter” key.
You can then list the available buffers to confirm that you successfully closed the default/current buffer.
For our case, we closed the “Ibuffer” buffer. The following list confirms that we no longer have the buffer open.
Method 2: Using the Ibuffer Option
The “ibuffer” command lets you interactively close the Emacs buffer. This option lets you specify one or more buffers that you want to close. The interactive mode gives you an access to different options to mark the target buffer and proceed to close them.
Start by pressing the “M-x” command and type “ibuffer”. The interactive window will open and appear like the one in the following. It shows the available buffers and other related information.
Use the arrow keys to scroll through the list. When you find the buffer that you want to close, press “m” to mark it. You will notice the color change on the highlighted buffer which hints that it’s ready for deletion. You can mark several buffers, but we selected only one for this example.
To close the selected buffer, press “D”. You will get prompted to verify that you want to close the selected buffer. Press “y” for the process to complete.
You will immediately notice that the buffer will be closed, and the changes will reflect on the open window. We now have two available buffers for our case which confirms that we managed to close the previously highlighted buffer.
Method 3: Closing a Specific Buffer
With the “kill-buffer” command, you can specify the buffer name that you want to close instead of closing the default buffer. Let’s first list the available buffers.
Next, press the “M-x” command and type “kill-buffer”. After pressing the “Enter” key, type the name of the buffer that you want to close. We closed the “Help” buffer for our case.
If we recheck the available buffers, we can confirm that we managed to close the “Help” buffer.
That’s the third method of closing the Emacs buffers.
Conclusion
When you have numerous open buffers in Emacs, closing them is recommended, especially for the no-longer-needed buffers. We discussed the three methods that you can use. First, we learned how to close the default buffer. Next, we learned how to close the buffers interactively. Finally, we learned how to close a specific buffer. Try the methods and find the ideal one for your situation.