Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

8 Answers 8

up vote 23 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)'
}
share|improve this answer

vim -R filename

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

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)
share|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

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|improve this answer
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.

share|improve this answer

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

share|improve this answer

To just view file without ability to edit

cat <filename> | less

In "less" you can go to "edit file mode" by pressing "v". But you cannot edit standard input.

share|improve this answer
    
Thanks for the v suggestion. Nifty. –  Tshepang Apr 4 at 9:08

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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