Brian Egizi
Brian Egizi

Reputation: 13

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

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''

Upvotes: 1

Views: 3318

Answers (2)

Ben
Ben

Reputation: 8905

Also silence the normal command:

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

Upvotes: 0

romainl
romainl

Reputation: 196596

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

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

Upvotes: 1

Related Questions