Bash command history editing multiple lines

bashcommand history

Over many years I used ksh. I like the possibility to use Esc v in command history to call the editor "vi". If this command in the history was spread over many lines – for example because of a while loop – "vi" shows this history also over many lines. With this feature it is easy possible to write complex statements without writing the input to a file.

Years ago I changes to bash. It has the same possibility with default shortcut CtrlX CtrlE. The slightly difference is that bash is merging all lines to one long line delimited with a semicolon. Syntax is still correct but we lose readability.

So what I am doing, I am calling ksh, if I see the commands become complex.

Is there a way to configure bash in a way not to merge the lines of the history and act as ksh is doing it?

Any help is welcome.

Best Answer

Use:

shopt -s lithist

lithist
If set, and the cmdhist option is enabled, multi-line commands are saved to the history with embedded newlines rather than using semicolon separators where possible.

I suspect the reason this isn't enabled by default is because people often use commands like history | grep something to find history entry numbers. If a history entry is split across multiple lines, the line that matches grep won't always contain the entry number.

Related Question