Suggestions on Emacs's Scratch Buffer
Xah Lee, 2008-09
In the article The Modernization of Emacs, i suggested that emacs's “*scratch*” buffer be removed. In this article, i give some detail about it.
In the article, i gave the following as primary reasons that scratch buffer should be removed:
- It is not useful by 99% of letter writers. If they wanted a scratch pad, they can open a new document and not save it. This way is familiar to all software users.
- The “*scratch*” “buffer” is primarily designed for elisp programers. (it defaults to lisp-interaction-mode.) Majority of people who use emacs are not lisp coders. For lisp coders, they can easily customize their emacs to have a “*scratch*” “buffer”.
- The “*scratch*” “buffer” is a intrusive idiosyncrasy. It is persistent, cannot be closed (it regenerates). It is foreign to all programers. This idiosyncrasy is the first thing presented to users, and it persists.
Here are few minor reasons:
- There is no easy, intuitive way to create multiple scratch buffers. (it is done by using the switch-to-buffer command (C-x b) and give name that is not one of existing buffers.)
- When the scratch buffer is closed, emacs does not prompt user to save it. This easily causes data loss.
- A scratch pad can be very useful not just for temporary elisp code but for any scratch notes or programing in other languages. (For example, well known programer Stevey Yegg in his popular
Effective Emacs↗ blog, considers creating new temp buffer as a top 10 tip in emacs productivity.) Emacs's “*scratch*” buffer is narrowly geared for elisp editing only, defaulting to emacs-lisp-mode.
- Emacs does not provide a user level function to create a new buffer. It has menu “File‣Open file...” (a wrapper to the find-file command), which immediately prompt user for a full file path. This is annoying. Modern apps's New File command actually just create a new untitled file without prompting, and only when user save it it prompt a file name. If user closes it, it prompts for saving.
Proposed Fix
I propose that emacs should also add a menu command “New buffer”, with the keyboard shortcut “Ctrl+n” (see note on this below). Once called, it should create a scratch buffer titled “untitled”. If one already exists, append numbers such “untitled 2”. Here are the reasons:
- The menu command “New” is a widely adopted standard among apps across Mac, Windows, unixes. It is familiar to all software users.
- The Ctrl+n shortcut for New is standard and familiar to all software users.
- A New Buffer command (where the corresponding elisp command name might be named new-empty-buffer), can completely cover the functionality of emacs's “*scratch*” buffer.
- When users want to have a scratch buffer, he can create it by simply pressing the shortcut, and when he doesn't want it, he can simply close it with a standard keystroke Ctrl+w.
- By adopting the New Buffer and Ctrl+n, users can intuitively create multiple scratch buffers for any purpose.
- The name “untitled” is conventional, far more widely understood, and more general than “scratch”.
- For those who use scratch buffer for elisp coding, she can set a preference (e.g. emacs customize feature) so that the new buffer default to emacs-lisp-mode.
- Adopting the suggestion would fix several problems for those who actually use emacs's “*scratch*” buffer. (1) emacs no longer mysteriously spawn a “*scratch*” when emacs starts, or respawn the “*scratch*” buffer when user tries to close all buffers. (2) Multiple scratch buffers can be created by a keyboard shortcut. (3) Data lose is prevented because closing a scratch buffer will prompt for save.
Note: the proposed keybinding “Ctrl+n” and “Ctrl+w” need not be part of this proposal because emacs already use “Ctrl+n” and “Ctrl+w” for basic cursor movement and cut. However, it could be adapted in conjunction with newly designed
Ergonomic Keybinding. (see below)
Draft Implementation
The above suggestion is experimentally implemented in my
Ergonomic Keyboard Shortcut Layout For Emacs. The following are the elisp files.
Some detail about the implementation:
- create-new-buffer will create a new empty buffer named “untitled”. (this should be added to the menu under “File‣New Buffer” but i haven't done that yet.)
- create-new-buffer has standard keyboard shortcut Ctrl+n.
- elisp command close-current-buffer will close the current buffer, and ask user whether to save, even if it is a buffer not associated with a file.
- close-current-buffer has the standard keyboard shortcut Ctrl+w.
- close-current-buffer should have menu under “File‣Close”, but it is not currently done. The existing “File‣Close” menu command in emacs 22.2 calls kill-this-buffer, which has 2 problems. (1) it doesn't have a shortcut. (2) it doesn't ask users to save a buffer that are not associated with file (in effect, any text in the buffer is irreversibly lost immediately). The standard emacs command used to close a file is kill-buffer (Ctrl+x k). It has a usability problem of prompting user to confirm the kill, even if the file is already saved.
I have been using the above code daily since late 2007, with incremental improvement and bug fixes. i'm sure it can use a lot more polishing for public use. The code is GPL'd, so feel free to grab pieces for your own use or submit into GNU.
PS the orginal thread of this discussion, which turned into a flame feast, is here: http://groups.google.com/group/gnu.emacs.help/browse_frm/thread/bf2fae706a7424f0/9d3b5dd5c1de989c.
2008-09