How can I open a file read-only from command line with emacs/vi/vim

74,482

Solution 1

For emacs:

emacs FILE --eval '(setq buffer-read-only t)'

There's no startup option to force read only.

Edit:
If you put this small function in your shell startup script (.bashrc for example) you can open a file read-only by typing ev file_to_view

ev() {
  emacs "$1" --eval '(setq buffer-read-only t)'
}

Solution 2

vim -R filename

Solution 3

view filename

Basically vim in read-only mode; simples!

As hinted by comment, in case view is linked to plain vi, here are bash commands to first inspect situation and then fix it:

# non-destructive inspection
which vim
which view
ls -l $(which view)

# overwrite current view with symlink to vim, requires root
ln -sfv $(which vim) $(which view)

Solution 4

vim -R <file>

allows writing with :w!

vim -c ":set nomodifiable"  <file>

Prevents the user from making any changes to the file in the buffer. But the user could make the buffer modifiable with :set modifiable

You could use

vim -c ":noremap q :q<cr>" -c ":map : <Esc>" -c ":set nomodifiable" <file>

to prevent the user from turning off the "nomodifiable", and allow the user to quit by pressing q. But, then the user can't enter command mode at all, which may or may not be what you want.

You could also open the file with the less command:

less <file>

To view the file in a vim-like environment but without the ability to change the file.

Solution 5

Small follow-up to the accepted answer: You can alias this in your shell to reduce it to a single command. For example in bash you can put the following in your .bashrc:

emacsro() {
  emacs $1 --eval '(setq buffer-read-only t)'
}

(different shells will have different formats for doing this, but you get the idea)

I would have added this as a comment in reply to the accepted answer, but it didn't seem possible to have a multi-line "code" block in a comment, and (in bash anyway) the above code really does need to be on 3 separate lines.

Share:
74,482

Related videos on Youtube

Julian
Author by

Julian

A rather lazy sysadmin and sometimes coder

Updated on September 17, 2022

Comments

  • Julian
    Julian over 1 year

    Is there a way to tell emacs/vi/vim (from the command line) that I want to view the file in view-mode or read-only.

    I know how to open a file as read only if emacs/vi/vim is already running.

    • Admin
      Admin over 7 years
      Note: if you don't have the write permission to the file, Vim will by default open it in read-only mode. Actually, I think it is easier to control the file permission than to provide a "read-only" way.
  • Julian
    Julian over 13 years
    No syntax highlighting though.
  • garyjohn
    garyjohn over 13 years
    @Nifle: There shouldn't be any difference in syntax highlighting. If your vim has syntax highlighting but your view doesn't, perhaps your view is a link to a minimal version of vim that doesn't have syntax highlighting compiled in. Compare the outputs of the :version command.
  • Julian
    Julian over 13 years
    Works great but I accepted Trey's answer because I prefer emacs over vim
  • tshepang
    tshepang about 10 years
    Thanks for the v suggestion. Nifty.
  • Nick
    Nick over 9 years
    I wish I could downvote emacs for this.
  • Sheharyar
    Sheharyar over 9 years
    +1 for using less, although as @nxdrvr points out you can press the v key to open the file in editiable mode in vi
  • Xosofox
    Xosofox over 8 years
    And all vim users take revenge by upvoting it to more than the "accepted answer" :D
  • Franklin Yu
    Franklin Yu over 7 years
    @Sheharyar He meant output redirection. Maybe you are more familiar with less < FILENAME, which gives you "Cannot edit standard input" when you press V. Similarly with Vim: vim - < FILENAME.
  • Franklin Yu
    Franklin Yu over 7 years
    Reason why it "just works": Vim will actually read argv[0] to decide its behavior. It's a common trick; AFAIK sometimes GCC and Bash does this as well.
  • armyofda12mnkeys
    armyofda12mnkeys over 6 years
    Note: some boxes seem to have 'view' command maybe aliases to 'vi -R'.
  • stafusa
    stafusa almost 4 years
    To open more than one file, the "-eval ..." has to be repeated after every filename, otherwise only the last file in the list is opened read-only.
  • Winny
    Winny about 3 years
    Quick n dirty hack to work with multiple files opened on the command line: emacs --eval "(add-hook 'find-file-hook (defun make-read-only () (setq buffer-read-only t)))" file1 file2 ...
  • Y. E.
    Y. E. over 2 years
    See also emacs FILE -f view-mode answer.