diff --git a/lib/readline/histfile.c b/lib/readline/histfile.c
index dc64bde..921c7ad 100644
--- a/lib/readline/histfile.c
+++ b/lib/readline/histfile.c
@@ -402,7 +402,7 @@ read_history_range (const char *filename, int from, int to)
 		  add_history (line_start);
 		if (last_ts)
 		  {
-		    add_history_time (last_ts);
+		    add_history_comment (last_ts);
 		    last_ts = NULL;
 		  }
 	      }
@@ -685,7 +685,11 @@ history_do_write (const char *filename, int nelements, int overwrite)
 #else
       {
 	if (history_write_timestamps && the_history[i]->timestamp && the_history[i]->timestamp[0])
-	  buffer_size += strlen (the_history[i]->timestamp) + 1;
+	  {
+	    buffer_size += strlen (the_history[i]->timestamp) + 1;
+	    if (the_history[i]->cwd && the_history[i]->cwd[0])
+	      buffer_size += strlen (the_history[i]->cwd) + 1;
+	  }
 	buffer_size += strlen (the_history[i]->line) + 1;
       }
 #endif
@@ -726,6 +730,12 @@ mmap_error:
 	  {
 	    strcpy (buffer + j, the_history[i]->timestamp);
 	    j += strlen (the_history[i]->timestamp);
+	    if (the_history[i]->cwd && the_history[i]->cwd[0])
+	      {
+	        buffer[j++] = ' ';
+	        strcpy (buffer + j, the_history[i]->cwd);
+	        j += strlen (the_history[i]->cwd);
+	      }
 	    buffer[j++] = '\n';
 	  }
 	strcpy (buffer + j, the_history[i]->line);
diff --git a/lib/readline/history.c b/lib/readline/history.c
index 67158b1..4f5f52a 100644
--- a/lib/readline/history.c
+++ b/lib/readline/history.c
@@ -135,7 +135,7 @@ using_history (void)
 
 /* Return the number of bytes that the primary history entries are using.
    This just adds up the lengths of the_history->lines and the associated
-   timestamps. */
+   timestamps and working directories. */
 int
 history_total_bytes (void)
 {
@@ -217,7 +217,7 @@ history_get (int offset)
 }
 
 HIST_ENTRY *
-alloc_history_entry (char *string, char *ts)
+alloc_history_entry (char *string, char *ts, char* cwd)
 {
   HIST_ENTRY *temp;
 
@@ -226,6 +226,7 @@ alloc_history_entry (char *string, char *ts)
   temp->line = string ? savestring (string) : string;
   temp->data = (char *)NULL;
   temp->timestamp = ts;
+  temp->cwd = cwd;
 
   return temp;
 }
@@ -319,13 +320,29 @@ add_history (const char *string)
 	}
     }
 
-  temp = alloc_history_entry ((char *)string, hist_inittime ());
+  temp = alloc_history_entry ((char *)string, hist_inittime (), getwd(NULL));
 
   the_history[new_length] = (HIST_ENTRY *)NULL;
   the_history[new_length - 1] = temp;
   history_length = new_length;
 }
 
+/* Change the time stamp and possible current working directory of the most
+   recent history entry to STRING. */
+void
+add_history_comment (const char *string)
+{
+  char *cwd = strchr(string, ' ');
+
+  if (cwd) {
+      *cwd = '\0';
+      cwd++;
+  }
+
+  add_history_time(string);
+  add_history_cwd(cwd);
+}
+
 /* Change the time stamp of the most recent history entry to STRING. */
 void
 add_history_time (const char *string)
@@ -339,6 +356,20 @@ add_history_time (const char *string)
   hs->timestamp = savestring (string);
 }
 
+/* Change the current working directory of the most recent history entry to
+   STRING. */
+void
+add_history_cwd (const char *string)
+{
+  HIST_ENTRY *hs;
+
+  if (string == 0 || history_length < 1)
+    return;
+  hs = the_history[history_length - 1];
+  FREE (hs->cwd);
+  hs->cwd = savestring (string);
+}
+
 /* Free HIST and return the data so the calling application can free it
    if necessary and desired. */
 histdata_t
