1

Search didn't yield any success. Perhaps I'm using the wrong search terms...

I'd like to insert a single space (just like when you hit SPC) but without changing point. I come up with an example (let | be the cursor). Say you have foobar in a buffer and you placed the cursor in-between.

foo|bar

Then I'd like to hit a key sequence that inserts a space character at point, but leaves point where it is, instead of moving it past the inserted char. Like so:

foo| bar

Perhaps something like insert-space-right. I could easily write some Elisp code that does this for me, but I'd like to find out whether such a thing is indeed already built-in and I'm just unable to find it.

  • 2
    Why should such a command already exist? btw: (defun foo () (interactive) (save-excursion (insert ? ))) – jue Nov 22 '19 at 13:21
  • @jue I'm sorry. I don't know the answer to your question. My question aims more for hard facts. Does such a command exist or not. Philosophical considerations about why specific commands should exists and which commands shouldn't is IMHO out of scope. – rudolfo.christ Nov 22 '19 at 14:05
  • I think maybe @jue's comment is more about asking why you want to do this. Maybe jue's thinking this might be an X-Y problem. – Drew Nov 22 '19 at 18:13
  • 2
    I don't think there is a command/key predefined for this. Node Inserting Text in the Emacs manual is where I'd expect this to be covered, if such a key existed. – Drew Nov 22 '19 at 18:18
  • The command you desire is bound by default to the key sequence SPC C-b. :P – Omar Nov 23 '19 at 17:37
2

There's no built-in command to insert a character before the point, presumably because any key binding for it would have to include at least one key stroke in addition to the character, and if you're going to type two keystrokes then the feature already exists: SPC Left or SPC Ctrl+B.

There is a built-in command open-line to insert a newline before the point. It exists because it does a few more things than simply inserting a newline, including adding a fill prefix or a margin if inserting a newline would normally add one.

The simplest way to write a command that inserts a character before the point is to insert a character, then move the point back.

(defun insert-space-before-point ()
  (interactive)
  (insert " ")
  (backward-char))

This works, but there's a clearer way to express that the point isn't moving, which is to call save-excursion around the part that moves the point. Among other advantages, this makes it straightforward to support a numeric repeat count like you can use when inserting a character.

(defun insert-space-before-point (&optional n)
  (interactive "p")
  (save-excursion
    (insert-char ?\  n)))

Depending on what you're doing, you may be better off using Emacs's support for right-to-left text, or in a very different vein Picture mode.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.