Elisp: Buffer Functions
What is a Buffer
A buffer in emacs is an area that displays text, for reading or modification. e.g. content of file, messages, program output, dir listing, chat mode, shell, prompt, etc.
For emacs lisp programers, a buffer is a special emacs lisp data structure that holds text, and allows you to search or edit it in a efficient way, programatically. You can think of it as a enhanced string data structure for storing large text for modification.
Importance of Buffer in Emacs
Buffer is critically important in emacs.
In most programing languages, you read a file to a string. In emacs, it's done by creating a buffer, and put file content in it. Everything you do with file, is done via buffer.
Basic Buffer Concepts
- Buffer is a datatype in elisp. A buffer is a lisp object. It holds text.
- Buffer Name. Each buffer has a name (a string). Buffer names are unique. Many functions accept a buffer argument that can be a buffer name or buffer object.
- Current Buffer. There is always one buffer designated as the current buffer. Most functions that work on text such as insert / delete work on the current buffer.
- File Buffer (aka Visiting Buffer). A buffer that is a file. It contains the file's content. A file path is associated with the buffer.
Create Buffer
with-temp-buffer-
(with-temp-buffer &rest BODY)Create a temporary buffer, and evaluate BODY, return the last expression. (then delete the temp buffer)
💡 TIP: when you have a big text and you want to do a lot modification on it, best is to create a temp buffer and edit. Use
buffer-substringto turn back into a string. Also useful isinsert-file-contents.;; use a temp buffer to manipulate string (setq xx "big text") (with-temp-buffer (insert xx) ;; manipulate the string here ;; return buffer content as string (buffer-string))
generate-new-buffer-
(generate-new-buffer NAME)- Create a new buffer, with a name based on NAME. Buffer name is created by
generate-new-buffer-name(it basically return a new buffer name with number appended if the name already exists). - If buffer name start with a space, undo is disabled, and the buffer is not listed.
- Return the buffer (does not make it current).
;; create a new buffer, save it to a var, so later you can switch to it or kill it (setq newBuf (generate-new-buffer " xyz")) - Create a new buffer, with a name based on NAME. Buffer name is created by
get-buffer-create-
(get-buffer-create BUFFER-OR-NAME)- Create or just return a existing buffer. If the buffer exists, it's returned, Else, new is created.
- Does not make it current.
- BUFFER-OR-NAME can be a string or buffer.
- If BUFFER-OR-NAME is a string and start with a space, undo is disabled.
;; create new buffer, without undo info. make sure the string passed is unique and has space in front (setq newBuf (get-buffer-create " xyz"))
Get (Visiting) Buffer's File Path
buffer-file-name-
(buffer-file-name &optional BUFFER)Return the full path of the file, or
nilif not a file. - buffer-file-name
-
A Buffer Local Variable. Value is the buffer's file path. If the buffer is not associated with a file, value is
nil.💡 TIP: use this variable if you want the file path of current buffer. If you have another buffer in mind, use the function
buffer-file-name.
Get Buffer Name, or Buffer Object
Each buffer is identified by the buffer object itself, or by a name (string).
current-buffer-
(current-buffer)return the current buffer object.
buffer-name-
(buffer-name &optional BUFFER)Return the name of buffer. The name of current buffer by default.
get-buffer-
(get-buffer BUFFER-OR-NAME)Return the buffer object.
Make Buffer Current
- when a buffer is current, editing functions operate on it.
- current buffer does not imply visible.
set-buffer-
(set-buffer BUFFER-OR-NAME)Make a buffer current. (but does not make it visible.) Return that buffer object.
with-current-buffer-
(with-current-buffer BUFFER-OR-NAME &rest BODY)Temporarily make a buffer current.
💡 TIP: Most of the time, you want to use this. Because it takes care of switching back to the original buffer when the function is done.
;; make xbuf current temporarily (with-current-buffer xbuf ;; code to edit text here )
save-current-buffer-
(save-current-buffer &rest BODY)Execute BODY, then restore the current buffer that is before this function call.
💡 TIP: use this function when you want to switch buffer in your code and restore the user's current buffer. Do not manually record current buffer and switch back to it at end of code. Because that wont restore when there are error conditions.
Print to a Buffer
with-output-to-temp-buffer
Switch to Buffer
switch-to-buffer-
(switch-to-buffer BUFFER-OR-NAME &optional NORECORD FORCE-SAME-WINDOW)Make a buffer visible to user, and make it the current buffer. The buffer fills the current emacs frame.
💡 TIP: use this when you want the user to see the buffer, as if the user switched to it.
Show Buffer
There are few important concepts when showing a buffer.
- Make a buffer visible.
- Make a buffer current or not.
- Is the buffer shown in a new split pane (a new emacs window), or fill the emacs frame.
pop-to-buffer-
(pop-to-buffer BUFFER-OR-NAME &optional ACTION NORECORD)Make a buffer visible to user, and make it the current buffer. (typically, this is shown in a new split pane.)
display-buffer-
(display-buffer BUFFER-OR-NAME &optional ACTION FRAME)Make a buffer visible to user, but does not make it current. (typically, this is shown in a new split pane.)
Save Buffer to File
use
save-buffer,
but better is
write-region
Kill (Delete) Buffer
kill-buffer-
(kill-buffer &optional BUFFER-OR-NAME)Delete a buffer. Default to the current buffer.
🛑 WARNING: if the buffer is not a saved file buffer, all text in the buffer are gone. If you want to keep the text, use
buffer-substringto extract the string first, orwrite-regionto save to a file. 〔see Elisp: Write File〕
More Buffer Functions
bufferp
rename-buffergenerate-new-buffer-name
buffer file name related
- buffer-file-truename
- buffer-file-number
File buffers related
get-file-bufferfind-buffer-visitingset-visited-file-name
;; when renaming file, also rename buffer (when (get-file-buffer xfile) (with-current-buffer (get-file-buffer xfile) (set-visited-file-name xnewname nil t)))
modified
buffer-modified-pset-buffer-modified-prestore-buffer-modified-pnot-modifiedbuffer-modified-tickbuffer-chars-modified-tickwith-silent-modifications
verify-visited-file-modtimeclear-visited-file-modtimevisited-file-modtimeset-visited-file-modtime
read only
- buffer-read-only
- inhibit-read-only
read-only-modebarf-if-buffer-read-only
ask-user-about-supersession-threatbuffer-listother-bufferlast-bufferbury-bufferunbury-buffer- buffer-list-update-hook
generate-new-buffer- kill-buffer-query-functions
- kill-buffer-hook
- buffer-offer-save
- buffer-save-without-query
buffer-live-pmake-indirect-bufferclone-indirect-bufferbuffer-base-bufferbuffer-swap-textgap-positiongap-size
- list-buffers-directory
Reference
Elisp, File, Buffer
- Elisp: Buffer Functions
- Elisp: Read File
- Elisp: Read File Content as String or Lines
- Elisp: Write File
- Elisp: Open File, Read, Possibly Write
- Elisp: File and Directory Functions
- Elisp: Get File Info
- Elisp: File Path Functions
- Elisp: Walk Directory, List Files
- Elisp: Walk Directory by Depth 📜
- Elisp: Get Dired Marked Files