if searchString != ""
let newBG = search(searchString) != 0 ? "green" : "red"
execute("hi StatusLine ctermfg=" . newBG)
endif
Explanation: It seems vim's search
function is the culprit (note that the function in question continuously runs vim's search
to update the color of the statusline). It automatically uses the previous search term if nothing is entered. This can be tested in a clean vim:
vim --clean
ia<CR>b<CR>a<esc>
(enter three lines, each with a single letter, end at third line)/a<CR>
(jumps to first line)/<CR>
(jumps to third line, because it uses the search terma
)
WARNING: This introduces another problem: The hlsearch-highlight skips one match now. What is highlighted before pressing Enter is not the closed match (which is what the search actually ends up presenting at pressing Enter).