How to prevent saving file in vim
I'm customizing vim (vimdiff) as a testing tool for diffing certain files, and would like to disable saving the files completely - however, I haven't been able to stop :w! .. I've tried starting vim/vimdiff with -R option, and I have also tried setting cpoptions to contain W, which means:
cpo-W
W Don't overwrite a readonly file. When omitted, ":w!"
overwrites a readonly file, if possible.
..but vim seems not to care about this:
echo yo > foo
vim -R foo
:set cpoptions=aABceFsW
:set cpoptions?
cpoptions=aABceFsW
:w
E45: 'readonly' option is set (add ! to override)
:w!
"foo" 1L, 3C written
..bug, or did I misunderstand it?
vim
add a comment |
I'm customizing vim (vimdiff) as a testing tool for diffing certain files, and would like to disable saving the files completely - however, I haven't been able to stop :w! .. I've tried starting vim/vimdiff with -R option, and I have also tried setting cpoptions to contain W, which means:
cpo-W
W Don't overwrite a readonly file. When omitted, ":w!"
overwrites a readonly file, if possible.
..but vim seems not to care about this:
echo yo > foo
vim -R foo
:set cpoptions=aABceFsW
:set cpoptions?
cpoptions=aABceFsW
:w
E45: 'readonly' option is set (add ! to override)
:w!
"foo" 1L, 3C written
..bug, or did I misunderstand it?
vim
My understanding: You just told Vim that it should run in "readonly mode". The file itself is not readonly. If youchmod -w foo, then Vim would not allow to write the file on:w!. It prints error "E504".
– user7369280
17 hours ago
Ahhh you're right.. Thanks! I can accept that as an answer if you'd like.
– boffman
17 hours ago
add a comment |
I'm customizing vim (vimdiff) as a testing tool for diffing certain files, and would like to disable saving the files completely - however, I haven't been able to stop :w! .. I've tried starting vim/vimdiff with -R option, and I have also tried setting cpoptions to contain W, which means:
cpo-W
W Don't overwrite a readonly file. When omitted, ":w!"
overwrites a readonly file, if possible.
..but vim seems not to care about this:
echo yo > foo
vim -R foo
:set cpoptions=aABceFsW
:set cpoptions?
cpoptions=aABceFsW
:w
E45: 'readonly' option is set (add ! to override)
:w!
"foo" 1L, 3C written
..bug, or did I misunderstand it?
vim
I'm customizing vim (vimdiff) as a testing tool for diffing certain files, and would like to disable saving the files completely - however, I haven't been able to stop :w! .. I've tried starting vim/vimdiff with -R option, and I have also tried setting cpoptions to contain W, which means:
cpo-W
W Don't overwrite a readonly file. When omitted, ":w!"
overwrites a readonly file, if possible.
..but vim seems not to care about this:
echo yo > foo
vim -R foo
:set cpoptions=aABceFsW
:set cpoptions?
cpoptions=aABceFsW
:w
E45: 'readonly' option is set (add ! to override)
:w!
"foo" 1L, 3C written
..bug, or did I misunderstand it?
vim
vim
asked 18 hours ago
boffmanboffman
1411111
1411111
My understanding: You just told Vim that it should run in "readonly mode". The file itself is not readonly. If youchmod -w foo, then Vim would not allow to write the file on:w!. It prints error "E504".
– user7369280
17 hours ago
Ahhh you're right.. Thanks! I can accept that as an answer if you'd like.
– boffman
17 hours ago
add a comment |
My understanding: You just told Vim that it should run in "readonly mode". The file itself is not readonly. If youchmod -w foo, then Vim would not allow to write the file on:w!. It prints error "E504".
– user7369280
17 hours ago
Ahhh you're right.. Thanks! I can accept that as an answer if you'd like.
– boffman
17 hours ago
My understanding: You just told Vim that it should run in "readonly mode". The file itself is not readonly. If you
chmod -w foo, then Vim would not allow to write the file on :w!. It prints error "E504".– user7369280
17 hours ago
My understanding: You just told Vim that it should run in "readonly mode". The file itself is not readonly. If you
chmod -w foo, then Vim would not allow to write the file on :w!. It prints error "E504".– user7369280
17 hours ago
Ahhh you're right.. Thanks! I can accept that as an answer if you'd like.
– boffman
17 hours ago
Ahhh you're right.. Thanks! I can accept that as an answer if you'd like.
– boffman
17 hours ago
add a comment |
2 Answers
2
active
oldest
votes
You just told Vim that it should run in "readonly mode". The file itself is not readonly. If you chmod -w foo, then Vim would not allow to write the file on :w! while W is present in cpoptions.
You could try to start Vim with -M:
vim -M foo
This disallows any changes to the file and prevents writing the file. See :help -M.
Another solution might be:
vim -m foo
This opens the file and you can edit it (you get the warning W10: Warning: Changing a readonly file), but you are not able to write it. The option write is reset (to nowrite) and prevents writing to any file. So this is global.
If you finally decide, that you need to write it anyway, you have to set the option with the command :set write. As said, this is global, so you are than able to write any file.
Actually, I want to be able to change the contents of the files, just not save them. But then I also noticed now that there is a -m option, that prevents all kinds of saving.. HOWEVER.. I also would like to run :TOhtml and save the diff as html and -m prevents saving the html :-D
– boffman
16 hours ago
@boffman See my edit.
– user7369280
16 hours ago
Perfect. -w in combination with set [no]write solved my problems! Thanks again!
– boffman
11 hours ago
add a comment |
It's much better to work on the file permissions, or to use the -M option as stated by @user7369280, but the funny thing is you can also launch vi with certain options that will prevent you to use the classic w or wq commands for example:
vi my_file -c 'cabbrev w <esc>' -c 'cabbrev wq <esc>'
It basically disables the commands w and wq with redefining them with the command escape (which doesn't do much)
You can still break out of that, though, with careful character entry and deletion, and cursor movements. Also, there are more commands that write files::wa,:x, ...
– L3viathan
17 hours ago
For some reason, those cabbrev commands/options does not seem to work for me, I can still save with :w / :wq
– boffman
17 hours ago
@boffman it might depends on the vi version. It's actually a vim only feature. You may try withabinstead ofcabbrev. For example:vi my_file -c 'ab wq <esc>'
– Yoric
16 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250472%2fhow-to-prevent-saving-file-in-vim%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You just told Vim that it should run in "readonly mode". The file itself is not readonly. If you chmod -w foo, then Vim would not allow to write the file on :w! while W is present in cpoptions.
You could try to start Vim with -M:
vim -M foo
This disallows any changes to the file and prevents writing the file. See :help -M.
Another solution might be:
vim -m foo
This opens the file and you can edit it (you get the warning W10: Warning: Changing a readonly file), but you are not able to write it. The option write is reset (to nowrite) and prevents writing to any file. So this is global.
If you finally decide, that you need to write it anyway, you have to set the option with the command :set write. As said, this is global, so you are than able to write any file.
Actually, I want to be able to change the contents of the files, just not save them. But then I also noticed now that there is a -m option, that prevents all kinds of saving.. HOWEVER.. I also would like to run :TOhtml and save the diff as html and -m prevents saving the html :-D
– boffman
16 hours ago
@boffman See my edit.
– user7369280
16 hours ago
Perfect. -w in combination with set [no]write solved my problems! Thanks again!
– boffman
11 hours ago
add a comment |
You just told Vim that it should run in "readonly mode". The file itself is not readonly. If you chmod -w foo, then Vim would not allow to write the file on :w! while W is present in cpoptions.
You could try to start Vim with -M:
vim -M foo
This disallows any changes to the file and prevents writing the file. See :help -M.
Another solution might be:
vim -m foo
This opens the file and you can edit it (you get the warning W10: Warning: Changing a readonly file), but you are not able to write it. The option write is reset (to nowrite) and prevents writing to any file. So this is global.
If you finally decide, that you need to write it anyway, you have to set the option with the command :set write. As said, this is global, so you are than able to write any file.
Actually, I want to be able to change the contents of the files, just not save them. But then I also noticed now that there is a -m option, that prevents all kinds of saving.. HOWEVER.. I also would like to run :TOhtml and save the diff as html and -m prevents saving the html :-D
– boffman
16 hours ago
@boffman See my edit.
– user7369280
16 hours ago
Perfect. -w in combination with set [no]write solved my problems! Thanks again!
– boffman
11 hours ago
add a comment |
You just told Vim that it should run in "readonly mode". The file itself is not readonly. If you chmod -w foo, then Vim would not allow to write the file on :w! while W is present in cpoptions.
You could try to start Vim with -M:
vim -M foo
This disallows any changes to the file and prevents writing the file. See :help -M.
Another solution might be:
vim -m foo
This opens the file and you can edit it (you get the warning W10: Warning: Changing a readonly file), but you are not able to write it. The option write is reset (to nowrite) and prevents writing to any file. So this is global.
If you finally decide, that you need to write it anyway, you have to set the option with the command :set write. As said, this is global, so you are than able to write any file.
You just told Vim that it should run in "readonly mode". The file itself is not readonly. If you chmod -w foo, then Vim would not allow to write the file on :w! while W is present in cpoptions.
You could try to start Vim with -M:
vim -M foo
This disallows any changes to the file and prevents writing the file. See :help -M.
Another solution might be:
vim -m foo
This opens the file and you can edit it (you get the warning W10: Warning: Changing a readonly file), but you are not able to write it. The option write is reset (to nowrite) and prevents writing to any file. So this is global.
If you finally decide, that you need to write it anyway, you have to set the option with the command :set write. As said, this is global, so you are than able to write any file.
edited 16 hours ago
answered 17 hours ago
user7369280user7369280
803210
803210
Actually, I want to be able to change the contents of the files, just not save them. But then I also noticed now that there is a -m option, that prevents all kinds of saving.. HOWEVER.. I also would like to run :TOhtml and save the diff as html and -m prevents saving the html :-D
– boffman
16 hours ago
@boffman See my edit.
– user7369280
16 hours ago
Perfect. -w in combination with set [no]write solved my problems! Thanks again!
– boffman
11 hours ago
add a comment |
Actually, I want to be able to change the contents of the files, just not save them. But then I also noticed now that there is a -m option, that prevents all kinds of saving.. HOWEVER.. I also would like to run :TOhtml and save the diff as html and -m prevents saving the html :-D
– boffman
16 hours ago
@boffman See my edit.
– user7369280
16 hours ago
Perfect. -w in combination with set [no]write solved my problems! Thanks again!
– boffman
11 hours ago
Actually, I want to be able to change the contents of the files, just not save them. But then I also noticed now that there is a -m option, that prevents all kinds of saving.. HOWEVER.. I also would like to run :TOhtml and save the diff as html and -m prevents saving the html :-D
– boffman
16 hours ago
Actually, I want to be able to change the contents of the files, just not save them. But then I also noticed now that there is a -m option, that prevents all kinds of saving.. HOWEVER.. I also would like to run :TOhtml and save the diff as html and -m prevents saving the html :-D
– boffman
16 hours ago
@boffman See my edit.
– user7369280
16 hours ago
@boffman See my edit.
– user7369280
16 hours ago
Perfect. -w in combination with set [no]write solved my problems! Thanks again!
– boffman
11 hours ago
Perfect. -w in combination with set [no]write solved my problems! Thanks again!
– boffman
11 hours ago
add a comment |
It's much better to work on the file permissions, or to use the -M option as stated by @user7369280, but the funny thing is you can also launch vi with certain options that will prevent you to use the classic w or wq commands for example:
vi my_file -c 'cabbrev w <esc>' -c 'cabbrev wq <esc>'
It basically disables the commands w and wq with redefining them with the command escape (which doesn't do much)
You can still break out of that, though, with careful character entry and deletion, and cursor movements. Also, there are more commands that write files::wa,:x, ...
– L3viathan
17 hours ago
For some reason, those cabbrev commands/options does not seem to work for me, I can still save with :w / :wq
– boffman
17 hours ago
@boffman it might depends on the vi version. It's actually a vim only feature. You may try withabinstead ofcabbrev. For example:vi my_file -c 'ab wq <esc>'
– Yoric
16 hours ago
add a comment |
It's much better to work on the file permissions, or to use the -M option as stated by @user7369280, but the funny thing is you can also launch vi with certain options that will prevent you to use the classic w or wq commands for example:
vi my_file -c 'cabbrev w <esc>' -c 'cabbrev wq <esc>'
It basically disables the commands w and wq with redefining them with the command escape (which doesn't do much)
You can still break out of that, though, with careful character entry and deletion, and cursor movements. Also, there are more commands that write files::wa,:x, ...
– L3viathan
17 hours ago
For some reason, those cabbrev commands/options does not seem to work for me, I can still save with :w / :wq
– boffman
17 hours ago
@boffman it might depends on the vi version. It's actually a vim only feature. You may try withabinstead ofcabbrev. For example:vi my_file -c 'ab wq <esc>'
– Yoric
16 hours ago
add a comment |
It's much better to work on the file permissions, or to use the -M option as stated by @user7369280, but the funny thing is you can also launch vi with certain options that will prevent you to use the classic w or wq commands for example:
vi my_file -c 'cabbrev w <esc>' -c 'cabbrev wq <esc>'
It basically disables the commands w and wq with redefining them with the command escape (which doesn't do much)
It's much better to work on the file permissions, or to use the -M option as stated by @user7369280, but the funny thing is you can also launch vi with certain options that will prevent you to use the classic w or wq commands for example:
vi my_file -c 'cabbrev w <esc>' -c 'cabbrev wq <esc>'
It basically disables the commands w and wq with redefining them with the command escape (which doesn't do much)
answered 17 hours ago
YoricYoric
1,1142710
1,1142710
You can still break out of that, though, with careful character entry and deletion, and cursor movements. Also, there are more commands that write files::wa,:x, ...
– L3viathan
17 hours ago
For some reason, those cabbrev commands/options does not seem to work for me, I can still save with :w / :wq
– boffman
17 hours ago
@boffman it might depends on the vi version. It's actually a vim only feature. You may try withabinstead ofcabbrev. For example:vi my_file -c 'ab wq <esc>'
– Yoric
16 hours ago
add a comment |
You can still break out of that, though, with careful character entry and deletion, and cursor movements. Also, there are more commands that write files::wa,:x, ...
– L3viathan
17 hours ago
For some reason, those cabbrev commands/options does not seem to work for me, I can still save with :w / :wq
– boffman
17 hours ago
@boffman it might depends on the vi version. It's actually a vim only feature. You may try withabinstead ofcabbrev. For example:vi my_file -c 'ab wq <esc>'
– Yoric
16 hours ago
You can still break out of that, though, with careful character entry and deletion, and cursor movements. Also, there are more commands that write files:
:wa, :x, ...– L3viathan
17 hours ago
You can still break out of that, though, with careful character entry and deletion, and cursor movements. Also, there are more commands that write files:
:wa, :x, ...– L3viathan
17 hours ago
For some reason, those cabbrev commands/options does not seem to work for me, I can still save with :w / :wq
– boffman
17 hours ago
For some reason, those cabbrev commands/options does not seem to work for me, I can still save with :w / :wq
– boffman
17 hours ago
@boffman it might depends on the vi version. It's actually a vim only feature. You may try with
ab instead of cabbrev. For example: vi my_file -c 'ab wq <esc>'– Yoric
16 hours ago
@boffman it might depends on the vi version. It's actually a vim only feature. You may try with
ab instead of cabbrev. For example: vi my_file -c 'ab wq <esc>'– Yoric
16 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250472%2fhow-to-prevent-saving-file-in-vim%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
My understanding: You just told Vim that it should run in "readonly mode". The file itself is not readonly. If you
chmod -w foo, then Vim would not allow to write the file on:w!. It prints error "E504".– user7369280
17 hours ago
Ahhh you're right.. Thanks! I can accept that as an answer if you'd like.
– boffman
17 hours ago