mcfly icon indicating copy to clipboard operation
mcfly copied to clipboard

Multiline commands are sometimes split in bash history.

Open ghuls opened this issue 6 years ago • 9 comments

Multiline commands are sometimes split in bash history.

Pasting this in the terminal and then pasting the following:

some_function () {
  printf '1\t2\t3\n' \
    | awk -F '\t' '
    {
        print $1;
        print $2;
        print $3;
    }' \
    | cat 
}

In bash without mcfly, pressing the up key, will bring back this full entry (history command):

 2006  some_function () {   printf '1\t2\t3\n'     | awk -F '\t' '
    {
        print $1;
        print $2;
        print $3;
    }'     | cat ; }

With mcfly, this entry is splitted:

 1340  some_function () {   printf '1\t2\t3\n'     | awk -F '\t' '
 1341      {
 1342          print $1;
 1343          print $2;
 1344          print $3;
 1345      }'     | cat ; }

ghuls avatar Mar 06 '19 17:03 ghuls

That does sound like a real bug, thank you for reporting it.

cantino avatar Mar 16 '19 17:03 cantino

Are you sure that the normal bash history works correctly with multiline commands? When I try your example without mcfly, it works in the local session, but if I start a new session, it shows up as separate lines in history.

cantino avatar May 13 '19 15:05 cantino

Apparently rereading history from bash history does not preserve multiline commands: https://lists.gnu.org/archive/html/bug-bash/2011-02/msg00029.html. It is a pity as I use multiline command quite often.

ghuls avatar May 14 '19 14:05 ghuls

I see this is tagged wontfix. Would you be willing to merge a pull request for this?

shelvacu avatar Jun 11 '21 02:06 shelvacu

Sure, if you can find a solution! I'm not sure how you can fix it. Bash history without timestamps doesn't differentiate single and multiline, or does it?

cantino avatar Jun 11 '21 03:06 cantino

Once the history is written with history -a then the information is lost, but history 1 is reliably parseable. You'd need to restructure a bit and pipe it into the mcfly binary for parsing, maybe HISTTIMEFORMAT="%s " history 1 | mcfly record_history bash or something.

shelvacu avatar Jun 11 '21 07:06 shelvacu

Doesn't presence of a backslash at the end indicate multi-line?

richvim avatar Jun 17 '21 10:06 richvim

Multiline commands can also be started from unclosed quotes or parens. I would hope to avoid trying to parse bash commands to see which are part of a multi-line command, as that would be shell & version specific, and hard to get perfect.

shelvacu avatar Jun 17 '21 22:06 shelvacu

If you can get it working, I'm very open to a PR.

cantino avatar Jun 18 '21 00:06 cantino