10

The command fc will allow visual editing of the previous command. If I change my mind in the editor, how do I stop the command from being executed. In vim, typing q! or q both result in the command being executed and CTRL-C doesn't work either. Is the only option to delete the command in the editor and then wq?

12

From vi you can type :cq to exit without saving and with a non-zero return code. In this case the command will not be repeated.

Alternatively, you can usually suspend the editor with ctrl-z which gets you back to the shell without redoing the command. You still have to fg to restart the editor, but the tmp file will no longer be around, so you can safely quit the editor. Or you can kill -9 % this suspended editor.

I agree, it could be easier. Of course, you can always edit lines within bash using vi or emacs commands.

  • vi does not have a command ":cq". Did you like to type ":q!"? – schily Aug 30 '15 at 17:12
  • @schily sorry, by vi I was assuming vim. You can read about vim cq in quickfix. – meuh Aug 30 '15 at 17:19
  • Unfortunately, vim is not POSIX compliant (besides enhancements there are deviations). For a generic help, it is usually a good idea not to depend on local or vendor unique enhancements. BTW: :q! is sufficient as a POSIX vi will exit != 0 in this case. – schily Aug 30 '15 at 17:24
  • @schily Depending on vim being installed is probably safe in most cases, and in this case the asker specifically said q! didn't work for them – Michael Mrozek Aug 30 '15 at 20:17
  • 2
    This is because there was no error and thus the exit code is 0. In vi it works to type :c (results in an illegal command message) and then to type :q! and the remembered error results in an exit code != 0. This however does not work in vim. My previous assumption that :q! always results in an exit code != 0 was wrong. – schily Aug 30 '15 at 22:20
2

Perhaps, an easier way to remember is to delete the command (dd should do) and quit (ZZ or any other way to quit with saving should do). IMHO, :cq is quite arcane.

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.