0
votes

I have a simple set of emacs lisp lines that I use to insert my initials and a time/datestamp, but this has stopped working in some modes (e.g., python-mode). In these modes it throws the error comint-send-string: Buffer *spam* has no process, where 'spam' is the buffer I'm trying to add my timestamp to.

The lisp lines are:

(defun msl-insert-datetime () (interactive) (insert (format-time-string "%Y-%m-%d %H:%M ABC: "))) (global-set-key [(control c)(control d)] 'msl-insert-datetime)

This works fine in buffers of various modes (text mode, lisp mode, etc.), inserting something like 2020-05-20 21:44 ABC:. But in Python mode (where I do much of my programming) I get the message above instead.

Any suggestions would be most welcome! I'm running Emacs 26.3 (build 2) on Ubuntu 20.04.

1

1 Answers

1
votes

C-cC-d is probably bound in python-mode (presumably to something which wants to talk to an inferior process).

(global-set-key [(control c)(control d)] 'msl-insert-datetime)

This binds the sequence in the global key map -- which is the lowest-priority keymap -- and you've chosen a sequence which is reserved for major modes.

Sequences consisting of ‘C-c’ followed by a control character or a digit are reserved for major modes.

-- C-hig (elisp)Key Binding Conventions

As such, it's not surprising that some major modes are binding that in their (higher-priority) major mode keymaps.

There are certain other sequences you can more safely use in the global map, without worrying about them being used by other keymaps:

Sequences consisting of ‘C-c’ and a letter (either upper or lower case) are reserved for users. Function keys <F5> through <F9> without modifier keys are also reserved for users to define.

Note that you can use such sequences as prefix bindings, so lots of custom commands might hang off of C-cd (for example).

If you have Super and/or Hyper modifier keys on your keyboard, they are typically also unused by Emacs (at least by default).

I also remove the default C-z binding and use it as another prefix for custom bindings.

Lastly, I highly recommend reading https://www.masteringemacs.org/article/mastering-key-bindings-emacs to gain an understanding of keymaps and their priorities.