7

It should be possible to do that by having ALT+UP generate consecutive keyboard input equivalent to cdspace..ENTER by means of a macro definition. But can't figure out how exactly to do it.

18

To do literally what you're asking, put the following line in your ~/.inputrc:

"\e\e[A": "cd ..\n"

Here \e\e[A is byte sequence that your terminal sends when you press Alt+Up (\e is parsed as the escape character), some terminals might send \e[1;3A~ or some other sequence instead. To find out what sequence your terminal sends, run cat and press the key (escape will display as ^[).

In bash, you can in principle bind a key to shell code, so in theory this should work:

bind -x '"\e\e[A":cd ..'

However as of bash 4.2 it doesn't work due to a hard-to-fix implementation bug. Zsh expert Stéphane Chazelas has a workaround:

bind -x '"\201":cd ..'
bind '"\e\e[A":"\201"'

The effect is somewhat confusing because the prompt isn't redrawn.

In bash ≥4, add shopt -s autocd to your ~/.bashrc. Then you can change to the parent directory (or any directory) by entering just .., without having to type the cd command.

  • This looks like a super-high quality answer, but for some reason it is not working in a Git Bash shell on Windows. (Haven't pulled out my Air to test there though yet...) Are you aware of any gotchas that uniquely affect Cygwin? – iconoclast Jul 25 '12 at 19:55
  • (I should have mentioned that it closes the terminal window.) – iconoclast Jul 25 '12 at 20:54
  • 2
    for some reason SE removed "@Gilles:" from the beginning of my comments. WTF!? – iconoclast Jul 26 '12 at 22:20
  • 1
    @iconoclast I think both Console2 and Mintty are portable. For Console2, unpack the zip and run it. For Mintty, you need mintty.exe plus the Cygwin dll either in the same directory or elsewhere on the %PATH%. Regarding the stripping of @Gilles: Eeeeek - what happened to my @ salutation? – Gilles Jul 26 '12 at 22:35
  • 1
    @iconoclast I think the founder and then lead developer (not a UX person) forced this feature through. Don't blame the UX designer for this. – Gilles Jul 26 '12 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.