The &readonly parameter is local to the buffer and is not set before loading the buffer. The vimrc file is parsed before any buffer is loaded.
See what happens if you type vim -o /etc/passwd ~/readable . One buffer will be read-only, and the other will not. If this setting is on or off?
So, you need to connect to the BufReadPost , which is executed after reading the file into the buffer:
autocmd BufReadPost * \ if &readonly \| echo "read only" \| else \| echo "not read only" \| endif
What should give the expected results.
Note that this will be executed every time the buffer is loaded. If you just want to show it once, you will need to delete this auto-command when it starts. This can be done using a group of auto commands; autocommand! <group_name> autocommand! <group_name> will delete all auto commands in the group.
augroup readonly autocmd! autocmd BufReadPost * \ if &readonly \| echom "read only" \| else \| echom "not read only" \| endif \| autocmd! readonly augroup end
source share