I use emacs as an editor in the command line (aliased with alias em='emacs -nw') and would like to be able to make an alias for !em to open up the file most recently opened in emacs.

I've tried alias e='!em', alias e='${!}em', alias e='${!em}', but so far can't find anything that works.

I've found some related posts about aliasing !!:

alias for a command using !! to recall the last command

  • This is pretty much answered in the question your linked question is marked as a duplicate of. Just use !em instead of !!.
    – ilkkachu
    Commented Apr 18, 2017 at 14:17
  • @ilkkachu - thanks, but I tried that before I asked the question and it did not work. It does not recognize the em command (although they are both in .bashrc, and I did run . .bashrc of course)
    – galois
    Commented Apr 18, 2017 at 16:39

2 Answers 2


I don't think history expansion is done after expanding aliases, so you'd need to use history -p to do it explicitly:

alias e='eval "$(history -p !em)"'

Though that doesn't work either in this case, since aliases aren't expanded after command substitution. But functions work, so these two should do it:

em() { emacs -nw "$@"; }
e() { eval "$(history -p !em)"; }

The latter could be an alias.


If you alias em as you described, then you should not attempt to make an additional alias for !em. ! invokes the shell history mechanism, so the command "!em" will repeat the most recent use of your alias (which might not be the last file actually edited by emacs, however.)

Not the answer you're looking for? Browse other questions tagged .