vimnewlinevim-registers

Vim: How to handle newlines when storing multiple commands in registers?


I have a file where I store snippets of vim commands. When I need a snippet, I yank it and then execute it with @". The snippets are stored as a script, one line per command, like this:

:s/foo/bar/g
:echo "hello"
:s/1/2/g

Edit: I removed normal mode commands from the example, as they were not part of the problem.

Now this procedure doesn't work anymore: when executing the snippet, it just stops at the first line as if waiting for a newline.

Is there an option somewhere affecting how @ is executed? I'm pretty sure it was working some time ago...

Substituting the newline with a ^M character works but makes the file more difficult to handle.


Additional information:

Here's another symptom: when I yank a snippet, if I execute it with @" it stops at the first line as I just explained. But if I execute it with :@ it works. But the help file doesn't seem to imply any difference in how the two commands treat the register's content...


Solution

  • I finally found the culprit. Somehow I had a command mapping on <C-J> in my .vimrc file. When read with the default cpoptions, this turned into a mapping on <NL>.

    How I found out: I noticed that when starting vim with -u ~/.vimrc, it would indeed execute yanked snippets. I generated a session file with and without that commandline option and compared them. This way I found out that a different set of cpoptions where used to read the same .vimrc file, so that in one case the mapping was indeed on <C-J>, in the other it was converted into a mapping on <NL>!

    If someone has a similar problem, I suggest to look carefully at the currently set command mappings, with :cmap.