Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if I've been run in read-only mode (-R) in vimrc

Tags:

vim

In my .vimrc, I'd like to be able to check if I've been run either by vim -R or view. If so, I plan to either disable or delay some plugins from loading to speed up the times when I quickly want to look at a file.

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

Checking if &readonly almost works. If I have this:

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

Then I get read only echoed 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!

like image 708
jmacdonagh Avatar asked Mar 31 '16 00:03

jmacdonagh


People also ask

How do I get out of read-only file?

We can now exit normally by typing ESC :q! since the file is still opened as read-only.

How do I exit read-only mode in Linux?

Press the [Esc] key and type Shift + Z Z to save and exit or type Shift+ Z Q to exit without saving the changes made to the file.

Which command opens an existing file in read-only mode?

vi -R filename Opens an existing file in the read-only mode.


1 Answers

Vim's program name (eg view) can be detected with the v:progname variable. This doesn't solve checking for vim -R, but it does allow you to use this variable in a condition in your .vimrc and potentially improve the efficiency of your config. For example:

if v:progname ==? 'view'
  " Do something
endif
like image 61
davidjb Avatar answered Oct 12 '22 06:10

davidjb