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

81

17

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.

Nifle

Posted 2010-10-28T09:46:11.283

Reputation: 31 337

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. – Franklin Yu – 2016-08-02T23:00:23.303

Answers

47

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)'
}

Trey Jackson

Posted 2010-10-28T09:46:11.283

Reputation: 3 731

11I wish I could downvote emacs for this. – Robert Calhoun – 2014-08-29T15:11:05.997

145

vim -R filename

RedGrittyBrick

Posted 2010-10-28T09:46:11.283

Reputation: 70 632

32And all vim users take revenge by upvoting it to more than the "accepted answer" :D – Xosofox – 2015-11-11T09:33:15.283

1Note: some boxes seem to have 'view' command maybe aliases to 'vi -R'. – armyofda12mnkeys – 2017-08-10T19:31:56.723

4Works great but I accepted Trey's answer because I prefer emacs over vim – Nifle – 2010-10-29T06:35:17.730

26

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)

mrverrall

Posted 2010-10-28T09:46:11.283

Reputation: 855

1Reason 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. – Franklin Yu – 2016-08-02T22:57:29.583

1No syntax highlighting though. – Nifle – 2010-10-28T11:59:25.360

7@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. – garyjohn – 2010-10-28T15:33:30.310

10

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.

Jay

Posted 2010-10-28T09:46:11.283

Reputation: 614

+1 for using less, although as @nxdrvr points out you can press the ___v___ key to open the file in editiable mode in vi – Sheharyar – 2015-01-19T02:11:48.867

9

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.

Chirael

Posted 2010-10-28T09:46:11.283

Reputation: 191

6

To just view file without ability to edit:

cat <file> | less

In less you can go to "edit file mode" by pressing the v key. But you cannot edit standard input, so piping the output of cat <file> to less, stops less from going to 'edit' mode on pressing 'v'.

For vim the same approach

cat <file> | vim -

nxdrvr

Posted 2010-10-28T09:46:11.283

Reputation: 61

2+1, but no need for cat. Just use <FILENAME less. – Sparhawk – 2014-10-09T05:07:21.957

@Sparkhawk, <file> less would give an error, I'm assuming you meant less <file>, but as explained in the answer, when using less directly, the user could press ___v___ key and go into edit-mode. Piping cat to less stops this from happening. – Sheharyar – 2015-01-19T02:30:17.607

@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 – 2016-08-02T22:45:16.537

4Thanks for the v suggestion. Nifty. – tshepang – 2014-04-04T09:08:13.923

5

For emacs you can also use the view-mode.

emacsclient --create-frame --eval '(view-file "/tmp/EXAMPLE")'

or alternative inside a terminal:

emacsclient --nw --eval '(view-file "/tmp/EXAMPLE")'

Or you can use my wrapper script

user86064

Posted 2010-10-28T09:46:11.283

Reputation:

1

In emacs you can do

emacs FILE -f view-mode

Syntax highlighting is applied. It doesn't just open the file as a read only buffer. Some commands, such as I-search, are accessible without the control key in this mode.

jogama

Posted 2010-10-28T09:46:11.283

Reputation: 11

0

I am not going to discrad to anyone user answer here, but i would like to aadd some more info about Read-only Mode file. As per oreilly documentation Read-only Mode There will be times when you want to look at a file but want to protect that file from inadvertent keystrokes and changes. (You might want to call in a lengthy file to practice vi movements, or you might want to scroll through a command file or program). You can enter a file in read-only mode and use all the vi movement commands, but you won't be able to change the file.

To look at a file in read-only mode, enter either:

$ vi -R file

or:

$ view file

(The view command, like the vi command, can use any of the command-line options for advancing to a specific place in the file.) If you do decide to make some edits to the file, you can override read-only mode by adding an exclamation point to the write command:

:w!
or:

:wq!

If you have a problem writing out the file.

For further ref here

Md Haidar Ali Khan

Posted 2010-10-28T09:46:11.283

Reputation: 101

0

sending a file to std out, may be acceptable given the size of the file

cat <file>  # dump whole file to stdout
head <file> # view the first few lines
tail <file> # view the last n lines

Nathan Feger

Posted 2010-10-28T09:46:11.283

Reputation: 173