TL;DR: You need either this:
let @a = "/=\"\n"
Or this:
call setreg('a', "/=\"\<Return>", 'c')
There are a few ways to enter a <Return>
character in a double-quoted string. See :help expr-quote
for details.
First, you can use the \<Return>
notation, which you can actually use with any special keystroke to enter the keycode for that key.
So this string would produce the search and execute the command.
"/=\"\<Return>"
Or also:
"/=\"\<CR>"
(You'll often find <CR>
used more often than <Return>
in Vim documentation or plug-ins. They are synonymous. Also <Enter>
.)
Another way is to use \r
which is exactly the same as \<CR>
or \<Return>
:
"/=\"\r"
(The \r
sequence comes from languages such as C and later adopted by many other languages, it's the sequence that expands to a "carriage return" character, which is also what <CR>
means.)
But there's a problem with using any of these three strings in a :let
expression on a register, so this will not work:
let @a = "/=\"\<CR>"
This will actually produce register contents that include an extra \n
at the end. If you read :help :let-register
, you'll see an explanation of why that happens:
[...] If the result of {expr1}
ends in a <CR>
or <NL>
, the register will be linewise, otherwise it will be set to characterwise.
When a register is linewise, Vim will always include a newline character \n
at the end of it. So it will find the <CR>
and add a <NL>
. The result is that using the @a
macro will look for the ="
string, find the next match, and then jump to the next line as if you had pressed "Enter" again.
This one will actually work:
let @a = "/=\"\n"
The \n
sequence is the "newline" character and is typically used as a line break on Linux/Unix systems. It's not really the most accurate one to use, since when you press the "Enter" or "Return" key on your keyboard, the key code that is actually produced is <CR>
or \r
and not \n
or <NL>
. But the \n
works to execute the search. And it doesn't have the side-effect of having :let
add an extra newline at the end of the register when it's stored as a linewise register.
Another possible approach is to use the setreg()
function instead of the :let
command. The setreg()
function takes an optional third argument which lets you pick whether you want a characterwise, linewise or blockwise register. So you can pass a c
to force it to be characterwise, even though it ends in a <CR>
.
call setreg('a', "/=\"\<CR>", 'c')
Or:
call setreg('a', "/=\"\<Return>", 'c')
Also the same as:
call setreg('a', "/=\"\r", 'c')
=
in:set
, you can do so in a:let
command. I findlet @a = "/=\""
much easier to read, so I'd highly recommend it! – filbranden♦ Apr 23 '20 at 20:46