1

I want to have predefined register that will jump to the location matched by regular expression, i.e. in vimrc I add:

let @a="/=\""

That will be useful for me when I want to navigate to the next entry point in HTML tag, such as class="" or method="". But when I test it I see the command appears at the bottom, but doesn't get executed until I press Enter. How can I embed a <Return> in the register, so the search is executed on @a?

  • Note that while you can't have spaces around the = in :set, you can do so in a :let command. I find let @a = "/=\"" much easier to read, so I'd highly recommend it! – filbranden Apr 23 at 20:46
0

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')
| improve this answer | |
0

I think you're mistaking buffers, registers and macros

The @a here is a register. It can contain any text, like a variable.

When you hit @a, you execute a macro, which will type what's written in the register as if you typed it.

A buffer in the other hand:

A buffer is a file loaded into memory for editing. All opened files are associated with a buffer. There are also buffers not associated with any file.

(from the Vim fandom)

So what you want to do here is to record a macro in the register a. to do so, you can do the following:

qa/="<ENTER>q

(Where <ENTER> is you, pressing the enter key)

  • qa will start recording the macro in the a register
  • /="<ENTER> this is what you want the macro to do
  • q to Stop the macro

Then, once that's done, the macro is contained into the a register, accessible via as a variable in @a

To print a register, from normal mode, you can:

"ap

From Vim doc:

" Use register {a-zA-Z0-9.%#:-"} for next delete, yank or put (use uppercase character to append with delete and yank) ({.%#:} only work with put).

So once you printed the content of the register (it should contain some weird character ^M maybe, that's the enter), you simply have to wrap it in quotes and add let @a = before it

| improve this answer | |
  • Personally, I don't really think the OP's usage of these terms was confusing at all... Macros and registers are essentially the same thing in Vim. When you record a macro, it ends in a register, so I don't see a problem in that. The usage of "buffer" here also didn't sound to me like a "Vim buffer", but more like a "memory area" where the register's contents are stored... My read on the question is that the OP wants to use the :let command to set the register's contents (i.e. create the macro), and not record it interactively, since that can be done from the vimrc file. See my answer. – filbranden Apr 23 at 20:44

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.