How to check if I was running in read-only (-R) mode in vimrc

In my .vimrc, I would like to be able to check whether I was launched either using vim -R or view . If so, I plan to either disable or delay some plugins from loading, in order to speed up the time when I quickly want to see the file.

:args and argv() only show me which files I wanted to edit, and not all the command line flags.

The if &readonly check almost works. If I have this:

 if &readonly echo "read only" else echo "not read only" endif 

Then I get a read only echo when I execute view or vim -R , but if I do either view ~/.vimrc or vim -R ~/.vimrc , then I get not read only . Very strange.

Thanks!

+5
source share
2 answers

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 
+1
source

The name of the Vim program (for example, view ) can be found using the v:progname . This does not resolve to check vim -R , but allows you to use this variable in state in .vimrc and potentially increase the efficiency of your configuration. For instance:

 if v:progname ==? 'view' " Do something endif 
0
source

Source: https://habr.com/ru/post/1246111/


All Articles