Pages

Monday, June 8, 2026

Emacs and the Numeric Keypad

If you have a numeric keypad1, try this in the *scratch* buffer: enable numlock and press the zero on the numeric keyboard (Num-0). Then press the zero located between the alpha keys and the function keys (0). Okay, you see 00, no big deal.

Why do I bring this up? Emacs interprets Num-0 keypress as <kp-0> while it interprets 0 as 0. This means that you could bind Num-0 to a function without affecting the other 0. You could configure Num-0 to insert "zero" by entering and evaluating this in the *scratch* buffer:

(keymap-local-set "<kp-0>" #'(lambda () (interactive) (insert "zero")))

That's a trivial example, of course. But it implies that you can have ten more keys to play with. Similarly, the arithmetic operator keys on the numeric keyboard differ from the "regular ones" that are grouped with the alpha keys. They can be bound to functions, referenced as <kp-add>, <kp-subtract>, <kp-multiply>, <kp-divide>. But these also accept the standard C- M- S- modifier keys, which gives you twelve more possibilities. This is true also for the Insert key <kp-insert> and the Delete key <kp-delete> that double as Num-0 and Num-., respectively.

I have a "Calc" key above the numeric keypad. Unfortunately I can't seem to use it for anything in Emacs -- it opens the OS's calc.exe program regardless of what modifiers I use with it. This is a shame because it would be the ideal mapping for M-x calc. Perhaps this can be altered in BIOS.

But I do use my Win key as a modifier (sometimes). And I've heard that the Caps Lock key can be repurposed. These are topics for other posts.


https://en.wikipedia.org/wiki/Numeric_keypad

Saturday, June 6, 2026

Follow-on to exeln

Sebastián Monía1 offered some alternatives to the exeln function that I posted on June 4.2

First, he pointed out that there's no need to bind asynch with the let function. Instead he suggested that I use (when arg "&") to append the ampersand when there's a prefix argument. Thus, shell-command can be called this way:

(shell-command (concat
                (buffer-substring
                 (line-beginning-position) (line-end-position))
                (when arg "&")))

Second, call async-shell-command rather than shell-command with the "&" as suggested in shell-command's doc string, which says, "You can also use async-shell-command that automatically adds '&'."3

Third, use thing-at-point from the built-in thingatpt.el library to return the line at point rather than calling buffer-substring.

(defun exeln (arg)
  "Execute current line as a shell command.
With prefix ARG, run asynchronously."
  (interactive "P")
  (funcall (if arg
               #'async-shell-command
             #'shell-command)
           (thing-at-point 'line t)))

And although he didn't mention it explicitly, he reworded the doc string -- it blends in seamlessly with the native functions' doc strings.

I set out to contribute to the Emacs community, but I'm getting so much more out of it than I expected! Thank you again, Sebastián Monía!


1 https://site.sebasmonia.com/
2 exeln-execute-line.html
3 GNU Emacs online shell-command help

Thursday, June 4, 2026

exeln: EXEcute LiNe

The following exeln function passes the current line to the system shell, which executes it, thus EXEcuting LiNe.

On Windows x86, my GNU Emacs has shell-file-name set to cmdproxy.exe, which is provided by x86_64-w64-mingw32, with which Emacs was compiled.

shell is invoked synchronously by default; a prefix argument can change this to asynchronous.

In the video below, point is on a line of text that would print the contents of hello_whirled.txt when entered at the Windows command prompt (or DOS prompt). I press C-c x, which is bound to exeln. The command is executed and the output is shown in *Shell Command Output*, the default buffer used for synchronous output from shell-command.

(defun exeln (arg)
    "Execute the current line as a shell command.
With prefix arg, operate asynchronously, same as calling
(shell-command COMMAND) where COMMAND ends in '&'.
Otherwise, execute synchronously."
  (interactive "P")
  (let (asynch)
    (if arg
        (setq asynch "&"))
    (shell-command (concat
                    (buffer-substring
                     (line-beginning-position) (line-end-position))
                    asynch))))

2026-06-04 Add demo video

Tuesday, June 2, 2026

Change Emacs Cursor to Indicate Overwrite Mode

The text editor I'd used for 20 years on an MS-DOS platform would change the shape of the cursor to distinguish between insert mode and overwrite mode. A filled rectangle (or box, in Emacs terms) indicated overwrite mode; an underscore (hbar), insert mode. It's a nice way to make overwrite mode obvious.

The video starts out with Emacs in Insert Mode. Then Overwrite Mode is activated, which changes cursor-type from hbar to box. The word "Overwrite" replaces "Insert" to demonstrate the effect.

Incidentally, you might enjoy listening to some sound while playing back the video. One of my favorites is "Forest Voice" from https://mynoise.net, by Dr. Stéphane Pigeon.

Anyway, here's a function to implement that behavior in Emacs:

(defun zeit-change-cursor-shape ()
  "Set cursor shape depending on overwrite mode.
When overwrite-mode is nil (off), set cursor-type to hbar.
Otherwise, set it to box."
  (interactive)
  (if (not overwrite-mode)
      (setq cursor-type 'hbar)
    (setq cursor-type 'box)))

(add-hook 'overwrite-mode-hook 'zeit-change-cursor-shape)

2026-06-02 Add add-hook  as suggested by @Howitzer105mm@pdx.social

2026-06-02 Move hook outside function

Wednesday, May 27, 2026

Emacs view- Commands

A kind reader suggested I try the view-lossage command to display a list of recent keystrokes. This would be helpful for those times when I press a key accidentally and don't know what I did (as described in "zap-to-char M-z").

The output resembles what you'd get from edit-kbd-macro (assuming that a keyboard macro had been defined). This provides an alternate way to define a keyboard macro -- just select the desired portion of the output and invoke read-kbd-macro.  At this point you can "play back" those keystrokes with C-x e.

After I understood view-lossage,  I skimmed through the list of commands that start with view- (by doing M-x view- TAB). The commands view-buffer,  view-file(and variants that view in another buffer, window, frame) provide a safe (read-only) way to examine the contents of a buffer or file.

Entering M-x view-file-other-frame .emacs allows me to display my init file in a new Emacs frame and close it (by pressing "q") when I'm done with it. And then I don't have to worry about changing it inadvertently.

Monday, May 25, 2026

Emacs and Wordle

Did you know that "EMACS" is a valid Wordle word?  Check it out:

Sunday, May 24, 2026

zap-to-char M-z

Sometimes I discover an Emacs feature after an accidental keystroke. If I can remember the keystroke, I'll invoke the keystroke help (C-h k) to learn about it. Or sometimes the prompt in the mode line will suggest the function's name.

Today, the keystroke was M-z , which is bound to the function zap-to-char.

It seems to be ideal if, like me, you doze briefly while typing a senteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

Oops, there I go again!

To fix that I can just press M-- M-z t to delete all the trailing "e" characters.

Note that M-- is invoked by holding down the Alt key while pressing hyphen. It defines a negative prefix argument, which instructs zap-to-char to "go backward," deleting all characters from point to the specified character ("t"). I'm not sure how or why you'd want the default "forward" behavior.

What accidental keystroke have you discovered recently?