Skip to content

Commit

Permalink
fix(dirhistory): fix ALT+Up/Down key bindings for Terminal.app
Browse files Browse the repository at this point in the history
  • Loading branch information
mcornella committed Nov 2, 2021
1 parent 49458b8 commit 7f49494
Showing 1 changed file with 32 additions and 44 deletions.
76 changes: 32 additions & 44 deletions plugins/dirhistory/dirhistory.plugin.zsh
Expand Up @@ -121,35 +121,24 @@ function dirhistory_zle_dirhistory_future() {
}

zle -N dirhistory_zle_dirhistory_back
# xterm in normal mode
bindkey "\e[3D" dirhistory_zle_dirhistory_back
bindkey "\e[1;3D" dirhistory_zle_dirhistory_back
# Terminal.app
if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
bindkey "^[b" dirhistory_zle_dirhistory_back
fi
# iTerm2
if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
bindkey "^[^[[D" dirhistory_zle_dirhistory_back
fi
# Putty:
bindkey "\e\e[D" dirhistory_zle_dirhistory_back
# GNU screen:
bindkey "\eO3D" dirhistory_zle_dirhistory_back
bindkey "\e[3D" dirhistory_zle_dirhistory_back # xterm in normal mode
bindkey "\e[1;3D" dirhistory_zle_dirhistory_back # xterm in normal mode
bindkey "\e\e[D" dirhistory_zle_dirhistory_back # Putty
bindkey "\eO3D" dirhistory_zle_dirhistory_back # GNU screen
case "$TERM_PROGRAM" in
iTerm.app) bindkey "^[^[[D" dirhistory_zle_dirhistory_back ;; # iTerm2
Apple_Terminal) bindkey "^[b" dirhistory_zle_dirhistory_back ;; # Terminal.app
esac

zle -N dirhistory_zle_dirhistory_future
bindkey "\e[3C" dirhistory_zle_dirhistory_future
bindkey "\e[1;3C" dirhistory_zle_dirhistory_future
# Terminal.app
if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
bindkey "^[f" dirhistory_zle_dirhistory_future
fi
# iTerm2
if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
bindkey "^[^[[C" dirhistory_zle_dirhistory_future
fi
bindkey "\e\e[C" dirhistory_zle_dirhistory_future
bindkey "\eO3C" dirhistory_zle_dirhistory_future
bindkey "\e[3C" dirhistory_zle_dirhistory_future # xterm in normal mode
bindkey "\e[1;3C" dirhistory_zle_dirhistory_future # xterm in normal mode
bindkey "\e\e[C" dirhistory_zle_dirhistory_future # Putty
bindkey "\eO3C" dirhistory_zle_dirhistory_future # GNU screen
case "$TERM_PROGRAM" in
iTerm.app) bindkey "^[^[[C" dirhistory_zle_dirhistory_future ;; # iTerm2
Apple_Terminal) bindkey "^[f" dirhistory_zle_dirhistory_future ;; # Terminal.app
esac


#
Expand Down Expand Up @@ -181,22 +170,21 @@ function dirhistory_zle_dirhistory_down() {
}

zle -N dirhistory_zle_dirhistory_up
# xterm in normal mode
bindkey "\e[3A" dirhistory_zle_dirhistory_up
bindkey "\e[1;3A" dirhistory_zle_dirhistory_up
if [[ "$TERM_PROGRAM" == "Apple_Terminal" || "$TERM_PROGRAM" == "iTerm.app" ]]; then
bindkey "^[[A" dirhistory_zle_dirhistory_up
fi
# Putty:
bindkey "\e\e[A" dirhistory_zle_dirhistory_up
# GNU screen:
bindkey "\eO3A" dirhistory_zle_dirhistory_up
bindkey "\e[3A" dirhistory_zle_dirhistory_up # xterm in normal mode
bindkey "\e[1;3A" dirhistory_zle_dirhistory_up # xterm in normal mode
bindkey "\e\e[A" dirhistory_zle_dirhistory_up # Putty
bindkey "\eO3A" dirhistory_zle_dirhistory_up # GNU screen
case "$TERM_PROGRAM" in
iTerm.app) bindkey "^[^[[A" dirhistory_zle_dirhistory_up ;; # iTerm2
Apple_Terminal) bindkey "^[OA" dirhistory_zle_dirhistory_up ;; # Terminal.app
esac

zle -N dirhistory_zle_dirhistory_down
bindkey "\e[3B" dirhistory_zle_dirhistory_down
bindkey "\e[1;3B" dirhistory_zle_dirhistory_down
if [[ "$TERM_PROGRAM" == "Apple_Terminal" || "$TERM_PROGRAM" == "iTerm.app" ]]; then
bindkey "^[[B" dirhistory_zle_dirhistory_down
fi
bindkey "\e\e[B" dirhistory_zle_dirhistory_down
bindkey "\eO3B" dirhistory_zle_dirhistory_down
bindkey "\e[3B" dirhistory_zle_dirhistory_down # xterm in normal mode
bindkey "\e[1;3B" dirhistory_zle_dirhistory_down # xterm in normal mode
bindkey "\e\e[B" dirhistory_zle_dirhistory_down # Putty
bindkey "\eO3B" dirhistory_zle_dirhistory_down # GNU screen
case "$TERM_PROGRAM" in
iTerm.app) bindkey "^[^[[B" dirhistory_zle_dirhistory_down ;; # iTerm2
Apple_Terminal) bindkey "^[OB" dirhistory_zle_dirhistory_down ;; # Terminal.app
esac

7 comments on commit 7f49494

@mveinot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit seems to break up/down command history in Terminal.app on macOS. The default used to be that up/down traversed command history. Seemingly this change causes up/down to traverse directory history instead which is very crazy making when your muscle memory relied on it traversing history for decades.

@mcornella
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make Option+Up go to parent directory and Option+Down go to first child directory?

bindkey "^[[A" dirhistory_zle_dirhistory_up
bindkey "^[[B" dirhistory_zle_dirhistory_down

@mveinot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem to. I tried the CTRL-V trick to capture the escaped output for both UP and Option+UP and they are the same in Terminal.app - at least for me.

@mcornella
Copy link
Member Author

@mcornella mcornella commented on 7f49494 Nov 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've set it back to what it was, so it at least doesn't break anything, but it looks like you'll have to add new keyboard sequences in your Terminal.app profile to send the \e\e[A and \e\e[B sequences on Option+Up/Down.

@mveinot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried adding entries for this, but they don't seem to generate the expected result. I still just get regular up/down without option. Maybe this is a bug in Terminal?

@mcornella
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, that has to be \e\e[A and \e\e[B for Option-Up and Option-Down. I've edited the previous post.

@mveinot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something - It seems like the escape when creating entries in the preferences for terminal is \033 (achieved by pressing ESC)
So I have entries for \033\033[A and \033\033[B assigned to option+up and option+down respectively, but this still isn't generating the desired difference

Please sign in to comment.