8

When entering or doing any command mode modifications to a read only file for the first time you will get the warning

W10: Warning: Changing a readonly file

That's fine, but this also introduces a second or two delay before the action actually happens.

Is there a way to disable this delay?

3 Answers 3

3

Short version: there is no way to disable this, or to shorten the 1 second timeout :-( The good news is that you can just start typing while this message is being displayed, the characters are still inserted.


Longer version: from misc1.c:

    if (msg_silent == 0 && !silent_mode)
    {
        out_flush();
        ui_delay(1000L, TRUE); /* give the user time to think about it */
    }

The msg_silent is set when using :silent, for example :s/./a/ will give the warning, but :silent s/./a/ won't. There is, as far as I can see, no way to start insert mode silently (:silent startinsert doesn't work).

silent_mode is set with -s, from vim(1):

   -s          Silent  mode.   Only  when started as "Ex" or when the "-e"
               option was given before the "-s" option.

But as the help message says, this only works when using Vim as ex, and not when using it as vim.

0
4

You could simply :set noro before doing the modification. This should prevent the warning.

2
  • 2
    au BufEnter * set noro
    – user14526
    Sep 30, 2017 at 8:29
  • See my comment for a more elaborate version of this approach.
    – dsimic
    Sep 18, 2023 at 8:43
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.