diff --git a/doc/ref/user_pref_list.xml b/doc/ref/user_pref_list.xml
index c03ae38f4b..73a1da2ac9 100644
--- a/doc/ref/user_pref_list.xml
+++ b/doc/ref/user_pref_list.xml
@@ -73,6 +73,26 @@ Try HelpViewers:= [ "screen", "firefox", "xpdf" ];.
Defaults: [ [ "screen" ], "", "" ].
+HistoryBackwardSearchSkipIdenticalEntries
+HistoryBackwardSearchSkipIdenticalEntries
+
+-
+
+When a command is executed multiple times, it is also stored in history
+multiple times. Setting this option to true skips identical entries
+when searching backwards in history.
+
+
+
+Admissible values:
+true,
+false.
+
+
+
+Default: false.
+
+
HistoryMaxLines
SaveAndRestoreHistory
HistoryMaxLines,
diff --git a/lib/cmdledit.g b/lib/cmdledit.g
index 1755451977..6a2ce39aed 100644
--- a/lib/cmdledit.g
+++ b/lib/cmdledit.g
@@ -64,6 +64,18 @@ readline support.",
default := "default",
) );
+DeclareUserPreference( rec(
+ name:= "HistoryBackwardSearchSkipIdenticalEntries",
+ description:= [
+ "When a command is executed multiple times, it is also stored in history \
+multiple times. Setting this option to true skips identical entries \
+when searching backwards in history."
+ ],
+ default:= false,
+ values:= [ true, false ],
+ multi:= false,
+ ) );
+
if GAPInfo.CommandLineOptions.E then
############################################################################
@@ -543,6 +555,9 @@ GAPInfo.CommandLineEditFunctions.Functions.BackwardHistory := function(l)
while n > 1 do
n := n - 1;
if PositionSublist(hist[n], start) = 1 then
+ if UserPreference("HistoryBackwardSearchSkipIdenticalEntries") and hist[n] = l[3] then
+ continue;
+ fi;
GAPInfo.History.Pos := n;
GAPInfo.History.Last := n;
return [1, Length(l[3])+1, hist[n], l[4]];