Search code examples
zsh

How to remove escapes from zsh history


If I use escaped EOLs in a command and view the history, the command includes escape characters which can't simply be copy/pasted.

eg

ls \
/etc

Shows in history like:

ls \\n/etc

Which I can't easily copy/paste/edit to a different terminal. Is there an option or other method to make the history result copy/pasteable?

Compared with bash which obliterates the \\n and shows ls /etc.

(Obviously this is a trivial example, imagine a much longer line command you want to copy/paste skipping an option on line 3 of many)


Solution

  • Set the HIST_REDUCE_BLANKS option to prevent irrelevant whitespace from being added to the history list.

    % ls \
    bin
    [output redacted]
    % history
    [...]
    1005 ls \\nbin
    % setopt HIST_REDUCE_BLANKS
    ls \
    bin
    [...]
    % history
    [...]
    1005 ls \\nbin
    1006 history
    1007 setopt HIST_REDUCE_BLANKS
    1008 ls bin