TransWikia.com

How to overwrite by pasting a string without selecting a region?

Emacs Asked by starquake on September 2, 2021

I can’t figure out how to paste a string, for example 145 characters long, into a text line in a way, that the next 145 characters after the string are overwritten by this string or simply erased.

An example with a 34 character string would be

| |

which is supposed to become this by pasting in the 34 character string "This is an example for the problem"

| This is an example for the problem |

Why should one want to do that?

While working with huge org mode tables any refreshing or re-aligning can become a pain in the ass, involving 20 minutes of frozen emacs session.
So, avoiding the need for re-freshing or re-aligning becomes very interesting, by editing a table in a way that doesn’t break aligning or trigger refresh in any way.

What I’ve tried (and didn’t work)

  • A manual way to do so is going into a field containing let’s say 180 space characters between both vertical column separator lines |, place cursor behind the 1st space character, paste the 145 character string, delete the 145 space characters after the pasted string. Then everything is still aligned in relation to neighbor lines and how the edited line was before, and no re-fresh is triggered.

    But the problem with that approach is, that it can’t be automated effectively by keyboard makros – that’s my only tool so far, because I can’t program:

    For that you need to be able to count how many characters the string is built of by M-= – as a consequence the number of characters is explicitely mentioned in *Messages* buffer – then go to *Messages* buffer, navigate in a general way to that number, copy it, go back to your table at the spot right after the pasted string, and doing C-d with prefix argument of that copied number.

    But that doesn’t work out: You can do all that, but at the very end it fails by not handling the pasted number from *Messages* buffer as numeric input for the prefix argument. So instead of doing C-u 145 C-d, pasting by C-y after having typed in C-u right before simply ends the C-u C-d operation and instead simply pastes that number 145 into the buffer right after the pasted 145 character string.

    The way all this came up is, that I’ve noticed that while filling the tables with huge amounts of data mistakes sneaked in for no reason.

    That made me start verifying every line filling to make sure that it’s filled correctly. That procedure was always the same, so after a while I made it so often, that it was so clear for me how to do it and so dull as a task, that I’ve created a keyboard macro for it. Whenever verification failed, it was always the same way so far, and that happened so often, that now I want to create a keyboard marco also for the fixing of lines where verification failed.

  • That overwriting by pasting I’ve done so far was like a manual re-construction of writing in overwrite mode, so why not going into overwrite mode instead, doing the pasting, and then going out of it again within the keyboard macro?

    That 2nd possibility, though, doesn’t work, either. It works just for typing in 1 character after another, not pasting a 145 character string. One could mark or select a region before and then paste the 145 character string, but that just leads you back to the core problem: You have the number of characters counted in the cache – this failed verification fix it’s 145, but the next one maybe 89, or 458, it’s always different from routine run to routine run – and you need to work with that number, apply it so that the selected region is just as long as 145 characters like the pasted string, and if you could do that, if you achieved this, then you could stick to the 1st possibility in the first place and wouldn’t need to try the 2nd possibility, because the problem is already solved.

I’ve tried a lot, checked the manual, but couldn’t solve that by myself.

There gotta has to be a straight way to do it.
When using isearch pasting with C-y a number like 145 is taken just as if one typed 1-4-5 straight away into the mini-buffer. But prefix arguments are not really mini-buffer and don’t work like that.
So that way I’ve tried didn’t work, so far.

One Answer

You can write

(defun overwriting-yank ()
  "Like `yank` but overwrites the corresponding text."
  (interactive)
  (let ((txt (current-kill 0)))
    (insert-for-yank txt)
    (delete-char (length txt))))

But this will misbehave in all kinds of circumstances (e.g. when yanking a multiline string, when yanking into a line shorter than the text you're yanking, ...).

Also, IIUC you're interested in columns more than in number of chars, so the behavior won't be quite right if there are double-width chars or TAB chars involved.

etc...

Finally, your description makes it clear that the only reason you want such an operation is in order to work around bugs in Org. So please make sure you report those bugs and try to work with Org mode's maintainers to get rid of them.

Answered by Stefan on September 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP