1

This question already has an answer here:

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

marked as duplicate by ilkkachu, Rui F Ribeiro, thrig, countermode, GAD3R Apr 19 '17 at 13:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • This is pretty much answered in the question your linked question is marked as a duplicate of. Just use !em instead of !!. – ilkkachu Apr 18 '17 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 Apr 18 '17 at 16:39
1

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.

0

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 or ask your own question.