• hasecilu@lemm.ee
    link
    fedilink
    English
    arrow-up
    6
    ·
    edit-2
    3 days ago

    I prefer to use kudasai … but as some may already know, it should be at the end of the sentence command 😅, so I added a custom hook to my ZSH config to make it run commands as pacman -Syu kudasai.

    Essentially you can add any type of string matching in the function. Add this to the end of your .zshrc file:

    function kudasai_preexec() {
        # Check if the last word is ' kudasai'
        if [[ $1 == *" kudasai" ]]; then
            # Remove ' kudasai' and prepend 'sudo'
            cmd=${1%" kudasai"}
            eval "sudo $cmd"
            # Prevent the original command from running
            return 1
        fi
    }
    
    # Register the preexec hook
    autoload -Uz add-zsh-hook
    add-zsh-hook preexec kudasai_preexec
    

    Edit: as downside you always get 1 exit code with this implementation (little_annoying) .