0

Trying to define this alias in zsh I think the problem is that inside the string, $(...) doesn't work properly

alias ss='sudo $(history -p \!\!)'

The output is the usage of sudo

usage: sudo -h | -K | -k | -V
usage: sudo -v [-AknS] 
....
Yuri Aps
  • 929
  • 8
  • 13
  • Use a function, not an alias – Shawn Mar 03 '22 at 07:31
  • Did you try `history -p \!\!` on the command line? It does not do anything. Probably you meant `history !!`, but, according to the zsh man page: _History expansion allows you to use words from previous command lines in the command line **you are typing**_, so it does not work inside an alias or function. – user1934428 Mar 03 '22 at 11:54
  • 1
    Does this answer your question? [How do I access the last command from a zsh function?](https://stackoverflow.com/questions/70282669/how-do-i-access-the-last-command-from-a-zsh-function) – user1934428 Mar 03 '22 at 11:55
  • @user1934428 kind of, I think this solution my friend provided in the EDIT is better – Yuri Aps Mar 03 '22 at 14:18
  • 1
    If it works, post it as an answer (it is OK to answer your own question). Also add some explanation for how it works. Since `fc` is a builtin command of zsh, you find it documented in the man-page. – user1934428 Mar 04 '22 at 06:46

1 Answers1

0

For listing the history on zsh according to this doc:

fc -ln <number>

the modifiers:
-n - removes the line counter
<number> - starts the history from this number historically (-1 is the last used)

for the purpose of the alias the solution is:
alias ss='sudo $(fc -ln -1)'

Special thanks to @ericnb

Yuri Aps
  • 929
  • 8
  • 13