Skip to main content
The history command manages your PsySH command history. You can view past commands, search with patterns, save to files, and replay sequences of commands.

Syntax

history [options]

Alias

  • hist

Options

Display Options

--show
range
Alias: -sShow a specific range of history lines. Formats:
  • N - Show line N only
  • N..M - Show lines N through M
  • N.. - Show lines N to end
  • ..M - Show lines 0 through M
--head
integer
Alias: -HDisplay the first N items of history
--tail
integer
Alias: -TDisplay the last N items of history
--no-numbers
flag
Alias: -NOmit line numbers from output

Search Options

--grep
pattern
Filter history by pattern (supports regular expressions)
--insensitive
flag
Alias: -iMake grep search case-insensitive
--invert
flag
Alias: -vInvert grep match (show lines that don’t match)

Actions

--save
filename
Save history to the specified file
--replay
flag
Replay the selected history. Must be combined with --show, --head, or --tail.
--clear
flag
Clear all command history

Usage Examples

View All History

>>> history
  0| $name = 'Alice'
  1| $age = 30
  2| echo $name
  3| ls
  4| doc DateTime::format
  5| $date = new DateTime()

Show Specific Lines

>>> history --show 5
Shows only line 5.
>>> history --show 10..20
Shows lines 10 through 20.
>>> history --show 50..
Shows lines 50 to the end.
>>> history --show ..10
Shows lines 0 through 10.

Show First/Last N Commands

>>> history --head 10
>>> history -H 5
Shows the first 10 or 5 commands.
>>> history --tail 20
>>> history -T 10  
Shows the last 20 or 10 commands.

Search History

>>> history --grep /DateTime/
  4| doc DateTime::format
  5| $date = new DateTime()
  8| show DateTime
 12| ls DateTime
Matching text is highlighted in the output.
>>> history --grep /datetime/ -i
>>> history --grep /DATETIME/ --insensitive
Matches “DateTime”, “datetime”, “DATETIME”, etc.
>>> history --grep /ls/ --invert
Shows all commands that don’t contain “ls”.
>>> history --grep "/^\$[a-z]+\s*=/"
Matches lines starting with variable assignments.
>>> history --grep "/doc|show/"
Matches lines containing “doc” or “show”.

Combine Filters

>>> history --tail 100 --grep /function/
Search the last 100 commands for “function”.
>>> history --show 50..100 --grep /DateTime/ -i
Search lines 50-100 for “datetime” (case-insensitive).

Save History

>>> history --save /tmp/myhistory.txt
>>> history --tail 50 --save recent.txt
>>> history --grep /DateTime/ --save datetime-commands.txt
Saving history in /tmp/myhistory.txt...
History saved.
The file contains the raw commands without line numbers:
$name = 'Alice'
$age = 30  
echo $name
ls

Replay History

>>> history --show 5..10 --replay
Replays commands 5 through 10.
>>> history --tail 3 --replay
Replays the last 3 commands.
Replaying 3 lines of history

>>> $name = 'Bob'
>>> $age = 25
>>> echo "$name is $age years old"
Bob is 25 years old
You must limit history before replaying:
>>> history --replay  # Error!
You must limit history via --head, --tail or --show before replaying

>>> history --tail 5 --replay  # OK

Clear History

>>> history --clear
History cleared.
Permanently removes all command history.

Hide Line Numbers

>>> history --no-numbers
>>> history -N
$name = 'Alice'
$age = 30
echo $name  
ls
Useful when copying commands.

Advanced Examples

Find All Function Definitions

>>> history --grep "/function\s+\w+/"

Replay a Specific Sequence

>>> history --show 15..18 --replay
Re-runs commands 15, 16, 17, and 18 in order.

Export Recent Work

>>> history --tail 50 --save recent-work.php
Save the last 50 commands to review or document later.

Search and Save

>>> history --grep /class\s+/ --save class-definitions.php
Find and save all class definitions.

Review Specific Session

>>> history --show 100..200
View commands from a specific range.

History Persistence

History is automatically saved between sessions in:
  • Linux/macOS: ~/.local/share/psysh/history
  • Windows: %APPDATA%/PsySH/history
The history file is a plain text file with one command per line.

Keyboard Shortcuts

Within the REPL, you can navigate history using:
  • Up Arrow - Previous command
  • Down Arrow - Next command
  • Ctrl+R - Reverse search (if readline support is available)
  • Ctrl+S - Forward search (if readline support is available)

Search Highlighting

When using --grep, matched portions are highlighted:
>>> history --grep /date/
  5| $date = new DateTime()      # 'date' is highlighted
  8| echo $date->format('Y-m-d') # 'date' is highlighted

Constraints

The following option combinations are mutually exclusive:

Display Limiters

Only one of:
  • --show
  • --head
  • --tail
>>> history --head 10 --tail 20  # Error!
Please specify only one of --show, --head, --tail

Actions

Only one of:
  • --save
  • --replay
  • --clear
>>> history --save file.txt --clear  # Error!
Please specify only one of --save, --replay, --clear

Tips

Use regex anchors for precise matching:
>>> history --grep "/^ls/"      # Commands starting with 'ls'
>>> history --grep "/DateTime$/" # Commands ending with 'DateTime'
Create command snippets:
>>> history --show 50..55 --save snippet.php
Save useful command sequences for later reuse.
Test before replaying:
>>> history --show 10..15        # Review first
>>> history --show 10..15 --replay  # Then replay
The current history command is not included in the output:
>>> ls
>>> dump $x
>>> history  # Shows 'ls' and 'dump $x', but not this 'history' call

See Also

  • edit - Edit commands in an external editor
  • help - Get help on commands

Build docs developers (and LLMs) love