How can I open a file read-only from command line with emacs/vi/vim
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.
Related videos on Youtube
Comments
-
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
orread-only
.I know how to open a file as read only if emacs/vi/vim is already running.
-
Admin over 7 yearsNote: 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 over 13 yearsNo syntax highlighting though.
-
garyjohn over 13 years@Nifle: There shouldn't be any difference in syntax highlighting. If your
vim
has syntax highlighting but yourview
doesn't, perhaps yourview
is a link to a minimal version ofvim
that doesn't have syntax highlighting compiled in. Compare the outputs of the:version
command. -
Julian over 13 yearsWorks great but I accepted Trey's answer because I prefer emacs over vim
-
tshepang about 10 yearsThanks for the
v
suggestion. Nifty. -
Nick over 9 yearsI wish I could downvote emacs for this.
-
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 invi
-
Xosofox over 8 yearsAnd all vim users take revenge by upvoting it to more than the "accepted answer" :D
-
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 over 7 yearsReason 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 over 6 yearsNote: some boxes seem to have 'view' command maybe aliases to 'vi -R'.
-
stafusa almost 4 yearsTo 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 about 3 yearsQuick 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. over 2 yearsSee also emacs FILE -f view-mode answer.