Below is an excerpt from my ~/.vimrc that approaches the OP's issue a bit differently. I didn't want to lose the warning message, which I find rather useful, but the hardcoded delay has simply become too annoying over time. It also interfered with some mappings that I use for the insert mode completion.
Here's the vimscript code:
" Get rid of the annoyingly long hardcoded delay when insert
" mode is entered for the first time on a read-only file, while
" emulating the original message that it rather useful
"
augroup vimrc-no-readonly-delay
autocmd!
autocmd InsertEnter *
\ if &readonly
\ | set noreadonly
\ | echohl ModeMsg
\ | echo "-- INSERT --"
\ | echohl NONE
\ | echon " "
\ | echohl ErrorMsg
\ | echon "Warning: Changing a readonly file"
\ | echohl NONE
\ | redraw
\ | sleep 500m
\ | endif
augroup END
Of course, you can adjust the sleep value to fit your prerefences. Having the emulated message displayed for 500 milliseconds (500m) works fine for me.
Below is an improved version of my vimscript code, which also avoids repeating the same warning message. For example, it happened when Shift + O was used to begin a new line above the cursor in a freshly opened read-only file.
Here's the improved vimscript code:
" Get rid of the annoyingly long hardcoded delay when insert
" mode is entered for the first time on a read-only file, but
" emulate the original message that it rather useful and avoid
" having it repeated, for example when using SHIFT-O
"
augroup vimrc-no-readonly-delay
autocmd!
autocmd BufEnter *
\ if &readonly
\ | set noreadonly
\ | let s:readonly = v:true
\ | else
\ | let s:readonly = v:false
\ | endif
autocmd InsertEnter *
\ if s:readonly
\ | let s:readonly = v:false
\ | echohl ModeMsg
\ | echo "-- INSERT --"
\ | echohl NONE
\ | echon " "
\ | echohl ErrorMsg
\ | echon "Warning: Changing a readonly file"
\ | echohl NONE
\ | redraw
\ | sleep 500m
\ | endif
augroup END
Actually, after using this vimscript code for some time, I figured out that unfortunately not all original warning messages are emulated and displayed this way. For example, no read-only warning is displayed when deleting a few lines in visual mode right upon opening a read-only file.
I spent some time trying to fix it using the FileChangedRO autocommand event, and I also tried using timers, but the end results were far from satisfactory. I'll see to implement a patch for vim that makes the timeout for the read-only warning configurable, and submit the patch upstream.
au BufEnter * set noro– Sep 30 '17 at 08:29