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 bash, how to expand the !! in command line while interactively editing the command inside shell? I am working in vi edit mode.

When typing a new command line, I want to introduce the previous line and expand it.

I would like the expansion to occur before I execute the command.

share|improve this question

migrated from stackoverflow.com Feb 21 '12 at 20:37

This question came from our site for professional and enthusiast programmers.

up vote 1 down vote accepted

You may try to use Alt-^ in emacs mode (it's similar to Ctrl-Alt-e, but it should do only history expansion).

If it doesn't work for you (for example, there's no default binding for history expansion in vi mode), you can add the binding manually by placing

bind '"\e^": history-expand-line'

somewhere in your .bashrc, or

"\e^": history-expand-line

in your .inputrc

UPDATE. Pair remarks:

  1. if everything is ok, you should be able to press Alt-^ to substitute any !! sequence with your previous command, for example echo "!!" would become echo "previous_command with args"
  2. if it doesn't work as desired, you can check the binding with bind -P | grep history-expand (it should return something like history-expand-line can be found on "\e^")
share|improve this answer
It works only when the cursor is at the beginning of the line. It's quite a limitation for me. – Yehonathan Sharvit Feb 20 '12 at 12:26
It really shouldn't depend on cursor position and it doesn't depend for me. Are you talking about emacs mode or vi mode? Did you add the binding manually or use default one? – Alexis Feb 20 '12 at 16:56
I use vi mode. Alt-_ works and expand to last args of previous command. But Alt-^ doesn't do anything – Yehonathan Sharvit Feb 20 '12 at 21:03
@YehonathanSharvit Did you try to define custom key binding with bind '"\e^": history-expand-line'? Just execute this command in your command line and after this you should be able to use Alt-^ to expand !! or !$ or any other !-sequences. Let me know if it works for you. – Alexis Feb 20 '12 at 21:12
No it doesn't work. (I am on ubuntu 11.04) – Yehonathan Sharvit Feb 20 '12 at 21:17

This might work for you:

echo abc

Now type Ctrl-Alt-e at the same time (or in emacs parlance C-M-e).

echo abc
share|improve this answer
Not relevant because I would like the expansion to occur before I execute the command – Yehonathan Sharvit Feb 20 '12 at 16:11
C-M-e expands but does not execute. You must type return afterwards. I'm not sure I understand your question? If you want to see the last command just type Ctrl-p – potong Feb 20 '12 at 17:02
I want to insert the previous line while editing the current line and after inserting the previous line, I want to continue editing the current line. – Yehonathan Sharvit Feb 20 '12 at 20:58
@YehonathanSharvit Ctrl-Alt-e should do exactly what you described. But it doesn't work in vi mode and it may expand more then you want (it does history, alias and word expansions) – Alexis Feb 20 '12 at 21:19

This would make M-, insert the previous line in emacs mode:

"\e,": "!!\e^"

You can also make space expand designators like !! by binding it to magic-space:

" ": magic-space
share|improve this answer

Your Answer


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

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