vimtricks.wiki Concise Vim tricks, one at a time.

How do I quit Vim with a non-zero exit code?

Answer

:cq

Explanation

The :cq command quits Vim and returns a non-zero exit code to the calling process. This is invaluable when Vim is launched as a subprocess by another tool, such as git commit or git rebase -i.

Why it matters

When Git opens Vim for a commit message or interactive rebase, it checks Vim's exit code. If Vim exits normally (:q), Git proceeds. If Vim exits with an error (:cq), Git aborts the operation.

Common use cases

  • Abort a git commit after Vim opens for the message
  • Cancel an interactive rebase mid-edit
  • Bail out of crontab -e without saving changes
  • Exit any tool that uses $EDITOR and checks the exit code

Example

" Git opens Vim for commit message
" You realize you're not ready to commit
:cq

Git sees the non-zero exit and prints:

Aborting commit due to empty commit message.

Tips

  • :cq takes an optional exit code: :cq 2 exits with code 2
  • Unlike :q!, which discards changes but exits successfully, :cq signals failure to the parent process
  • This works even if you have unsaved changes — no need to add !

Next

How do I always access my last yanked text regardless of deletes?