Is there a way to start vim in read only mode

10,098

Solution 1

See $ vim --help for Vim's many launch arguments.

For launching Vim in read only mode that would be:

$ vim -R filename

EDIT

$ view and $ vim -R seem to be one and the same by way of symbolic links. I'm not sure what features you'll gain from running $ vim -R that you don't have with $ view.

Here, on Ubuntu, I can visually select stuff in both.

Solution 2

If you forgot the -R option when start up vim, you can use:

:set ro

Solution 3

If your goal is simply not to overwrite the original file, you can vim's read from stdin feature:

cat filename | vim -

Solution 4

As @garyjohn mentioned, view is often symbolically linked to either vi or vim. Depending on which Linux distribution you are using and how vi or vim were installed this could vary. On my RHEL 6 system the output of ls -l `which view` shows that /bin/view is symbolically linked to vi.

So if you want the features of vim in read-only mode you would either need to alias view to vim or use vim -R <filename> solution provided by @kev (assuming you don't have privileges to change the symbolic links on the system in question).

Share:
10,098

Related videos on Youtube

nelaaro
Author by

nelaaro

Linux admin, tech enthusiast. opensource evangelist.

Updated on September 18, 2022

Comments

  • nelaaro
    nelaaro over 1 year

    I use /bin/view as it is a safe way to view text files and not have accidental changes persist once I am finished.

    /bin/view seams to be lack features that normal vim has. Is there a way to start vim in read only mode, or start /bin/view with visual mode.

    I want to be able to select text and paste it with out having to use my mouse. Visual mode make it so easy to do that.

    The problem I am having is related to CentOS / redhat Linux distros, is the -visual in view.

    # vim --version | grep visual
    +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo +vreplace 
    # view --version | grep visual
    -textobjects -title -toolbar -user_commands -vertsplit -virtualedit -visual
    
    • garyjohn
      garyjohn about 12 years
      view is a link (usually symbolic these days) to vim that runs vim in read-only mode. Your view seems to be linked to a different vim than the one you get from executing "vim". Take a look at the update-alternatives(8) man page to see how to fix that.
  • nelaaro
    nelaaro about 12 years
    In centos view and vim -R are not the same. view is compiled with -visual for some reason.
  • Benjamin R
    Benjamin R about 6 years
    This is what I did, and it works perfectly (where vim is any version/flavour of Vim). Now I can have a read-only window for viewing side-by-side diffs in vim, with another window also editing the actual file at the same time.
  • Makmeksum
    Makmeksum about 4 years
    When you use :set ro, how do you enable writing egain?
  • enharmonic
    enharmonic about 4 years
    @Makmeksum :set noro or :set noreadonly; or when quitting use :wq!