1

You are probably wondering what I could possibly need this for?

Well, my program will sometimes deal with erroneous strings that need to be manually fixed. So what I do, is I create a process which opens up a Vim, and suspends my program until a signal is emitted in which the file is changed (i.e. saved). I don't want to accidentally save without quitting; that would cause issues.

So is it possible to launch vim in which it is not possible to save without quitting?

  • 1
    How can you 'accidentally save' I mean when you type :w<CR> or :x<CR> it's at least 3 keypresses (4 if you're in insert mode and have to press <esc> before) I don't see how you can do that by mistake. Even for ZZ you don't always have a visual feedback but it's still hard to accidentally type that, right? (For ZZ you could create a mapping like nnoremap ZZ <nop>) – statox Jan 9 '17 at 9:17
  • 1
    @statox for me, it is a two keystroke as I delegated the colon to level one on my keyboard. I'll often save out of muscle memory habit. – Akiva Jan 9 '17 at 9:29
  • @statox It's easy to type 3 keypresses by mistake. You've never deleted the wrong file, restored the wrong backup etc? It's not like he accidentally pressed 3 keys where he meant to do nothing. – user859 Jan 9 '17 at 13:29
  • @Alex and @ Akiva mistyping a save command doesn't happen often enough so that I would want an option to disable saving but yes everybody has a different workflow and I can't understand that it happens to OP (Which is why I posted an answer btw :-) ) – statox Jan 9 '17 at 13:35
6

You could play with the write option. From :h 'write':

'write'         boolean (default on)
            global
            {not in Vi}
    Allows writing files.  When not set, writing a file is not allowed.
    Can be used for a view-only mode, where modifications to the text are
    still allowed.  Can be reset with the |-m| or |-M| command line
    argument.  Filtering text is still possible, even though this requires
    writing a temporary file.

So maybe you could use set nowrite in your vimrc: this will prevent any saving command to work (using autocommands to set nowrite on specific files would be even more efficient). And you can create a mapping to allow writing, something like nnoremap <key> :set write<CR> to enable write when to press the key or nnoremap <key> :set write!<CR> to toggle write/nowrite on each keypress.

4

As you have stated in the comments that you need to prevent the automatic saves you perform under muscle memory, you could just remap the particular combination(s) that you perform without thinking.

e.g.

nnoremap :w<cr> <nop>

You can start Vim with this mapping in place by using the "-c" command line option:

vim -c "nnoremap :w<cr> <nop>" no_saves_allowed.txt
3

You could key an event to an Autocommand:

autocmd BufWritePost :q

This will quit if you ever try to write the buffer.

EDIT: Got it the wrong way around, thought you wanted to not be able to quit without saving. Also BufWritePost rather than BufWrite will wait with quitting Vim until it's made sure the entire file is saved.

PS: A complete list of the autocommand events can be found on :h autocmd-events

  • Oh, I edited it to match the question already. The original version was autocmd BufUnload :w – Wolfie Jan 8 '17 at 0:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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