DEV Community

Jaakko Pallari
Jaakko Pallari

Posted on • Originally published at lepovirta.org on

Prevent Emacs from killing "git commit" on C-g

If you use Emacs and Git, you might have noticed that pressing control + G (C-g) while typing a commit message kills the whole commit process. Here’s how you can work around it.

C-g in Emacs is used for cancelling commands. It also sends a SIGINT signal to Emacs. The signal is also passed to the parent process, git, which causes git to end the whole process.

The workaround is to prevent the signal from getting to git. One such way is to call Emacs within a shell script. Shells typically ignore SIGINT signals. Here’s an example of such script:

#!/bin/sh -i
/usr/bin/emacs -nw "$@"
Enter fullscreen mode Exit fullscreen mode

Here shell script starts in an interactive mode, calls /usr/bin/emacs, and passes all the script’s parameters to Emacs. Save the script somewhere in your $PATH (for example $HOME/bin/emacs.sh), and make git use the script as its editor:

$ git config --global core.editor emacs.sh
Enter fullscreen mode Exit fullscreen mode

The same workaround works for Mercurial, too. Change the editor for hg:

$ hg config --config ui.editor emacs.sh
Enter fullscreen mode Exit fullscreen mode

If you want to use the script as an editor for all the command line programs, just change the EDITOR and VISUAL environment variables in your shell’s initialization files:

export EDITOR="emacs.sh"
export VISUAL="$EDITOR"
Enter fullscreen mode Exit fullscreen mode

Sources:

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay