Reputation: 62588
A few micro-questions regarding normal!, incsearch and the / operator (search). I'm not solving any particular problem here, just trying to understand the relations between the mentioned, so I can put up some sort of analogy in my head.
If I have, set incsearch on, while in normal mode upon pressing /something Vim will jump to the first instance of something even before/without me pressing enter.
Okey, so normal! /something should do the same thing. Why doesn't it?
Similarly, normal! doesn't recognize special characters. So if I have a text file with this text in it,
something<cr>
(that's not a special character in there, but literally typed in text)
`normal! /something<cr>`
should put me up to that text. Why doesn't it?
I like it, but sometimes Vim's inconsistencies are, to put it mildly, most interesting :)
Upvotes: 0
Views: 161
Reputation: 3069
In :help :normal
the relevant text is:
{commands} should be a complete command. If
{commands} does not finish a command, the last one
will be aborted as if <Esc> or <C-C> was typed.
The command string you give to normal
has to be a complete command, or it aborts. However, normal! /something
isn't complete; if you were typing it, Vim would be waiting for you to finish. You could press Esc or Ctrl-C to abort. Or you could complete it by pressing Enter. So the command isn't complete and it aborts, instead of jumping. To get it to complete, give it the Enter it wants (at the command line):
:normal! /something^M
You can get the ^M
by typing Ctrl-V and then Enter. (If on Windows and Ctrl-V pastes, use Ctrl-Q.) Then you'll need to press Enter again to finish the whole command. If you were using execute
you could do it with:
:execute "normal! /something\<cr>"
Be sure to use double-quotes so the \<cr>
will be interpreted correctly.
I believe your second question is the same thing. The literally typed <cr>
doesn't affect this situation, you just need to provide the carriage return that will complete the search command, or normal
aborts it.
Since you've tagged this question with vimscript, I'm guessing that at least part of your interest is in using this in a script. Getting the literal carriage return into a script and having it work right is kind of a pain. In those cases, I would use execute
as shown above, so you can use the double-quote interpretted string method of getting the carriage return.
As for incsearch
, it won't have an effect because you can't be partially searching with normal
, as we've seen. You can only submit the entire search. It will, however, update the most-recent-search register "/
, so highlighting will still update correctly.
Upvotes: 4