Open a file in a tab in vim in readonly mode

42,726

Solution 1

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.

Solution 2

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

:view /path/to/file

or from command line:

$ vim -M /path/to/file

Solution 3

vim -M filename opens the file in readonly mode.

Solution 4

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

Solution 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.

Share:
42,726

Related videos on Youtube

Srikanth
Author by

Srikanth

Updated on July 05, 2022

Comments

  • Srikanth
    Srikanth almost 2 years

    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.

    • Dharmit
      Dharmit almost 12 years
      vim -R works as read-only mode. vim -M opens the file with modifiable set to off.
  • Anthony DiSanti
    Anthony DiSanti almost 12 years
    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
  • Saulo Silva
    Saulo Silva over 10 years
    He specifically said "how to open a file from inside vim".
  • user456584
    user456584 over 10 years
    Still useful for people coming in from Google.
  • Seng Cheong
    Seng Cheong over 9 years
    @user456584 Only for those who are bad at using Google. This question is specifically about opening read-only in a tab.
  • Seng Cheong
    Seng Cheong over 9 years
    This question is specifically about opening read-only in a tab.
  • Aman
    Aman about 8 years
    I like this alternative; fits my normal workflow for tabs, and easier to remember.
  • erikbstack
    erikbstack over 7 years
    It's still good you posted that, because I came here exactly for that!
  • jpaugh
    jpaugh over 7 years
    user2015258's answer does this in one step.
  • shaahiin
    shaahiin over 5 years
    You can use vim -R filename for opening the file in read-only mode (file is modifiable, but not writable) and vim -M filename for opening the file with modifiable set to off (file is neither modifiable, nor writable).

Related