fish-shell/share/functions/__fish_prepend_sudo.fish
Enrico Borba f10de5f653
__fish_prepend_sudo: Toggle "sudo" on multiple presses (#7012)
At the moment calling __fish_prepend_sudo multiple times does not toggle
sudo, and also unnecessarily uses the `-c` flag to `commandline` to see if
the first token on the commandline is "sudo".

This change removes the `-c` switch and also toggles "sudo" on multiple
calls to __fish_prepend_sudo, while maintaining the cursor position and
while maintaining any spaces between "sudo" and the next token on the
commandline.
2020-05-17 11:15:15 +02:00

13 lines
419 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 "$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