'Omit 'Pattern not found' error message in Vim script

I added a function in my .vimrc that's just few search and replace commands. However if one of the patterns is not found I get an error message that I need to enter through. How can I suppress this?



Solution 1:[1]

You might use silent:

:silent %s/x/y/g

or, if you need to do string manipulation to determine the strings to search and replace:

exec ":silent %s/x/" . varName . "/g"

I'm not 100% sure, but I think that silent only works in scripts.

Solution 2:[2]

Probably a little bit off topic but i found this thread when searching for solutions for "search pattern not found" errors in vim, when i wanted to search for the next occurence in vim. The following map omits the error when pressing 'n'

map n :silent! /

Solution 3:[3]

Maybe the :he :silent thing can be used to suppress those errors?

Solution 4:[4]

this also works for global operation

:silent g/^$/d'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paolo Tedesco
Solution 2 user6359986
Solution 3 Sam
Solution 4 zzapper