Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In zsh I can do Esc h for any command, get man page for this command, quit from the man pager and get my command back with all arguments. For example in my shell is entered:

zsh# tar -czvf docs.tgz docs

and I can call man for tar by pressing Esc h, with the cursor in any place.

Now, I want the same thing, but in bash. One possible solution is shown below, but I want to know if there are others.

bind '"\eh": "\C-a\ef\C-k\C-f\C^Cman \C-y\C-m\C-y\C-y\ey"'
share|improve this question

migrated from serverfault.com Oct 17 '11 at 11:33

This question came from our site for system and network administrators.

+1 I didn't know that little zsh tidbit. – dietbuddha Oct 28 '11 at 4:12

When we talk about commandline manipulation we're really talking about the readline library. Updating the readline bindings can be done from the commandline as in your example, in the main init file for readline /etc/inputrc (check this with echo $INPUTRC), or create a local init for your account ~/.inputrc. If you haven't already lookup GNU readline library (try here)

Having said that, here are some suggestions for your problem:

Suggestion 1

  • zsh uses readline and you could likely inspect the key binding for ESC-h (\eh) under an account with zsh and copy that binding into your account with bash. Some really smart people have put those bindings together and it may work without any tweaking.
    • In a zsh account use the bind command (bind -P [use the lowercase p switch for raw binding output]) to view the active bindings in a zsh account.
    • You may need to piece together the actual codes from the output of bind.
    • Add the complete line to your bindings in your bash account (like you tried to do above)

Suggestion 2

  • Follow the path in your example above

On that note your example above could work, but it needs a little tweaking. This worked for me

  • "\eh": "\C-a\C-kman \e1\e.\C-m\C-y\M-y"

I put my binding in /etc/inputrc. You must re-read inputrc at the commandline using the key sequence ctrl+x ctrl+r.

However, your question is how to do this for the word under the cursor. This worked for me:

  • "\eh": "\ef\eb\ed\C-y\e#man \C-y\C-m\C-p\C-p\C-a\C-d"

In short, it works like this:

For the key sequence \eh (ESC+h)

  • first find the end of the word, then the beginning (\ef\eb)
  • delete the current word (the one your interested in) and put it back (\ed\C-y). We need to get it in the kill buffer.
  • comment out the entire commandline and go to a new prompt (\e#)
  • type "man " on the commandline (notice the space)
  • paste your command of interest on the commandline and execute it (\C-y\C-m)
  • find the second (previous) entry in the command history ("man" and then you original command) (\C-p\C-p).
  • go to the beginning of the line and delete the comment char (\C-a\C-d)

Suggestion 3

  • Set readline to use emacs and vi mode. You may be able to use use most of your favorite, and very powerful editor commands in your keybindings.

Note: I have not tried implementing this option as a binding before, but I do use it at the commandline. You can set this via the inputrc file, or bashrc.

  • In inputrc: set editing-mode vi
  • In bashrc: set -o vi

The process of doing the binding is the seems similar to the default readline key sequences. But the examples in suggestion 2 should put you on the right path.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.