autoscratch - solve the *scratch* buffer problem

I use emacs for more than 20 years now. I love it, I am addicted, I can't even do anything productive without it. However, there was one problem left: the *scratch* buffer. This is a non-file buffer, which always exists always in emacs. By default it has emacs-lisp-mode enabled and you can use it to hack short elisp sexps for testing. In fact I use it for exactly this purpose.

But sometimes I hate it as well! I get a phone call and need to take a note quickly, *scratch* is already open, so I use this. But the mode doesn't really fit. So I switch to text-mode and then enter the notes. I did it this way almost since day one of my emacs usage.

A couple of months ago I came up with a "solution", I just create a *text* buffer on startup with text-mode already enabled in my .emacs. But I still need to switch to it everytime I need it. Still too annoying!

So now, here's my final solution, which tries to fix this mess once and for all: autoscratch.

This major mode is really simple. Basically it consits of an alist with instructions on how to automatically switch the *scratch* buffer mode. Enter an "(" and it switches to emacs-lisp-mode, enter "*" and it switches to org-mode, enter some letter a-z, and it switches to text-mode. You get the idea.

It also solves another problem I (and many other users according to google) have: once I set the *scratch* buffer mode to, say, *text-mode* with some text in there, I don't have an elisp *scratch* buffer left anymore. I'd need another one, but how to create a second *scratch* buffer? Obviously you'll need to rename the current text-mode buffer first and then create a new empty buffer. The emacs wiki contains lots of suggestions for this kind of solution.

No more of this! Autoscratch can just "fork" the buffer, if enabled (set autoscratch-fork-after-trigger to t which is the default). Here's how it works: type a "(" into the empty autoscratch-enabled *scratch* buffer. Autoscratch renames this buffer to *emacs-lisp-scratch*, enables emacs-lisp-mode and creates a new *scratch* buffer in the background. So, if you need some *scratch* space, it'll be there for you waiting bravely for input.

Here's the default trigger list telling autoscratch how to switch modes:

'(("[(;]"         . (emacs-lisp-mode))
  ("#"            . (autoscratch-select
                     '(("perl"   . (cperl-mode))
                       ("ruby"   . (ruby-mode))
                       ("python" . (python-mode))
                       ("conf"   . (conf-unix-mode))
                       ("shell"  . (shell-script-mode)))))
  ("[-a-zA-Z0-9]" . (text-mode))
  ("/"            . (c-mode))
  ("*"            . (progn (insert " ") (org-mode)))
  ("."            . (fundamental-mode)))

Now if you configure autoscratch like this:

