0

When I commit to git, I generally enter my message in Vi and then type :wq but just occasionally I miss and type :Wq which is clearly invalid. The problem is that when I do this, Vi exits with return code 1. Git sees this and thinks I aborted the commit and so I lose my commit message.

What I want is to still be able to abort a commit with :cq - so changing git to ignore the error is not ideal (although if no-one knows of anything else then I'd go for that) but to be able to still commit correctly when I've typed :Wq accidentally.

  • This doesn't quite make sense. If you :Wq, vim should spit an error at you but leave you still inside vim. Then if you execute the proper :wq, it would write and exit normally. – Patrick Mar 13 '18 at 12:40
  • That is exactly what happens, then when I type :wq it writes the file and quits with a return code of 1 - which git rightly interprets to mean that the editor failed in some way. – mjaggard Mar 14 '18 at 16:28
  • That's not "exit normally" :-). My vim does not exhibit the behavior you describe. If I :Wq then :wq, vim exits status 0. – Patrick Mar 14 '18 at 17:22
  • Good point. It seems that my Ubuntu box doesn't show this issue by MacOS does. – mjaggard Mar 15 '18 at 14:32
3

Simply add

cabbrev Wq wq

to your .vimrc file. This will make :Wq an alias for :wq.

  • I did this in March last year and had no problems until today when I typed :WQ :-( – mjaggard Aug 19 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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