Suggestions on Emacs's Scratch Buffer
Xah Lee, 2008-09, 2009-08-27
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 a 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 so that the new buffer default to emacs-lisp-mode. (for example, thru emacs's command “customize”.)
- 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 Ergoemacs Keybindings. (see below)
Draft Implementation
The above suggestion is implemented in
ErgoEmacs Keybinding.
Some detail about the implementation:
- A new command “create-new-buffer”. It will create a new empty buffer named “untitled”. There is a menu “File‣New Buffer” to invoke this command, and a shortcut 【Ctrl+n】.
- A new command “close-current-buffer”. It will close the current buffer, and ask user whether to save if not already saved, even if it is a buffer not associated with a file. A menu “File‣Close” and shortcut 【Ctrl+w】 invokes this command.
- The existing “File‣Close” menu command in emacs 22.2 and 23.1 invokes kill-this-buffer is now replaced by close-current-buffer. The kill-this-buffer has a few 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). (3) It is different from the standard emacs command kill-buffer (Ctrl+x k). The kill-buffer command 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. It is part of ergoemacs, used by a hundred or more people. 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:
Newsgroups: gnu.emacs.help
From: Xah Lee.
Date: Tue, 16 Sep 2008 13:57:59 -0700 (PDT)
Subject: Re: How to get rid of *GNU Emacs* buffer on start-up?
Source
Was this page useful? If so, please do donate $3, thank you
donors!
2008-09
© 2008 by Xah Lee.