DEV Community

Cover image for Dealing with Vim errors during Git operations
Nick Vahalik
Nick Vahalik

Posted on

Dealing with Vim errors during Git operations

I use a lot of git CLI tools and use vim as my editor. While doing a git rebase, I'll sometimes accidentally type :wQ into vim's command entry. However, this isn't a valid command:

E492: Not an editor command: wQ
Enter fullscreen mode Exit fullscreen mode

This aggrieves vim and it doesn't take such mistakes lightly. It registers this as an error causing a non-zero status code to be emitted upon exit and now git rebase thinks something has gone horribly wrong:

$ git rebase -i master
(much editing and such, including an accidental :wQ!)                  
error: there was a problem with the editor 'vi'
Enter fullscreen mode Exit fullscreen mode

I have been plagued by this for years and today finally found a solution.

The issue is one of mental model. Vim isn't just an editor. You can script it. And in some sense, you aren't using it as an editor, it's an editor with a REPL that you can use during editing. That is, your inputs after hitting ⎋ (Escape) will be treated as a live "script" and it's just as if you were running Vim with a script directly.

In other words, my input (the script) had an error.

Anyway, the result is that even after successfully :wq!, Vim still returns a status code of 1 and the rebase will fail. Because you screwed up, you have lost all possibility of getting a successful zero-status return code.

Thankfully, there is a way to work around this. Put this snippet into your ~/.vimrc:

command! WQ wq
command! Wq wq
command! W w
command! Q q
cnoreabbrev wQ wq
Enter fullscreen mode Exit fullscreen mode

This way, accidentally typing WQ|Wq|W|Q|wQ will now map to a valid command and all scripting sins are forgiven.

Now, even if I type :wQ, vim sees the correct command (:wq) and saves and exits successfully. Now I can rebase once instead of 3 times. Yay.

Top comments (0)