Skip to content

Instantly share code, notes, and snippets.

@NotTheDr01ds
Created December 27, 2021 02:46
Show Gist options
  • Save NotTheDr01ds/6357e48b511735c42b94c4cc081ac5dc to your computer and use it in GitHub Desktop.
Save NotTheDr01ds/6357e48b511735c42b94c4cc081ac5dc to your computer and use it in GitHub Desktop.
Fish `sudo` replacement function
function fish_load_sudo_alias
function sudo
if functions -q -- "$argv[1]"
# Create a string which quotes each of the original arguments
# so that they can be safely passed into the new fish
# instance that is called by sudo.
set cmdline (
for arg in $argv
printf "\"%s\" " $arg
end
)
# We need to pass the function source to another fish instance.
# Since it is multi-line, any attempt to store the function in a
# variable results in an array, which also can't be passed to
# another fish instance.
#
# So first we escape the existing function (mostly in case it
# has '\n' literals in it, then we join it on "\n".
#
# After passing it into fish, the new shell splits it,
# unescapes it, and passes the function declaration to
# `source`, which loads it into memory in the new shell.
set -x function_src (string join "\n" (string escape --style=var (functions "$argv[1]")))
set argv fish -c 'string unescape --style=var (string split "\n" $function_src) | source; '$cmdline
command sudo -E $argv
else
command sudo $argv
end
end
end
@NotTheDr01ds
Copy link
Author

Place in ~/.config/fish/functions/fish_load_sudo_alias.fish, then load on demand by running fish_load_sudo_alias. At that point, this sudo alias will override the normal sudo command, allowing it to run Fish functions/aliases.

@Susensio
Copy link

Thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment