15

Occasionally I absentmindedly type :w when I'm reading something. This causes the modification time of the file to change and sometimes causes my build system to unnecessarily rebuild a substantial chunk of a project.

Is there a way to set vim to simply not do anything if I try to write a file and it hasn't been changed?

2 Answers 2

15

Extending @lcd047's answer you can use cabbrev to modify w

:cabbrev w <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'up' : 'w')<CR>

When you type :w you will notice that it will be replaced with :up when you press return.

0
14

Not directly (that is, the behaviour of :w can't be changed), but you can learn to use :up instead of :w. It does exactly what you want.

1
  • 3
    Just let me add: I use a favourite keymapping for saving, and I have bound to it :update instead of :write to obtain the Original Poster requirement. Apr 14, 2015 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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