How to exit with an error code from nano

exit codenano

When using vim for “replying” to some program – typically git – it can be useful to deliberate exit with error, to abort the entire action that opened the window. Like

$ git commit -a
# vim opens, showing me the staged files and asking for a commit message
# oops, I see some stuff staged that wasn't supposed to be committed
:cq     # exit vim with nonzero exit code

This will prevent git from actually doing the commit.

Now unfortunately, some machines have nano configured as the default editor, so I occasionally find myself in that editor and correspondingly helpless.

What I've done for now was: log in with another ssh session and killall editor.

Is there a way to do the same thing from within nano itself, corresponding to vim's :cq?

Best Answer

This is what I can do in interactive sh (or bash etc.) with job control:

I can suspend nano with Ctrl+Z (^Z). Note: if the message is Suspension is not enabled then I need to press Alt+Z (M-Z) to enable. As far as I know these are default keybindings in nano.

If the command was nano || foo then foo will be triggered immediately.

Now I'm back in the shell with nano as a job (see jobs). kill %% will send a signal to it, still nano won't exit yet. I need to fg after sending the signal, only then nano terminates.

Not as fast as vim's :cq but still without additional SSH session.

Related Question