I don't believe it's possible to completely prevent the user from doing this.
However, you could prevent the user from doing it inadvertently via the use of autocommands. Here's a rough proof of concept that ignores the new file name when the user attempts :w newname.txt
:
augroup MySave
autocmd BufWriteCmd * :w % | set nomodified
augroup END
However, note that there are many ways that a user can save text to a file in Vim. e.g. If you also want to catch :saveas
, you're going to also need to look into the BufFilePre/BufFilePost
autocommands; if you want to prevent the user writing ranges of the file, you'll also need to set up something for FileWriteCmd
etc.
You might also want to start Vim with the -Z
argument. See :help -Z
.
See :help autocmd.txt
for more details, in particular, :help Cmd-event
.
A sufficiently knowledgeable user could simply remove these autocommands, (and even if they couldn't, they could just copy the text to the clipboard!) so this will only be a speedbump for a someone determined to save the text elsewhere.