@@ -350,6 +381,7 @@ free_history_entry (HIST_ENTRY *hist)
     return ((histdata_t) 0);
   FREE (hist->line);
   FREE (hist->timestamp);
+  FREE (hist->cwd);
   x = hist->data;
   xfree (hist);
   return (x);
@@ -360,15 +392,19 @@ copy_history_entry (HIST_ENTRY *hist)
 {
   HIST_ENTRY *ret;
   char *ts;
+  char *cwd;
 
   if (hist == 0)
     return hist;
 
-  ret = alloc_history_entry (hist->line, (char *)NULL);
+  ret = alloc_history_entry (hist->line, (char *)NULL, (char *)NULL);
 
   ts = hist->timestamp ? savestring (hist->timestamp) : hist->timestamp;
   ret->timestamp = ts;
 
+  cwd = hist->cwd ? savestring (hist->cwd) : hist->cwd;
+  ret->cwd = cwd;
+
   ret->data = hist->data;
 
   return ret;
@@ -391,6 +427,7 @@ replace_history_entry (int which, const char *line, histdata_t data)
   temp->line = savestring (line);
   temp->data = data;
   temp->timestamp = savestring (old_value->timestamp);
+  temp->cwd = savestring (old_value->cwd);
   the_history[which] = temp;
 
   return (old_value);
diff --git a/lib/readline/history.h b/lib/readline/history.h
index cc3de29..84cb20f 100644
--- a/lib/readline/history.h
+++ b/lib/readline/history.h
@@ -46,11 +46,12 @@ typedef char *histdata_t;
 typedef struct _hist_entry {
   char *line;
   char *timestamp;		/* char * rather than time_t for read/write */
+  char *cwd;
   histdata_t data;
 } HIST_ENTRY;
 
 /* Size of the history-library-managed space in history entry HS. */
-#define HISTENT_BYTES(hs)	(strlen ((hs)->line) + strlen ((hs)->timestamp))
+#define HISTENT_BYTES(hs)	(strlen ((hs)->line) + strlen ((hs)->timestamp) + strlen((hs)->cwd))
 
 /* A structure used to pass the current state of the history stuff around. */
 typedef struct _hist_state {
@@ -82,10 +83,18 @@ extern void history_set_history_state PARAMS((HISTORY_STATE *));
    The associated data field (if any) is set to NULL. */
 extern void add_history PARAMS((const char *));
 
+/* Change the time stamp and possible current working directory of the most
+   recent history entry to STRING. */
+extern void add_history_comment PARAMS((const char *));
+
 /* Change the timestamp associated with the most recent history entry to
    STRING. */
 extern void add_history_time PARAMS((const char *));
 
+/* Change the current working directory associated with the most recent history
+   entry to STRING. */
+extern void add_history_cwd PARAMS((const char *));
+
 /* Remove an entry from the history list.  WHICH is the magic number that
    tells us which element to delete.  The elements are numbered from 0. */
 extern HIST_ENTRY *remove_history PARAMS((int));
@@ -93,9 +102,9 @@ extern HIST_ENTRY *remove_history PARAMS((int));
 /* Remove a set of entries from the history list: FIRST to LAST, inclusive */
 extern HIST_ENTRY **remove_history_range PARAMS((int, int));
 
-/* Allocate a history entry consisting of STRING and TIMESTAMP and return
-   a pointer to it. */
-extern HIST_ENTRY *alloc_history_entry PARAMS((char *, char *));
+/* Allocate a history entry consisting of STRING and TIMESTAMP and CWD and
+   return a pointer to it. */
+extern HIST_ENTRY *alloc_history_entry PARAMS((char *, char *, char*));
 
 /* Copy the history entry H, but not the (opaque) data pointer */
 extern HIST_ENTRY *copy_history_entry PARAMS((HIST_ENTRY *));
