I have a function that counts the number of matches in a file using the substitute Ex command with the /n flag and returns the value. However, I cannot echo the answer because the status line is getting overwritten by the file name. In other words, I will do this:
:echo Myfun("pattern")
and the result will be:
"scratch1" [Modified][New file] 3 lines --100%--
If I use the message history (echom) it clearly shows that the correct result is being generated and returned, but the message is getting overwritten by the "modified" message. I don't understand this because the script does not modify anything. Why is the status line getting overwritten?
The effect seems to be caused by bufdo. A minimal example of Myfun is the following:
function! Myfun( pattern )
let command = '%s/'.a:pattern.'//gen'
let count_pattern = 0
bufdo let count_pattern += str2nr( strpart( execute( command ), 1 ) )
return count_pattern
endfunction
If the environment has only one open buffer then the echo command works correctly, but if there are more than one buffers open, then the status line is getting overwritten. In other words, what is happening is the following sequence of events:
- function runs (switching buffers in the process)
- function returns a number it calculated
- echo command prints that number to the status line
- Vim writes ""example" [New file] --No lines in buffer--" to status line
So, after the command is over Vim is updating the status line if the buffer was changed at any point when the command executed. I need to prevent this from happening to see the result.
silent[!]
?