3

I've changed 2 pieces of text accidentally, by using C. Is it possible to access the previous piece, not the last one? (after getting out of vim)

If I log back in, by pressing p it prints the last snippet, is there any way to get the previous one?

Edit

I've just checked registers can be accessed from command line mode typing :registers, but for some reason this register isn't there, and instead is just the first word -.-

5

Usually the text you delete goes into the number registers starting at 1, so "2p would put the contents of the second-to-last text that you deleted. But unfortunately that's only the case for when you delete whole lines, for example with a command such as dd or cc.

The C command (equivalent to c$) only changes part of a line (from the cursor to the end of the line), so it ends up using the "- register (also known as "small delete register") instead. And that register only holds the last deleted text, so once you overwrite it with another short deletion, the previous contents are lost.

As mentioned in :help "1:

Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (the small delete register is used then). An exception is made for the delete operator with these movement commands: %, (, ), `, /, ?, n, N, { and }. Register "1 is always used then (this is Vi compatible). The "- register is used as well if the delete is within a line.

Using "undo" would be an option, but you mentioned you quit Vim and entered again, so this would only help if you had configured Vim to preserve "undo" across sessions, which is not the default.

4
  • 1
    Thank you anyways!
    – Minsky
    Jan 28 at 23:25
  • 2
    This is a major Vim pain. There are no less than ten numbered registers, yet things deleted with D or C should go straight to a black hole? I just can't understand why. You may want to add :help quote0: "Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (the small delete register is used then). An exception is made for the delete operator with these movement commands: |%|, |(|, |)|, |`|, |/|, |?|, |n|, |N|, |{| and |}|."
    – Quasímodo
    Jan 29 at 11:09
  • 1
    @Quasímodo Incorporated that into the answer. Thanks for the suggestion! Agree that it's a bit of a pain.
    – filbranden
    Jan 29 at 17:14
  • 3
    relevant issue: github.com/vim/vim/issues/7756
    – user938271
    Jan 29 at 18:43

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.