Instant scratch buffer for current mode

When emacs launches it will typically show the *scratch* buffer, which is in lisp mode by default, and is useful for making rough notes and evaluating bits of lisp code.

You can chose the default mode of the scratch buffer by setting the variable initial-major-mode as described on emacs redux. So if you want it to be in text mode, use

(setq initial-major-mode 'text-mode)

Today I wanted a scratch buffer in org-mode for some rough work with a table. I could have switched to the scratch buffer and used M-x org-mode to switch the major mode of the scratch buffer, but I found a nicer solution. I installed the package scratch and added the following to my emacs config file:

;; multiple scratch buffers                                               ;;
;; uses package "scratch"
(autoload 'scratch "scratch" nil t)

With this package, if you use M-x scratch it will launch a scratch buffer for the current mode. So in my org-mode buffer, using this command gave me a scratch buffer called *org* where I could put my temporary org scribblings.


Using server and clients for instantaneous startup

If you use an advanced emacs configuration like my recommended set up prelude, then the startup time for emacs can be several seconds or longer. To avoid this, and to get other benefits I strongly suggest running emacs with a server and clients.

The way this works is that the first time you start emacs, you start a server, and then any other emacs sessions you start just connect to that server. This means they start instantly, and also have the same buffer list as every other emacs window (frame in emacs terminology) that you have open.

To do this we need to make a simple shell script

#! /bin/tcsh -f

# start emacs server - replace with path to your emacs
/Applications/Emacs.app/Contents/MacOS/Emacs --daemon

# start emacs client - replace with path to your emacsclient
# this is usually in the same place as your emacs executable
/Applications/Emacs.app/Contents/MacOS/bin-x86_64-10_9/emacsclient -c $argv

and save it somewhere sensible like ~/scripts/emacs_daemon.csh

Then set up an alias for your emacs command in your ~/.cshrc or similar:

# alias to start emacs in client mode
# starts server if one is not already started
# replace with path to your emacsclient
alias em '/Applications/Emacs.app/Contents/MacOS/bin-x86_64-10_9/emacsclient --alternate-editor "~/scripts/emacs_daemon.csh" -c'

Now you can use the command em to start emacs every time. The first time it will take as long as usual, but after that it will be instant. Running em file1 file2 or similar will open a new emacs window with the named files as you would expect.

One last point to note is that since your emacs windows are now clients of an emacs server that is running in the background, closing the window will not stop the server. If you want to close the entire emacs session and stop the server (e.g. so you can start a fresh emacs session after changing your config file), you can use M-x save-buffers-kill-emacs, which I have bound to C-x c for convenience. To do this, add the following to your emacs config file

;; set shortcut to kill whole emacs session
(global-set-key (kbd "C-x c") 'save-buffers-kill-emacs)