fish-shell/share/functions/__fish_prepend_sudo.fish
Fabian Homborg e646285bcb
Let __fish_prepend_sudo use the last commandline if there is no current one (#7079)
* docs/faq: Mention prepend_sudo

[ci skip]

* __fish_prepend_sudo: Use $history[1] if commandline is empty

Currently, if you press alt+s with an empty commandline, it inserts
"sudo", which seems fairly useless.

Now, it inserts "sudo " followed by the last history entry, which
makes it a replacement for `sudo !!`.

* docs
2020-06-04 18:25:02 +02:00

15 lines
488 B
Fish

function __fish_prepend_sudo -d "Prepend 'sudo ' to the beginning of the current commandline"
set -l cmd (commandline -po)
set -l cursor (commandline -C)
if test -z "$cmd"
commandline -r "sudo $history[1]"
else if test "$cmd[1]" != sudo
commandline -C 0
commandline -i "sudo "
commandline -C (math $cursor + 5)
else
commandline -r (string sub --start=6 (commandline -p))
commandline -C -- (math $cursor - 5)
end
end