Pragmatism in today's world

Stop in-place editing of bash history items

Recently, since getting a new computer, I’ve noticed that I’ve been losing bash history items and it took a while to work out what was going on, though I’m still not completely sure as it never seemed to be so much of a problem.

I regularly use the up and down keys with context specific history. For example, I will type ma and then press up to search back through all the make commands I’ve used recently and then press enter to run it.

Sometimes, I’ll realise that I don’t actually want this command and edit it and press enter. Sometimes I’ll decide halfway through editing that really I should use a docker compose command instead and I’ll just back out of my edit via some key stroke that works. I’m not sure what I do here though, probably up/down, or maybe ctrl+c. Whatever I do, sometimes, the history for that line is now my edited mess and not the original command. Then later, when I go to try and find it via the up arrow, it’s missing.

This happened infrequently enough that I thought I was misremembering what was in the history, or that maybe it was another tab I was thinking about.

I never want the bash history to be editable; if I cancel out, then I want it back to what it was.

Fixing with revert-all-at-newline

This finally annoyed me enough that I sat down with the Internet to work out how to fix it with the revert-all-at-newline setting.

The revert-all-at-newline option in bash controls whether readline reverts any changes made to a history line when you press Enter. Note that this is part of readline’s behavior, so it affects command line editing in bash and other programs that use readline.

The simplest thing is to add this to .bashrc:

bind 'set revert-all-at-newline on'

Alternatively, you can create a .inputrc file with this in it:

$include /etc/inputrc
set revert-all-at-newline on

To view the current value of revert-all-at-newline, use:

bind -V | grep revert-all-at-newline

It solved my problem, and I’ve not yet found a case when I want it set the other way.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *