claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[BUG] Going to previous command in terminal editing loses current command (data loss)

Open peterkaminski opened this issue 7 months ago • 6 comments

Environment

  • Platform (select one):
    • [x] Anthropic API
    • [ ] AWS Bedrock
    • [ ] Google Vertex AI
    • [ ] Other:
  • Claude CLI version: v1.0.25
  • Operating System: macOS Sonoma 14.7
  • Terminal: iTerm2

Bug Description

As I was creating and editing a multiline command, I used Ctrl-P and Ctrl-N to move between displayed lines in the current command. Using Ctrl-P auto-repeat, I accidentally moved from the current, not-sent command, to the previously issued command. The command I was creating was no longer displayed or accessible with Ctrl-N. The command was about 10 lines long, and contained 3-4 minutes of concentration and careful creation, and it was lost.

Steps to Reproduce

  1. In the Claude Code terminal entry box, issue /clear
  2. Type a multiline prompt.
  3. Use Ctrl-P (and perhaps Ctrl-N) to move to different parts of the draft command to edit.
  4. Use Ctrl-P to back up to the /clear command.
  5. Use Ctrl-N to try to re-enter the draft command.

Expected Behavior

If I'm drafting a command and use Ctrl-P to move to the most recently performed command, I expect the terminal editor to preserve my draft command, and let me use Ctrl-N to move back down into it.

Actual Behavior

I used Ctrl-P within my draft command, and accidentally moved into the most recently performed command, /clear. At that point, I could no longer see my draft command, nor use Ctrl-N to move back to it. My draft command was lost.

Additional Context

My regular shell, zsh, works as I would expect. If I move from a partially completed draft command back in the history with Ctrl-P, I can use Ctrl-N to get back to the partially completed draft command.

I had previously run /terminal-setup in Claude Code to install Shift+Enter key binding for newlines in iTerm 2.

Clunky workaround: For a prompt/command that extends over more than one display line, draft it in a text editor instead of Claude Code, then copy/paste it into Claude Code.

peterkaminski avatar Jun 17 '25 18:06 peterkaminski

Hm, arrow down doesn't have this problem does it?

wolffiex avatar Jun 18 '25 00:06 wolffiex

@wolffiex, after Ctrl-P up to the previous command, neither Ctrl-N nor down arrow recovered the partially completed draft command.

I didn't test multiple up arrows and then down arrow. My preferred cursor movement keys are Emacs-style.

peterkaminski avatar Jun 18 '25 01:06 peterkaminski

I'm using Claude Code v. 1.0.96 on Windows 11 and had this happen to me several times now. If you accidently hit the up arrow when you are on the first character on the first line of a command, Claude jumps to the previous message. If you try to hit the down arrow to retrieve your typed command, it goes straight to "/clear". Claude effectively deletes your unsent command. If you are on a long command and hit the up arrow one too many times, you've lost your command. Very frustrating.

dgodfredson avatar Aug 29 '25 09:08 dgodfredson

What worked for me was to delete the "/clear" text with backspace, then click the down arrow.

hosay avatar Sep 11 '25 00:09 hosay

On Claude Code v1.0.113, I found that "ctrl + _" was the 'undo' equivalent, allowing me to restore several minutes of work. The hint '? for shortcuts' was a sanity saver this time.

lhtsl avatar Sep 16 '25 06:09 lhtsl

This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

github-actions[bot] avatar Dec 05 '25 10:12 github-actions[bot]