Search code examples
vimdigraphs

Why does a newline digraph in a macro get interpreted as a `j` in Vim?


Let's say I create a macro that goes down one line.

qajq

Register a contains exactly qajq, and so @a moves me down one line.

Now let's say I type j into my buffer and yank it into register a using "ayy. Now, register a looks like j^J (notice the terminal newline digraph). Now, when I execute the macro a, it jumps down two lines.

What exactly is happening here? Does Vim see the ^J digraph and think that I want to go down an extra line, or is it something more subtle?

I guess this is one of those questions where the answer is, "because that's how it was designed." :P


Solution

  • After you type qajq the a register content is the string j. qa starts recording the macro into register a, then you type j and it's the first character recorder, now you type q and vim stops recording. So, you end up with just j in the register a.

    The command yy works linewise, so the newline is also inserted into the register. When you type "ayy you end up with j^J in the a register because ^J or Ctrl-J (a line feed) is what vim uses to represent the newline, and it also moves the cursor one line down as described in :help Ctrl-J

    In order to yank the line without the newline you can type 0"ay$. That would be the same as typing qajq.