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.

link|improve this question

feedback

5 Answers

up vote 7 down vote accepted

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)'
}
link|improve this answer
feedback

vim -R filename

link|improve this answer
Works great but I accepted Trey's answer because I prefer emacs over vim – Nifle Oct 29 '10 at 6:35
feedback

view filename

Basically vim in read-only mode; simples!

link|improve this answer
1  
No syntax highlighting though. – Nifle Oct 28 '10 at 11:59
2  
@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 Oct 28 '10 at 15:33
feedback
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.

link|improve this answer
feedback

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
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.