(require 'autoscratch-mode)
(setq initial-major-mode 'autoscratch-mode)
(setq initial-scratch-message "")
(setq inhibit-startup-screen t)

then emacs will start with an empty *scratch* buffer as always, but with autoscratch mode enabled. Type in a "(" and emacs-lisp-mode will be started, the *scratch* buffer will be renamed and a new *scratch* created in the background. Or, type in a "#" and you'll be asked what to switch (the autoscratch-select function does this). You can configure almost anything here.

Oh, and just in case you need to manually create a new scratch buffer, just execute "M-x autoscratch-buffer".

Update 2018-08-02:

There was an issue with autoscratch in combination with magit, the original scratch buffer has the same problem as well. The buffer did not alter the default-directory variable. It is buffer-local and inherits the contents of the same global variable, if set to something. So, if you have some file open in a buffer and start a new autoscratch buffer from there, default-directory of this buffer will then be set to the directory of the file visited in the buffer which was last active. Now, if you close all files, except the scratch buffer, and start magit-status AND if the directory is inside of a git repo, then magit will open this repo instead of asking you which repo to open. This is not the behavior I expect from a scratch buffer, so I modified autoscratch-mode to reset the default-directory (if enabled) to $HOME.

Update 2017-07-17:

You can complete this scratch buffer setup with the great persistent-scratch.el mode. Here's my config for it:

(require 'persistent-scratch)
(setq persistent-scratch-save-file
    (expand-file-name "scratches.el" user-init-dir))

(defun tvd-autoscratch-p ()
  "Return non-nil if the current buffer is a scratch buffer"
  (string-match "scratch*" (buffer-name)))

(setq persistent-scratch-scratch-buffer-p-function 'tvd-autoscratch-p)

With this setup, scratch buffers will be saved on exit and restored on startup, so you never loose any cool snippet or note you have in a scratch buffer.

17 July 2017 | #emacs


csh sensible defaults

On FreeBSD (and possibly others) the csh is the default login shell for root or default users. I always try to avoid it and use bash. However, sometimes it's not possible or dangerous to install bash (inside a jail for example).

So I decided to check if it is possible to configure csh in a way to make it less annoying. I read the docs, tried many different options and here is the result.

That's not much but helps digging around a system using csh. With this config you'll get:

  • completion similar to bash
  • right/lift jumping by words using CTRL-left and CTRL-right
  • CTRL-up goto beginning of line, CTRL-down goto end of line
  • history search using CTRL-R
  • some sensible aliases
  • a meaningful prompt
# sensible defaults for CSH 
alias  h     history 25
alias  j     jobs -l
alias  la    ls -a
alias  lf    ls -FA
alias  ll    ls -lA
alias  l     ls -laF
alias  lt    ls -ltr
alias  md    mkdir -p
alias  ..    cd ..
alias  ...   cd ../..
alias  ....  cd ../../../
alias  vi    nvi
alias  vim   nvi

# A righteous umask
umask 22 # environment config
set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin $HOME/bin) setenv EDITOR vi setenv PAGER more setenv BLOCKSIZE K # if interactive, configure further
if ($?prompt) then # interactive prompt
set prompt = "%N@%m:%~ %# " set promptchars = "%#" # history config
set history = 5000 set savehist = 5000 # complete case-insensitive
set complete = enhanced # print possible completions if more than 1 match
set autolist = ambiguous # enable filename completion
set filec # shut up
set nobeep # enable redirect protections
set noclobber # forbid rm *
set rmstar # mail?
set mail = (/var/mail/$USER) # convenience bindkeys, similar to emacs or inputrc
bindkey "^[^[[D" backward-word # ALT-RIGHT
bindkey "^[^[[C" forward-word # ALT-UP + HOME
bindkey "^[^[[A" beginning-of-line # ALT-DOWN + POS1
bindkey "^[^[[B" end-of-line # CTRL-R (like bash), then type
bindkey "^R" i-search-back # SHIFT-TAB cycles through possible completions, let go if good
bindkey "^[[Z" complete-word-fwd endif

13 July 2017 | #source


Render current HTML buffer with EWW

From time to time I end up with raw HTML content in an emacs buffer and want to know how it looks or just want to read it properly. Until now either I opened firefox to read that file or I opened it with eww-open-file. The problem is, that sometimes there is no file, the HTML buffer is just the output of some generator or something. In such cases I needed to save the buffer as a file and then re-open it with EWW. Very annoying.

With this simple function the nightmare is over. I just use the EWW internal render function to do the job. That way I can view any HTML in emacs without even saving it to disk.

(require 'eww)

(defun eww-render-current-buffer ()
  "Render HTML in the current buffer with EWW"
  (eww-display-html 'utf8 (buffer-name)))

(global-set-key (kbd "<C-c C-e C-w C-w>") 'eww-render-current-buffer)

Now when I'm inside a HTML buffer, I can just render it and take a look with EWW. Very handy!

08 June 2017 | #emacs



Finlay is seit Dezember 2016 bei uns. Er kommt aus einer Tötungsstation im Ausland. Wir trainieren viel, Schmusen viel und sind happy mit ihm. Hier ein paar erste Bilder:

2017-06-12 - Ein Küschen für die (grosse!) Dame:

2017-06-12 - So gut muss man es haben:

2017-05-14 - Training, Training, Training:

2017-05-14 - Cheeees!:

2017-05-14 - Hübscher Bub oder?:

2017-05-14 - Geschmückt, aber Müüüüüde:

14 May 2017 | #hund


Cheap RSA Paper Token Holder

Materail needed: Paper (I suggest  photo paper which is thicker), scissors. Build instructions: trim the paper and fold it appropreately. Done. Now you can plase the token on your desk an read the numbers without taking your hands off the keyboard. Relief!

2017-05-13 - The token holder:

13 May 2017 | #networking