2

How should I enter the following as a single string at the command prompt in vim

:let v:errmsg = ""  
:silent! /\cend of .*project gutenberg  
:if v:errmsg != ""  
:echo "Not found"  
:endif  

This does not work, the message is not printed.

:let v:errmsg = ""|:silent! /\cend of .*project gutenberg|:if v:errmsg != ""|:echo "Not found"|:endif  
1
  • What's wrong with the message you get when there's no match? Commented Mar 31, 2013 at 20:24

2 Answers 2

2

Well, the problem is the normal search you are doing. The range search sees the | as part of its arguments and therefore it cannot be used to enter another command. Therefore, wrap it into an :exe call like this:

let v:errmsg = ""|exe 'sil! /\cend of .*project gutenberg'|if v:errmsg != ""|echo "Not found"|endif 
Sign up to request clarification or add additional context in comments.

2 Comments

Where have you seen normal here? /… search is range, it works just like :1 to go to the first line.
Thank you, that solves the problem. I needed this to include in a key mapping, and I find myself continually frustrated by command sequences that will work entered individually at the prompt, but which fail when transferred to vimrc as a string. But apparently the underlying cause of this problem is the mode that vim is in in each case?
0

Range-search may be not the best option in your case. You can use

if !search('\cend of .*project gutenberg') | echo 'Not found' | endif

if you are fine with not updating last search pattern.

1 Comment

Thank you, this is a simpler solution, and as it also relocates the prompt if it does find it, which is what I really wanted from the search, it serves my purpose better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.