Vim Error E20 'Mark not set' when running BuffWrite Command

3.2k Views Asked by At

I set up an auto run script in my vimrc to condense any block of 3 or more empty newlines down to 3 newlines. I set a mark so after the script executes, I retain my cursor position but I'm getting an E20 Mark not set error when the cursor is within an area that is being removed.

How can I fix this issue/silence the error when this happens?

" .vimrc file: autocmd BufWrite * mark ' | silent! %s/\n\{3,}/\r\r\r/e | norm''

2

There are 2 best solutions below

1
romainl On BEST ANSWER

You could replace your marks with winsaveview() and winrestview().

autocmd BufWrite * let w:winview = winsaveview() | ... | if exists('w:winview') | call winrestview(w:winview) | endif
0
Ben On

Also silence the normal command:

autocmd BufWrite * mark ' | silent! %s/\n\{3,}/\r\r\r/e | silent! exe "norm! ''"