A way for command history to jump whole commands instead of individual lines?

59 views
Skip to first unread message

Hobo

unread,
Feb 15, 2025, 11:25:25 AM2/15/25
to iterm2-discuss
I tried googling around but couldn't find what I was looking for, maybe just because I don't know what terms to search for if it already exists.

I'd like to make it so that traversing the command history with up-arrow (or ctrl+p) and down-arrow (or ctrl+n) move through entire commands for each button press instead of individual lines of multi-line commands. I thought iTerm2 would be able to recognize this already, especially after the update adding selection boxes, but I didn't find an option for this behavior.

For a more concrete example of what I'm looking for, if I run the commands in the following order:

  1. ls -altrh
  2. curl \
      --request GET \
      https://google.com
  3. export TOKEN=mytoken
Currently pressing the up arrow 3 times will land me on "--request GET \": on first press is export, 2nd press is the URL, 3rd press is on the request option.

I'd like it so that after 3 presses, I'm on the 3rd previous command, in this case ls.

Adrian Bool

unread,
Feb 15, 2025, 1:13:32 PM2/15/25
to iterm2-discuss

At least on my config, hitting ctrl+r first results in the behaviour you're after...


--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/iterm2-discuss/53cb0edf-46a9-47b7-a5e6-424a721ba8f5n%40googlegroups.com.

Adrian Bool

unread,
Feb 16, 2025, 3:43:22 AM2/16/25
to iterm2-discuss

Here's another option...

Add the following to your ~/.zshrc:

bindkey "^[[A" history-search-backward

The "^[[A" codes refers to your up arrow; with this bindkey command changing the up arrow's normal association with "up-line-or-history" to "history-search-backward" - with the latter functioning in the way you described.

Note that this is not an iTerm thing; command history is controlled by your shell (zsh by default on Macs these days).

Cheers

aid

Hobo

unread,
Feb 16, 2025, 9:48:55 PM2/16/25
to iterm2-discuss
Hi, thanks for the suggestion.

I forgot to call out ctrl+r history in my post. I do use this feature but it has some drawbacks for my personal setup where it truncates the command, so I can't see the entire text for particularly large commands in the history.
It's great normally, but this is also why I was hoping iTerm would have a bit more contextual awareness for the command history and offer a better experience out of the box.

Hobo

unread,
Feb 16, 2025, 9:48:55 PM2/16/25
to iterm2-discuss
Hmm that's a great suggestion I wasn't aware of this. Doing it for forward and backwards with up and down arrows almost does what I want, but is missing some nuance. Once I press left to go back a character, then up/down should no longer search history and should instead move by line.
Thanks for the inspiration, I'll have to keep researching further into this. I do think the complexity level I'm looking for makes it better suited to a Terminal emulator feature rather than the shell itself but in the meantime I'll continue to see what I can discover. Thanks!

George Nachman

unread,
Mar 5, 2025, 6:29:27 PM3/5/25
to iterm2-...@googlegroups.com
Sorry for the delay in getting back to you.

If you install shell integration, cmd-shift-up and down arrows do this.

--

Hobo

unread,
Mar 7, 2025, 5:12:30 PM3/7/25
to iterm2-discuss
Oh wow thank you so much! That is exactly what I was hoping for.

Out of interest, where could I have found this feature in the docs? I tried searching a bit but I am guessing I didn't quite know the correct terms to use or look in the right places.

George Nachman

unread,
Mar 7, 2025, 5:19:51 PM3/7/25
to iterm2-...@googlegroups.com
It is mentioned here:


But I admit that’s not easy to find. Where did you look? I’d like to make it easier to find.

Hobo

unread,
Mar 10, 2025, 12:38:00 AM3/10/25
to iterm2-discuss
I think the first place I looked was command selection https://iterm2.com/documentation-command-selection.html
From here I may have even went to the shell integration page but skipped down to the "command history" subheader looking for it. After that I did some googling and nothing led me to the marks section for my answer, until you shared it directly.
Reply all
Reply to author
Forward
0 new messages