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?

Praxeolitic
  • 2,468
  • 2
  • 15
  • 28

2 Answers2

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.

jecxjo
  • 2,660
  • 13
  • 24
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.

lcd047
  • 3,700
  • 16
  • 26
  • 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. – Giovanni Maggiorani Apr 14 '15 at 21:10