86

I'm aware of opening files in readonly mode from shell using vim -R, but how to open a file from inside vim in a separate tab (:tabe <filename>) in readonly mode?

Thanks for your time.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Srikanth
  • 11,780
  • 23
  • 72
  • 92

7 Answers7

89

To open a file in read only mode in a new tab, use

tab sview /path/to/file

To open the file in the same pane, (without using a new window or tab), use

view /path/to/file

Note that tab view /path/to/file does not open a new tab.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
ZyX
  • 52,536
  • 7
  • 114
  • 135
  • 43
    For anyone that isn't looking to use tabs, simply `:view /path/to/file` will open a new buffer with the file in read-only mode – Anthony DiSanti Jun 15 '12 at 20:59
45

You can open a file in readonly mode from inside vim:

:view /path/to/file

or from command line:

$ vim -M /path/to/file
sudo make install
  • 5,629
  • 3
  • 36
  • 48
user2015258
  • 1,287
  • 10
  • 5
31

vim -M filename opens the file in readonly mode.

erikbstack
  • 12,878
  • 21
  • 81
  • 115
Mr Lou
  • 1,757
  • 19
  • 19
23

Just open your file by using :tabe <filename>, then enter :view. It will automatically switch to read-only mode.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Jeyamaran
  • 783
  • 1
  • 9
  • 16
  • 3
    I like this alternative; fits my normal workflow for tabs, and easier to remember. – Aman Feb 03 '16 at 01:37
6
vim -R /path/to/file
vim -m /path/to/file

Read-only mode. Modification in text is allowed but you cannot write (no saving).

vim -M /path/to/file

Even modification in text is not allowed.

ghchoi
  • 4,812
  • 4
  • 30
  • 53
5

Try :tabedit +set\ noma|set\ ro FILE; this will open FILE in a new tab with modifiable off and readonly on, preventing you from modifying or writing the file. If you just want readonly, omit the noma set. Might be convenient to remap this to another command.

Wyatt Anderson
  • 9,612
  • 1
  • 22
  • 25
2

Something that works for me:

:vnew path/to/file

and once the file is opened, give the view command:

:view
Vivek
  • 96
  • 2